Skip to content

Commit

Permalink
fix image example buffer size calculation (#359)
Browse files Browse the repository at this point in the history
* fix image example buffer size calculation

* make deprecated since field semver-compliant

* replace manual slice size calculations with std::mem::size_of_val

* remove legacy numeric method `::max_value` in favor of the associated constant `::MAX`

* add type annotations to `std::mem::transmute` calls

* make cargo fmt happy
  • Loading branch information
JanNeuenfeld authored Aug 25, 2024
1 parent d62b0f5 commit 578327d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 25 deletions.
4 changes: 2 additions & 2 deletions examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ fn main() {
decoder.set_transformations(Transformations::ALPHA);
let mut reader = decoder.read_info().unwrap();

let mut buffer = vec![0u32; reader.output_buffer_size()];
let mut buffer = vec![0u32; reader.output_buffer_size() / 4];

// View of pixels as individual subpixels (avoids allocating a second pixel buffer).
let mut u8_buffer = unsafe {
std::slice::from_raw_parts_mut(
buffer.as_mut_ptr() as *mut u8,
buffer.len() * std::mem::size_of::<u32>(),
std::mem::size_of_val(&buffer),
)
};

Expand Down
8 changes: 4 additions & 4 deletions src/key_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use instant::{Duration, Instant};
#[cfg(not(feature = "web"))]
use std::time::{Duration, Instant};

use crate::{InputCallback, Key, KeyRepeat};
use crate::{key, InputCallback, Key, KeyRepeat};

pub struct KeyHandler {
pub key_callback: Option<Box<dyn InputCallback>>,
Expand Down Expand Up @@ -47,7 +47,7 @@ impl KeyHandler {
for (idx, is_down) in self.keys.iter().enumerate() {
if *is_down {
unsafe {
keys.push(std::mem::transmute(idx as u8));
keys.push(std::mem::transmute::<u8, key::Key>(idx as u8));
}
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@ impl KeyHandler {
for (idx, is_down) in self.keys.iter().enumerate() {
if *is_down && self.is_key_index_pressed(idx, repeat) {
unsafe {
keys.push(std::mem::transmute(idx as u8));
keys.push(std::mem::transmute::<u8, key::Key>(idx as u8));
}
}
}
Expand All @@ -99,7 +99,7 @@ impl KeyHandler {
for (idx, is_down) in self.keys.iter().enumerate() {
if !(*is_down) && self.is_key_index_released(idx) {
unsafe {
keys.push(std::mem::transmute(idx as u8));
keys.push(std::mem::transmute::<u8, key::Key>(idx as u8));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ impl Window {
/// ```
#[inline]
#[deprecated(
since = "0.26",
since = "0.26.0",
note = "use `set_target_fps` instead, this function will be removed in the future"
)]
pub fn limit_update_rate(&mut self, time: Option<Duration>) {
Expand Down
15 changes: 4 additions & 11 deletions src/os/posix/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ impl DisplayInfo {
// Add a black canvas into the framebuffer
let frame: Vec<u32> = vec![0xFF00_0000; (size.0 * size.1) as usize];
let slice = unsafe {
std::slice::from_raw_parts(
frame.as_ptr() as *const u8,
frame.len() * std::mem::size_of::<u32>(),
)
std::slice::from_raw_parts(frame.as_ptr() as *const u8, std::mem::size_of_val(&frame))
};
tempfile
.write_all(slice)
Expand Down Expand Up @@ -276,7 +273,7 @@ impl DisplayInfo {

// Give the buffer to the surface and commit
surface.attach(Some(buffer), 0, 0);
surface.damage(0, 0, i32::max_value(), i32::max_value());
surface.damage(0, 0, i32::MAX, i32::MAX);
surface.commit();

let xdg_config = Rc::new(RefCell::new(None));
Expand Down Expand Up @@ -348,10 +345,7 @@ impl DisplayInfo {
fd.seek(SeekFrom::Start(0))?;

let slice = unsafe {
std::slice::from_raw_parts(
buffer.as_ptr() as *const u8,
buffer.len() * std::mem::size_of::<u32>(),
)
std::slice::from_raw_parts(buffer.as_ptr() as *const u8, std::mem::size_of_val(buffer))
};

fd.write_all(slice)?;
Expand All @@ -363,8 +357,7 @@ impl DisplayInfo {
}

self.surface.attach(Some(buf), 0, 0);
self.surface
.damage(0, 0, i32::max_value(), i32::max_value());
self.surface.damage(0, 0, i32::MAX, i32::MAX);
self.surface.commit();

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/os/posix/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ impl Window {
}

fn update_key_state(&mut self, sym: xlib::KeySym, is_down: bool) {
if sym > u32::max_value() as xlib::KeySym {
if sym > u32::MAX as xlib::KeySym {
return;
}

Expand Down
7 changes: 1 addition & 6 deletions src/os/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ use web_sys::{window, CanvasRenderingContext2d, HtmlCanvasElement, ImageData};
#[inline(always)]
#[allow(dead_code)] // Only used on 32-bit builds currently
pub fn u32_as_u8<'a>(src: &'a [u32]) -> &'a [u8] {
unsafe {
std::slice::from_raw_parts(
src.as_ptr() as *mut u8,
src.len() * std::mem::size_of::<u32>(),
)
}
unsafe { std::slice::from_raw_parts(src.as_ptr() as *mut u8, std::mem::size_of_val(&src)) }
}

struct MouseState {
Expand Down

0 comments on commit 578327d

Please sign in to comment.