Skip to content

Commit

Permalink
Use NonZeroU32 for arguments to Surface::resize
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Apr 5, 2023
1 parent 20ab5fd commit 26304be
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 32 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ For now, the priority for new platforms is:
Example
==
```rust,no_run
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand All @@ -72,7 +73,12 @@ fn main() {
let size = window.inner_size();
(size.width, size.height)
};
surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(width * height) {
Expand Down
8 changes: 7 additions & 1 deletion examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -49,7 +50,12 @@ fn main() {

let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];

surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use image::GenericImageView;
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -35,7 +36,12 @@ fn main() {

match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
surface.resize(fruit.width(), fruit.height()).unwrap();
surface
.resize(
NonZeroU32::new(fruit.width()).unwrap(),
NonZeroU32::new(fruit.height()).unwrap(),
)
.unwrap();

let mut buffer = surface.buffer_mut().unwrap();
let width = fruit.width() as usize;
Expand Down
8 changes: 7 additions & 1 deletion examples/libxcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
mod example {
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
use std::num::NonZeroU32;
use x11rb::{
connection::Connection,
protocol::{
Expand Down Expand Up @@ -98,7 +99,12 @@ mod example {
match event {
Event::Expose(_) => {
// Draw a width x height red rectangle.
surface.resize(width.into(), height.into()).unwrap();
surface
.resize(
NonZeroU32::new(width.into()).unwrap(),
NonZeroU32::new(height.into()).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.fill(RED);
buffer.present().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroU32;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -57,7 +58,12 @@ fn main() {
};

// Resize surface if needed
surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();

// Draw something in the window
let mut buffer = surface.buffer_mut().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion examples/winit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -33,7 +34,12 @@ fn main() {
(size.width, size.height)
};

surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();

let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(width * height) {
Expand Down
6 changes: 5 additions & 1 deletion examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -32,7 +33,10 @@ fn main() {
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
surface
.resize(BUFFER_WIDTH as u32, BUFFER_HEIGHT as u32)
.resize(
NonZeroU32::new(BUFFER_WIDTH as u32).unwrap(),
NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(),
)
.unwrap();

let mut buffer = surface.buffer_mut().unwrap();
Expand Down
7 changes: 4 additions & 3 deletions src/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cocoa::base::{id, nil};
use cocoa::quartzcore::{transaction, CALayer, ContentsGravity};
use foreign_types::ForeignType;

use std::num::NonZeroU32;
use std::sync::Arc;

struct Buffer(Vec<u32>);
Expand Down Expand Up @@ -56,9 +57,9 @@ impl CGImpl {
})
}

pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
self.width = width;
self.height = height;
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.width = width.into();
self.height = height.into();
Ok(())
}

Expand Down
6 changes: 5 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use std::error::Error;
use std::num::NonZeroU32;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -29,7 +30,10 @@ pub enum SoftBufferError {
IncompleteDisplayHandle,

#[error("Surface size {width}x{height} out of range for backend.")]
SizeOutOfRange { width: u32, height: u32 },
SizeOutOfRange {
width: NonZeroU32,
height: NonZeroU32,
},

#[error("Platform error")]
PlatformError(Option<String>, Option<Box<dyn Error>>),
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod error;
mod util;

use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::ops;
#[cfg(any(wayland_platform, x11_platform))]
use std::rc::Rc;
Expand Down Expand Up @@ -76,7 +77,7 @@ macro_rules! make_dispatch {
}

impl SurfaceDispatch {
pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Expand Down Expand Up @@ -297,7 +298,7 @@ impl Surface {
/// in the upper-left corner of the window. It is recommended in most production use cases
/// to have the buffer fill the entire window. Use your windowing library to find the size
/// of the window.
pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.surface_impl.resize(width, height)
}

Expand Down
8 changes: 4 additions & 4 deletions src/orbital.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use raw_window_handle::OrbitalWindowHandle;
use std::{cmp, slice, str};
use std::{cmp, num::NonZeroU32, slice, str};

use crate::SoftBufferError;

Expand Down Expand Up @@ -68,9 +68,9 @@ impl OrbitalImpl {
})
}

pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
self.width = width;
self.height = height;
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.width = width.into();
self.height = height.into();
Ok(())
}

Expand Down
12 changes: 8 additions & 4 deletions src/wayland/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{error::unwrap, util, SoftBufferError};
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
use std::{cell::RefCell, num::NonZeroI32, rc::Rc};
use std::{
cell::RefCell,
num::{NonZeroI32, NonZeroU32},
rc::Rc,
};
use wayland_client::{
backend::{Backend, ObjectId},
globals::{registry_queue_init, GlobalListContents},
Expand Down Expand Up @@ -77,11 +81,11 @@ impl WaylandImpl {
})
}

pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.size = Some(
(|| {
let width = NonZeroI32::new(i32::try_from(width).ok()?)?;
let height = NonZeroI32::new(i32::try_from(height).ok()?)?;
let width = NonZeroI32::try_from(width).ok()?;
let height = NonZeroI32::try_from(height).ok()?;
Some((width, height))
})()
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?,
Expand Down
10 changes: 9 additions & 1 deletion src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use web_sys::ImageData;

use crate::SoftBufferError;
use std::convert::TryInto;
use std::num::NonZeroU32;

/// Display implementation for the web platform.
///
Expand Down Expand Up @@ -96,7 +97,14 @@ impl WebImpl {
}

/// Resize the canvas to the given dimensions.
pub(crate) fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub(crate) fn resize(
&mut self,
width: NonZeroU32,
height: NonZeroU32,
) -> Result<(), SoftBufferError> {
let width = width.get();
let height = height.get();

self.buffer.resize(total_len(width, height), 0);
self.canvas.set_width(width);
self.canvas.set_height(height);
Expand Down
8 changes: 4 additions & 4 deletions src/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use raw_window_handle::Win32WindowHandle;

use std::io;
use std::mem;
use std::num::NonZeroI32;
use std::num::{NonZeroI32, NonZeroU32};
use std::ptr::{self, NonNull};
use std::slice;

Expand Down Expand Up @@ -164,10 +164,10 @@ impl Win32Impl {
})
}

pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
let (width, height) = (|| {
let width = NonZeroI32::new(i32::try_from(width).ok()?)?;
let height = NonZeroI32::new(i32::try_from(height).ok()?)?;
let width = NonZeroI32::new(i32::try_from(width.get()).ok()?)?;
let height = NonZeroI32::new(i32::try_from(height.get()).ok()?)?;
Some((width, height))
})()
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?;
Expand Down
20 changes: 14 additions & 6 deletions src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{util, SoftBufferError};
use nix::libc::{shmat, shmctl, shmdt, shmget, IPC_PRIVATE, IPC_RMID};
use raw_window_handle::{XcbDisplayHandle, XcbWindowHandle, XlibDisplayHandle, XlibWindowHandle};
use std::ptr::{null_mut, NonNull};
use std::{fmt, io, mem, rc::Rc};
use std::{fmt, io, mem, num::NonZeroU32, rc::Rc};

use x11_dl::xlib::Display;
use x11_dl::xlib_xcb::Xlib_xcb;
Expand Down Expand Up @@ -230,7 +230,11 @@ impl X11Impl {
}

/// Resize the internal buffer to the given width and height.
pub(crate) fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub(crate) fn resize(
&mut self,
width: NonZeroU32,
height: NonZeroU32,
) -> Result<(), SoftBufferError> {
log::trace!(
"resize: window={:X}, size={}x{}",
self.window,
Expand All @@ -240,12 +244,16 @@ impl X11Impl {

// Width and height should fit in u16.
let width: u16 = width
.get()
.try_into()
.or(Err(SoftBufferError::SizeOutOfRange { width, height }))?;
let height: u16 = height.try_into().or(Err(SoftBufferError::SizeOutOfRange {
width: width.into(),
height,
}))?;
let height: u16 = height
.get()
.try_into()
.or(Err(SoftBufferError::SizeOutOfRange {
width: NonZeroU32::new(width.into()).unwrap(),
height,
}))?;

if width != self.width || height != self.height {
self.buffer
Expand Down

0 comments on commit 26304be

Please sign in to comment.