Skip to content

Commit e557e78

Browse files
committed
Handle zero-sized buffers internally
And default to a 0-sized buffer instead of panicking in `buffer_mut`.
1 parent 7af470a commit e557e78

26 files changed

+416
-432
lines changed

CHANGELOG.md

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

33
- **Breaking:** Remove generic type parameters `D` and `W` from `Buffer<'_>` struct.
4+
- **Breaking:** Use `u32` instead of `NonZeroU32`, and handle zero-sized buffers internally.
45

56
# 0.4.7
67

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() * buffer.height()) {

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/raytracing/main.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! Note that this is quite slow, you probably don't want to do realtime CPU raytracing in practice.
44
//!
55
//! [Ray Tracing in One Weekend]: https://raytracing.github.io/books/RayTracingInOneWeekend.html
6-
use std::num::NonZeroU32;
76
use winit::event::{DeviceEvent, ElementState, KeyEvent, WindowEvent};
87
use winit::event_loop::EventLoop;
98
use winit::keyboard::{Key, KeyCode, NamedKey, PhysicalKey};
@@ -32,10 +31,7 @@ fn main() {
3231
move |_elwt, window| {
3332
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
3433
surface
35-
.resize(
36-
NonZeroU32::new(window.inner_size().width).unwrap(),
37-
NonZeroU32::new(window.inner_size().height).unwrap(),
38-
)
34+
.resize(window.inner_size().width, window.inner_size().height)
3935
.unwrap();
4036
let game = Game::new();
4137
(surface, game)
@@ -53,11 +49,7 @@ fn main() {
5349
return;
5450
};
5551

56-
if let (Some(width), Some(height)) =
57-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
58-
{
59-
surface.resize(width, height).unwrap();
60-
}
52+
surface.resize(size.width, size.height).unwrap();
6153
}
6254
WindowEvent::RedrawRequested => {
6355
let Some((surface, game)) = surface else {

examples/rectangle.rs

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

61-
if let (Some(width), Some(height)) =
62-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
63-
{
64-
// Resize surface
65-
surface.resize(width, height).unwrap();
66-
}
60+
// Resize surface
61+
surface.resize(size.width, size.height).unwrap();
6762
}
6863
WindowEvent::RedrawRequested => {
6964
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 {

0 commit comments

Comments
 (0)