Skip to content

Commit

Permalink
Revert "fix image example buffer size calculation (#359)"
Browse files Browse the repository at this point in the history
This reverts commit 578327d.
  • Loading branch information
emoon authored Aug 25, 2024
1 parent 578327d commit f22a163
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 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() / 4];
let mut buffer = vec![0u32; reader.output_buffer_size()];

// 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,
std::mem::size_of_val(&buffer),
buffer.len() * std::mem::size_of::<u32>(),
)
};

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::{key, InputCallback, Key, KeyRepeat};
use crate::{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::<u8, key::Key>(idx as u8));
keys.push(std::mem::transmute(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::<u8, key::Key>(idx as u8));
keys.push(std::mem::transmute(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::<u8, key::Key>(idx as u8));
keys.push(std::mem::transmute(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.0",
since = "0.26",
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: 11 additions & 4 deletions src/os/posix/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ 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, std::mem::size_of_val(&frame))
std::slice::from_raw_parts(
frame.as_ptr() as *const u8,
frame.len() * std::mem::size_of::<u32>(),
)
};
tempfile
.write_all(slice)
Expand Down Expand Up @@ -273,7 +276,7 @@ impl DisplayInfo {

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

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

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

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

self.surface.attach(Some(buf), 0, 0);
self.surface.damage(0, 0, i32::MAX, i32::MAX);
self.surface
.damage(0, 0, i32::max_value(), i32::max_value());
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 as xlib::KeySym {
if sym > u32::max_value() as xlib::KeySym {
return;
}

Expand Down
7 changes: 6 additions & 1 deletion src/os/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ 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, std::mem::size_of_val(&src)) }
unsafe {
std::slice::from_raw_parts(
src.as_ptr() as *mut u8,
src.len() * std::mem::size_of::<u32>(),
)
}
}

struct MouseState {
Expand Down

0 comments on commit f22a163

Please sign in to comment.