Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix image example buffer size calculation #359

Merged
merged 6 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading