Skip to content

Commit ec10941

Browse files
committed
Remove unnecessary generics from Buffer
This should make it more ergonomic to pass around.
1 parent c8489f0 commit ec10941

File tree

13 files changed

+62
-96
lines changed

13 files changed

+62
-96
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:** Remove generic type parameters `D` and `W` from `Buffer<'_>` struct.
4+
35
# 0.4.7
46

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

examples/raytracing/game.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::time::{Duration, Instant};
22

33
use rand::rngs::SmallRng;
44
use rand::{Rng, SeedableRng};
5-
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
65
use softbuffer::Buffer;
76

87
use crate::camera::Camera;
@@ -44,21 +43,13 @@ impl Game {
4443
}
4544
}
4645

47-
pub fn draw(
48-
&self,
49-
buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>,
50-
scale_factor: f32,
51-
) {
46+
pub fn draw(&self, buffer: &mut Buffer<'_>, scale_factor: f32) {
5247
self.draw_scene(buffer, scale_factor);
5348
self.draw_ui(buffer, scale_factor);
5449
}
5550

5651
/// Draw the 3D scene.
57-
fn draw_scene(
58-
&self,
59-
buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>,
60-
scale_factor: f32,
61-
) {
52+
fn draw_scene(&self, buffer: &mut Buffer<'_>, scale_factor: f32) {
6253
// Raytracing is expensive, so we only do it once every 4x4 pixel.
6354
//
6455
// FIXME(madsmtm): Avoid the need for this once we can do hardware scaling.
@@ -130,11 +121,7 @@ impl Game {
130121
}
131122

132123
/// Draw a simple example UI on top of the scene.
133-
fn draw_ui(
134-
&self,
135-
buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>,
136-
scale_factor: f32,
137-
) {
124+
fn draw_ui(&self, buffer: &mut Buffer<'_>, scale_factor: f32) {
138125
struct Rect {
139126
left: f32,
140127
right: f32,

examples/rectangle.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
21
use softbuffer::Buffer;
32
use std::num::NonZeroU32;
43
use winit::event::{ElementState, KeyEvent, WindowEvent};
@@ -7,7 +6,7 @@ use winit::keyboard::{Key, NamedKey};
76

87
mod util;
98

10-
fn redraw(buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>, flag: bool) {
9+
fn redraw(buffer: &mut Buffer<'_>, flag: bool) {
1110
let width = buffer.width().get();
1211
let height = buffer.height().get();
1312
for y in 0..height {

src/backend_dispatch.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ macro_rules! make_dispatch {
7676

7777
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for SurfaceDispatch<D, W> {
7878
type Context = ContextDispatch<D>;
79-
type Buffer<'a> = BufferDispatch<'a, D, W> where Self: 'a;
79+
type Buffer<'a> = BufferDispatch<'a> where Self: 'a;
8080

8181
fn new(window: W, display: &Self::Context) -> Result<Self, InitError<W>>
8282
where
@@ -108,7 +108,7 @@ macro_rules! make_dispatch {
108108
}
109109
}
110110

111-
fn buffer_mut(&mut self) -> Result<BufferDispatch<'_, D, W>, SoftBufferError> {
111+
fn buffer_mut(&mut self) -> Result<BufferDispatch<'_>, SoftBufferError> {
112112
match self {
113113
$(
114114
$(#[$attr])*
@@ -138,14 +138,14 @@ macro_rules! make_dispatch {
138138
}
139139
}
140140

141-
pub(crate) enum BufferDispatch<'a, $dgen, $wgen> {
141+
pub(crate) enum BufferDispatch<'a> {
142142
$(
143143
$(#[$attr])*
144144
$name($buffer_inner),
145145
)*
146146
}
147147

148-
impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferDispatch<'a, D, W> {
148+
impl BufferInterface for BufferDispatch<'_> {
149149
#[inline]
150150
fn width(&self) -> NonZeroU32 {
151151
match self {
@@ -214,7 +214,7 @@ macro_rules! make_dispatch {
214214
}
215215
}
216216

217-
impl<D: fmt::Debug, W: fmt::Debug> fmt::Debug for BufferDispatch<'_, D, W> {
217+
impl fmt::Debug for BufferDispatch<'_> {
218218
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219219
match self {
220220
$(
@@ -232,7 +232,7 @@ macro_rules! make_dispatch {
232232
make_dispatch! {
233233
<D, W> =>
234234
#[cfg(target_os = "android")]
235-
Android(D, backends::android::AndroidImpl<D, W>, backends::android::BufferImpl<'a, D, W>),
235+
Android(D, backends::android::AndroidImpl<D, W>, backends::android::BufferImpl<'a>),
236236
#[cfg(all(
237237
feature = "x11",
238238
not(any(
@@ -243,7 +243,7 @@ make_dispatch! {
243243
target_os = "windows"
244244
))
245245
))]
246-
X11(std::sync::Arc<backends::x11::X11DisplayImpl<D>>, backends::x11::X11Impl<D, W>, backends::x11::BufferImpl<'a, D, W>),
246+
X11(std::sync::Arc<backends::x11::X11DisplayImpl<D>>, backends::x11::X11Impl<D, W>, backends::x11::BufferImpl<'a>),
247247
#[cfg(all(
248248
feature = "wayland",
249249
not(any(
@@ -254,7 +254,7 @@ make_dispatch! {
254254
target_os = "windows"
255255
))
256256
))]
257-
Wayland(std::sync::Arc<backends::wayland::WaylandDisplayImpl<D>>, backends::wayland::WaylandImpl<D, W>, backends::wayland::BufferImpl<'a, D, W>),
257+
Wayland(std::sync::Arc<backends::wayland::WaylandDisplayImpl<D>>, backends::wayland::WaylandImpl<D, W>, backends::wayland::BufferImpl<'a>),
258258
#[cfg(all(
259259
feature = "kms",
260260
not(any(
@@ -265,13 +265,13 @@ make_dispatch! {
265265
target_os = "windows"
266266
))
267267
))]
268-
Kms(std::sync::Arc<backends::kms::KmsDisplayImpl<D>>, backends::kms::KmsImpl<D, W>, backends::kms::BufferImpl<'a, D, W>),
268+
Kms(std::sync::Arc<backends::kms::KmsDisplayImpl<D>>, backends::kms::KmsImpl<D, W>, backends::kms::BufferImpl<'a>),
269269
#[cfg(target_os = "windows")]
270-
Win32(D, backends::win32::Win32Impl<D, W>, backends::win32::BufferImpl<'a, D, W>),
270+
Win32(D, backends::win32::Win32Impl<D, W>, backends::win32::BufferImpl<'a>),
271271
#[cfg(target_vendor = "apple")]
272-
CoreGraphics(D, backends::cg::CGImpl<D, W>, backends::cg::BufferImpl<'a, D, W>),
272+
CoreGraphics(D, backends::cg::CGImpl<D, W>, backends::cg::BufferImpl<'a>),
273273
#[cfg(target_family = "wasm")]
274-
Web(backends::web::WebDisplayImpl<D>, backends::web::WebImpl<D, W>, backends::web::BufferImpl<'a, D, W>),
274+
Web(backends::web::WebDisplayImpl<D>, backends::web::WebImpl<D, W>, backends::web::BufferImpl<'a>),
275275
#[cfg(target_os = "redox")]
276-
Orbital(D, backends::orbital::OrbitalImpl<D, W>, backends::orbital::BufferImpl<'a, D, W>),
276+
Orbital(D, backends::orbital::OrbitalImpl<D, W>, backends::orbital::BufferImpl<'a>),
277277
}

src/backends/android.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct AndroidImpl<D, W> {
2525
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for AndroidImpl<D, W> {
2626
type Context = D;
2727
type Buffer<'a>
28-
= BufferImpl<'a, D, W>
28+
= BufferImpl<'a>
2929
where
3030
Self: 'a;
3131

@@ -76,7 +76,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
7676
})
7777
}
7878

79-
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
79+
fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
8080
let native_window_buffer = self.native_window.lock(None).map_err(|err| {
8181
SoftBufferError::PlatformError(
8282
Some("Failed to lock ANativeWindow".to_owned()),
@@ -104,7 +104,6 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
104104
Ok(BufferImpl {
105105
native_window_buffer,
106106
buffer: util::PixelBuffer(buffer),
107-
marker: PhantomData,
108107
})
109108
}
110109

@@ -115,16 +114,15 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Android
115114
}
116115

117116
#[derive(Debug)]
118-
pub struct BufferImpl<'a, D: ?Sized, W> {
117+
pub struct BufferImpl<'a> {
119118
native_window_buffer: NativeWindowBufferLockGuard<'a>,
120119
buffer: util::PixelBuffer,
121-
marker: PhantomData<(&'a D, &'a W)>,
122120
}
123121

124122
// TODO: Move to NativeWindowBufferLockGuard?
125-
unsafe impl<'a, D, W> Send for BufferImpl<'a, D, W> {}
123+
unsafe impl Send for BufferImpl<'_> {}
126124

127-
impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'a, D, W> {
125+
impl BufferInterface for BufferImpl<'_> {
128126
fn width(&self) -> NonZeroU32 {
129127
NonZeroU32::new(self.native_window_buffer.width() as u32).unwrap()
130128
}

src/backends/cg.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<D, W> Drop for CGImpl<D, W> {
127127
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<D, W> {
128128
type Context = D;
129129
type Buffer<'a>
130-
= BufferImpl<'a, D, W>
130+
= BufferImpl<'a>
131131
where
132132
Self: 'a;
133133

@@ -258,29 +258,27 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
258258
Ok(())
259259
}
260260

261-
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
261+
fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
262262
Ok(BufferImpl {
263263
buffer: util::PixelBuffer(vec![0; self.width * self.height]),
264264
width: self.width,
265265
height: self.height,
266266
color_space: &self.color_space,
267267
layer: &mut self.layer,
268-
_phantom: PhantomData,
269268
})
270269
}
271270
}
272271

273272
#[derive(Debug)]
274-
pub struct BufferImpl<'a, D, W> {
273+
pub struct BufferImpl<'a> {
275274
width: usize,
276275
height: usize,
277276
color_space: &'a CGColorSpace,
278277
buffer: util::PixelBuffer,
279278
layer: &'a mut SendCALayer,
280-
_phantom: PhantomData<(D, W)>,
281279
}
282280

283-
impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
281+
impl BufferInterface for BufferImpl<'_> {
284282
fn width(&self) -> NonZeroU32 {
285283
NonZeroU32::new(self.width as u32).unwrap()
286284
}

src/backends/kms.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, Raw
1313

1414
use std::collections::HashSet;
1515
use std::fmt;
16-
use std::marker::PhantomData;
1716
use std::num::NonZeroU32;
1817
use std::os::unix::io::{AsFd, BorrowedFd};
1918
use std::sync::Arc;
@@ -95,7 +94,7 @@ struct Buffers {
9594
}
9695

9796
/// The buffer implementation.
98-
pub(crate) struct BufferImpl<'a, D: ?Sized, W: ?Sized> {
97+
pub(crate) struct BufferImpl<'a> {
9998
/// The mapping of the dump buffer.
10099
mapping: DumbMapping<'a>,
101100

@@ -119,11 +118,9 @@ pub(crate) struct BufferImpl<'a, D: ?Sized, W: ?Sized> {
119118

120119
/// Age of the back buffer.
121120
back_age: &'a mut u8,
122-
123-
_phantom: PhantomData<(&'a D, &'a W)>,
124121
}
125122

126-
impl<D: ?Sized + fmt::Debug, W: ?Sized + fmt::Debug> fmt::Debug for BufferImpl<'_, D, W> {
123+
impl fmt::Debug for BufferImpl<'_> {
127124
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128125
// FIXME: Derive instead once `DumbMapping` impls `Debug`.
129126
f.debug_struct("BufferImpl").finish_non_exhaustive()
@@ -146,7 +143,7 @@ struct SharedBuffer {
146143
impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> for KmsImpl<D, W> {
147144
type Context = Arc<KmsDisplayImpl<D>>;
148145
type Buffer<'a>
149-
= BufferImpl<'a, D, W>
146+
= BufferImpl<'a>
150147
where
151148
Self: 'a;
152149

@@ -252,7 +249,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
252249
}
253250
*/
254251

255-
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
252+
fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
256253
// Map the dumb buffer.
257254
let set = self
258255
.buffer
@@ -287,7 +284,6 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
287284
device: self.display.device.clone(),
288285
front_age,
289286
back_age,
290-
_phantom: PhantomData,
291287
})
292288
}
293289
}
@@ -308,7 +304,7 @@ impl<D: ?Sized, W: ?Sized> Drop for KmsImpl<D, W> {
308304
}
309305
}
310306

311-
impl<D: ?Sized, W: ?Sized> BufferInterface for BufferImpl<'_, D, W> {
307+
impl BufferInterface for BufferImpl<'_> {
312308
fn width(&self) -> NonZeroU32 {
313309
self.size.0
314310
}

src/backends/orbital.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> OrbitalImpl<D, W> {
8080
impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for OrbitalImpl<D, W> {
8181
type Context = D;
8282
type Buffer<'a>
83-
= BufferImpl<'a, D, W>
83+
= BufferImpl<'a>
8484
where
8585
Self: 'a;
8686

@@ -116,7 +116,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Orbital
116116
Ok(())
117117
}
118118

119-
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
119+
fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
120120
let (window_width, window_height) = window_size(self.window_fd());
121121
let pixels = if self.width as usize == window_width && self.height as usize == window_height
122122
{
@@ -137,7 +137,6 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Orbital
137137
height: self.height,
138138
presented: &mut self.presented,
139139
pixels,
140-
_phantom: PhantomData,
141140
})
142141
}
143142
}
@@ -149,16 +148,15 @@ enum Pixels {
149148
}
150149

151150
#[derive(Debug)]
152-
pub struct BufferImpl<'a, D, W> {
151+
pub struct BufferImpl<'a> {
153152
window_fd: usize,
154153
width: u32,
155154
height: u32,
156155
presented: &'a mut bool,
157156
pixels: Pixels,
158-
_phantom: PhantomData<(D, W)>,
159157
}
160158

161-
impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
159+
impl BufferInterface for BufferImpl<'_> {
162160
fn width(&self) -> NonZeroU32 {
163161
NonZeroU32::new(self.width as u32).unwrap()
164162
}

0 commit comments

Comments
 (0)