Skip to content

Commit 76304d2

Browse files
committed
Handle zero-sized buffers internally
And default to a 0-sized buffer instead of panicking in `buffer_mut`.
1 parent 53ccc11 commit 76304d2

25 files changed

+386
-429
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- **Breaking:** Use `u32` instead of `NonZeroU32`, and handle zero-sized buffers internally.
4+
35
# 0.4.7
46

57
- Fix documentation building on `docs.rs`.

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ To run the Android-specific example on an Android phone: `cargo apk r --example
6868
## Example
6969

7070
```rust,no_run
71-
use std::num::NonZeroU32;
7271
use std::rc::Rc;
7372
use winit::event::{Event, WindowEvent};
7473
use winit::event_loop::{ControlFlow, EventLoop};
@@ -102,12 +101,7 @@ fn main() {
102101
return;
103102
};
104103
let size = window.inner_size();
105-
surface
106-
.resize(
107-
NonZeroU32::new(size.width).unwrap(),
108-
NonZeroU32::new(size.height).unwrap(),
109-
)
110-
.unwrap();
104+
surface.resize(size.width, size.height).unwrap();
111105
112106
let mut buffer = surface.buffer_mut().unwrap();
113107
for index in 0..(buffer.width().get() * buffer.height().get()) {

benches/buffer_mut.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
fn buffer_mut(c: &mut criterion::Criterion) {
99
use criterion::black_box;
1010
use softbuffer::{Context, Surface};
11-
use std::num::NonZeroU32;
1211
use winit::event_loop::ControlFlow;
1312
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
1413

@@ -27,12 +26,7 @@ fn buffer_mut(c: &mut criterion::Criterion) {
2726
let mut surface = Surface::new(&context, &window).unwrap();
2827

2928
let size = window.inner_size();
30-
surface
31-
.resize(
32-
NonZeroU32::new(size.width).unwrap(),
33-
NonZeroU32::new(size.height).unwrap(),
34-
)
35-
.unwrap();
29+
surface.resize(size.width, size.height).unwrap();
3630

3731
c.bench_function("buffer_mut()", |b| {
3832
b.iter(|| {

examples/animation.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[cfg(not(target_family = "wasm"))]
22
use rayon::prelude::*;
33
use std::f64::consts::PI;
4-
use std::num::NonZeroU32;
54
use web_time::Instant;
65
use winit::event::{KeyEvent, WindowEvent};
76
use winit::event_loop::{ControlFlow, EventLoop};
@@ -46,11 +45,7 @@ fn main() {
4645
return;
4746
};
4847

49-
if let (Some(width), Some(height)) =
50-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
51-
{
52-
surface.resize(width, height).unwrap();
53-
}
48+
surface.resize(size.width, size.height).unwrap();
5449
}
5550
WindowEvent::RedrawRequested => {
5651
let Some(surface) = surface else {

examples/drm.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ mod imple {
1919
use raw_window_handle::{DisplayHandle, DrmDisplayHandle, DrmWindowHandle, WindowHandle};
2020
use softbuffer::{Context, Surface};
2121

22-
use std::num::NonZeroU32;
2322
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd};
2423
use std::path::Path;
2524
use std::time::{Duration, Instant};
@@ -114,10 +113,7 @@ mod imple {
114113

115114
// Resize the surface.
116115
let (width, height) = mode.size();
117-
surface.resize(
118-
NonZeroU32::new(width as u32).unwrap(),
119-
NonZeroU32::new(height as u32).unwrap(),
120-
)?;
116+
surface.resize(width as u32, height as u32)?;
121117

122118
// Start drawing to it.
123119
let start = Instant::now();

examples/fruit.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use image::GenericImageView;
2-
use std::num::NonZeroU32;
32
use winit::event::{KeyEvent, WindowEvent};
43
use winit::event_loop::{ControlFlow, EventLoop};
54
use winit::keyboard::{Key, NamedKey};
@@ -27,12 +26,7 @@ fn main() {
2726
// Intentionally only set the size of the surface once, at creation.
2827
// This is needed if the window chooses to ignore the size we passed in above, and for the
2928
// platforms softbuffer supports that don't yet extract the size from the window.
30-
surface
31-
.resize(
32-
NonZeroU32::new(width).unwrap(),
33-
NonZeroU32::new(height).unwrap(),
34-
)
35-
.unwrap();
29+
surface.resize(width, height).unwrap();
3630
surface
3731
},
3832
)

examples/libxcb.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,7 @@ mod example {
117117
match event {
118118
Event::Expose(_) => {
119119
// Draw a width x height red rectangle.
120-
surface
121-
.resize(
122-
NonZeroU32::new(width.into()).unwrap(),
123-
NonZeroU32::new(height.into()).unwrap(),
124-
)
125-
.unwrap();
120+
surface.resize(width.into(), height.into()).unwrap();
126121
let mut buffer = surface.buffer_mut().unwrap();
127122
buffer.fill(RED);
128123
buffer.present().unwrap();

examples/rectangle.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
22
use softbuffer::Buffer;
3-
use std::num::NonZeroU32;
43
use winit::event::{ElementState, KeyEvent, WindowEvent};
54
use winit::event_loop::{ControlFlow, EventLoop};
65
use winit::keyboard::{Key, NamedKey};
@@ -59,12 +58,8 @@ fn main() {
5958
return;
6059
};
6160

62-
if let (Some(width), Some(height)) =
63-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
64-
{
65-
// Resize surface
66-
surface.resize(width, height).unwrap();
67-
}
61+
// Resize surface
62+
surface.resize(size.width, size.height).unwrap();
6863
}
6964
WindowEvent::RedrawRequested => {
7065
let Some(surface) = surface else {

examples/winit.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::num::NonZeroU32;
21
use winit::event::{KeyEvent, WindowEvent};
32
use winit::event_loop::{ControlFlow, EventLoop};
43
use winit::keyboard::{Key, NamedKey};
@@ -33,11 +32,7 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
3332
return;
3433
};
3534

36-
if let (Some(width), Some(height)) =
37-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
38-
{
39-
surface.resize(width, height).unwrap();
40-
}
35+
surface.resize(size.width, size.height).unwrap();
4136
}
4237
WindowEvent::RedrawRequested => {
4338
let Some(surface) = surface else {

examples/winit_multithread.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod util;
44

55
#[cfg(not(target_family = "wasm"))]
66
pub mod ex {
7-
use std::num::NonZeroU32;
87
use std::sync::{mpsc, Arc, Mutex};
8+
use winit::dpi::PhysicalSize;
99
use winit::event::{KeyEvent, WindowEvent};
1010
use winit::event_loop::{ControlFlow, EventLoop, OwnedDisplayHandle};
1111
use winit::keyboard::{Key, NamedKey};
@@ -16,20 +16,20 @@ pub mod ex {
1616
type Surface = softbuffer::Surface<OwnedDisplayHandle, Arc<Window>>;
1717

1818
fn render_thread(
19-
do_render: mpsc::Receiver<(Arc<Mutex<Surface>>, NonZeroU32, NonZeroU32)>,
19+
do_render: mpsc::Receiver<(Arc<Mutex<Surface>>, PhysicalSize<u32>)>,
2020
done: mpsc::Sender<()>,
2121
) {
2222
loop {
2323
tracing::info!("waiting for render...");
24-
let Ok((surface, width, height)) = do_render.recv() else {
24+
let Ok((surface, size)) = do_render.recv() else {
2525
tracing::info!("main thread destroyed");
2626
break;
2727
};
2828

2929
// Perform the rendering.
3030
let mut surface = surface.lock().unwrap();
3131
tracing::info!("resizing...");
32-
surface.resize(width, height).unwrap();
32+
surface.resize(size.width, size.height).unwrap();
3333

3434
let mut buffer = surface.buffer_mut().unwrap();
3535
for y in 0..buffer.height() {
@@ -96,13 +96,9 @@ pub mod ex {
9696

9797
let size = window.inner_size();
9898
tracing::info!("got size: {size:?}");
99-
if let (Some(width), Some(height)) =
100-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
101-
{
102-
// Start the render and then finish it.
103-
start_render.send((surface.clone(), width, height)).unwrap();
104-
finish_render.recv().unwrap();
105-
}
99+
// Start the render and then finish it.
100+
start_render.send((surface.clone(), size)).unwrap();
101+
finish_render.recv().unwrap();
106102
}
107103
WindowEvent::CloseRequested
108104
| WindowEvent::KeyboardInput {

0 commit comments

Comments
 (0)