Skip to content

Commit 2317f29

Browse files
committed
Change As(Mut)Slice2 type parameter to an associated type
This is a better match and allows making various Target impls generic over any pixel format. The custom Framebuf type for SDL2 could be removed.
1 parent 8faf138 commit 2317f29

File tree

6 files changed

+78
-100
lines changed

6 files changed

+78
-100
lines changed

core/src/render/target.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ impl<B, F: Default> Colorbuf<B, F> {
4949
}
5050
}
5151

52-
impl<T, B: AsMutSlice2<T>, F> AsMutSlice2<T> for Colorbuf<B, F> {
53-
fn as_mut_slice2(&mut self) -> MutSlice2<'_, T> {
52+
impl<B: AsMutSlice2, F> AsMutSlice2 for Colorbuf<B, F> {
53+
type Elem = B::Elem;
54+
#[inline]
55+
fn as_mut_slice2(&mut self) -> MutSlice2<'_, Self::Elem> {
5456
self.buf.as_mut_slice2()
5557
}
5658
}
@@ -78,9 +80,9 @@ impl<T: Target> Target for &RefCell<T> {
7880

7981
impl<Col, Fmt, Dep> Target for Framebuf<Colorbuf<Col, Fmt>, Dep>
8082
where
81-
Col: AsMutSlice2<u32>,
82-
Dep: AsMutSlice2<f32>,
83-
Color4: IntoPixel<u32, Fmt>,
83+
Col: AsMutSlice2,
84+
Dep: AsMutSlice2<Elem = f32>,
85+
Color4: IntoPixel<Col::Elem, Fmt>,
8486
{
8587
/// Rasterizes `scanline` into this framebuffer.
8688
fn rasterize<V: Vary, Fs: FragmentShader<V>>(
@@ -96,8 +98,8 @@ where
9698

9799
impl<Buf, Fmt> Target for Colorbuf<Buf, Fmt>
98100
where
99-
Buf: AsMutSlice2<u32>,
100-
Color4: IntoPixel<u32, Fmt>,
101+
Buf: AsMutSlice2,
102+
Color4: IntoPixel<Buf::Elem, Fmt>,
101103
{
102104
/// Rasterizes `scanline` into this `u32` color buffer.
103105
/// Does no z-buffering.
@@ -133,11 +135,11 @@ impl Target for Buf2<Color3> {
133135
}
134136
}
135137

136-
pub fn rasterize<T, V: Vary>(
137-
buf: &mut impl AsMutSlice2<T>,
138+
pub fn rasterize<B: AsMutSlice2, V: Vary>(
139+
buf: &mut B,
138140
mut sl: Scanline<V>,
139141
fs: &impl FragmentShader<V>,
140-
mut conv: impl FnMut(Color4) -> T,
142+
mut conv: impl FnMut(Color4) -> B::Elem,
141143
ctx: &Context,
142144
) -> Throughput {
143145
let x0 = sl.xs.start;
@@ -158,12 +160,12 @@ pub fn rasterize<T, V: Vary>(
158160
io
159161
}
160162

161-
pub fn rasterize_fb<T, V: Vary>(
162-
cbuf: &mut impl AsMutSlice2<T>,
163-
zbuf: &mut impl AsMutSlice2<f32>,
163+
pub fn rasterize_fb<B: AsMutSlice2, V: Vary>(
164+
cbuf: &mut B,
165+
zbuf: &mut impl AsMutSlice2<Elem = f32>,
164166
mut sl: Scanline<V>,
165167
fs: &impl FragmentShader<V>,
166-
mut conv: impl FnMut(Color4) -> T,
168+
mut conv: impl FnMut(Color4) -> B::Elem,
167169
ctx: &Context,
168170
) -> Throughput {
169171
let x0 = sl.xs.start;

core/src/render/tex.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ impl SamplerRepeatPot {
224224
/// Creates a new `SamplerRepeatPot` based on the dimensions of `tex`.
225225
/// # Panics
226226
/// If the width or height of `tex` is not a power of two.
227-
pub fn new<C>(tex: &Texture<impl AsSlice2<C>>) -> Self {
227+
pub fn new(tex: &Texture<impl AsSlice2>) -> Self {
228228
let w = tex.width() as u32;
229229
let h = tex.height() as u32;
230230
assert!(w.is_power_of_two(), "width must be 2^n, was {w}");
@@ -237,11 +237,11 @@ impl SamplerRepeatPot {
237237
///
238238
/// Uses nearest neighbor sampling.
239239
#[inline]
240-
pub fn sample<C: Copy>(
240+
pub fn sample<D: AsSlice2<Elem: Copy>>(
241241
&self,
242-
tex: &Texture<impl AsSlice2<C>>,
242+
tex: &Texture<D>,
243243
tc: TexCoord,
244-
) -> C {
244+
) -> D::Elem {
245245
let scaled_uv = uv(tex.width() * tc.u(), tex.height() * tc.v());
246246
self.sample_abs(tex, scaled_uv)
247247
}
@@ -252,11 +252,11 @@ impl SamplerRepeatPot {
252252
///
253253
/// Uses nearest neighbor sampling.
254254
#[inline]
255-
pub fn sample_abs<C: Copy>(
255+
pub fn sample_abs<D: AsSlice2<Elem: Copy>>(
256256
&self,
257-
tex: &Texture<impl AsSlice2<C>>,
257+
tex: &Texture<D>,
258258
tc: TexCoord,
259-
) -> C {
259+
) -> D::Elem {
260260
use crate::math::float::f32;
261261
// Convert first to signed int to avoid clamping to zero
262262
let u = f32::floor(tc.u()) as i32 as u32 & self.w_mask;
@@ -277,11 +277,11 @@ impl SamplerClamp {
277277
///
278278
/// Uses nearest neighbor sampling.
279279
#[inline]
280-
pub fn sample<C: Copy>(
280+
pub fn sample<D: AsSlice2<Elem: Copy>>(
281281
&self,
282-
tex: &Texture<impl AsSlice2<C>>,
282+
tex: &Texture<D>,
283283
tc: TexCoord,
284-
) -> C {
284+
) -> D::Elem {
285285
self.sample_abs(tex, uv(tc.u() * tex.w, tc.v() * tex.h))
286286
}
287287

@@ -291,11 +291,11 @@ impl SamplerClamp {
291291
///
292292
/// Uses nearest neighbor sampling.
293293
#[inline]
294-
pub fn sample_abs<C: Copy>(
294+
pub fn sample_abs<D: AsSlice2<Elem: Copy>>(
295295
&self,
296-
tex: &Texture<impl AsSlice2<C>>,
296+
tex: &Texture<D>,
297297
tc: TexCoord,
298-
) -> C {
298+
) -> D::Elem {
299299
use crate::math::float::f32;
300300
let u = f32::floor(tc.u().clamp(0.0, tex.w - 1.0)) as u32;
301301
let v = f32::floor(tc.v().clamp(0.0, tex.h - 1.0)) as u32;
@@ -322,11 +322,11 @@ impl SamplerOnce {
322322
/// # Panics
323323
/// May panic if `tc` is not in the valid range.
324324
#[inline]
325-
pub fn sample<C: Copy>(
325+
pub fn sample<D: AsSlice2<Elem: Copy>>(
326326
&self,
327-
tex: &Texture<impl AsSlice2<C>>,
327+
tex: &Texture<D>,
328328
tc: TexCoord,
329-
) -> C {
329+
) -> D::Elem {
330330
let scaled_uv = uv(tex.width() * tc.u(), tex.height() * tc.v());
331331
self.sample_abs(tex, scaled_uv)
332332
}
@@ -340,11 +340,11 @@ impl SamplerOnce {
340340
/// # Panics
341341
/// May panic if `tc` is not in the valid range.
342342
#[inline]
343-
pub fn sample_abs<C: Copy>(
343+
pub fn sample_abs<D: AsSlice2<Elem: Copy>>(
344344
&self,
345-
tex: &Texture<impl AsSlice2<C>>,
345+
tex: &Texture<D>,
346346
tc: TexCoord,
347-
) -> C {
347+
) -> D::Elem {
348348
let u = tc.u() as u32;
349349
let v = tc.v() as u32;
350350

core/src/util/buf.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@ use inner::Inner;
1818
//
1919

2020
/// A trait for types that can provide a view of their data as a [`Slice2`].
21-
pub trait AsSlice2<T> {
21+
pub trait AsSlice2 {
22+
type Elem;
2223
/// Returns a borrowed `Slice2` view of `Self`.
23-
fn as_slice2(&self) -> Slice2<'_, T>;
24+
fn as_slice2(&self) -> Slice2<'_, Self::Elem>;
2425
}
2526

2627
/// A trait for types that can provide a mutable view of their data
2728
/// as a [`MutSlice2`].
28-
pub trait AsMutSlice2<T> {
29+
pub trait AsMutSlice2 {
30+
type Elem;
2931
/// Returns a mutably borrowed `MutSlice2` view of `Self`.
30-
fn as_mut_slice2(&mut self) -> MutSlice2<'_, T>;
32+
fn as_mut_slice2(&mut self) -> MutSlice2<'_, Self::Elem>;
3133
}
3234

3335
//
@@ -266,44 +268,51 @@ impl<'a, T> MutSlice2<'a, T> {
266268
// Local trait impls
267269
//
268270

269-
impl<T> AsSlice2<T> for Buf2<T> {
271+
impl<T> AsSlice2 for Buf2<T> {
272+
type Elem = T;
270273
#[inline]
271274
fn as_slice2(&self) -> Slice2<'_, T> {
272275
self.0.as_slice2()
273276
}
274277
}
275-
impl<T> AsSlice2<T> for &Buf2<T> {
278+
impl<T> AsSlice2 for &Buf2<T> {
279+
type Elem = T;
276280
#[inline]
277281
fn as_slice2(&self) -> Slice2<'_, T> {
278282
self.0.as_slice2()
279283
}
280284
}
281-
impl<T> AsSlice2<T> for Slice2<'_, T> {
285+
impl<T> AsSlice2 for Slice2<'_, T> {
286+
type Elem = T;
282287
#[inline]
283288
fn as_slice2(&self) -> Slice2<'_, T> {
284289
self.0.as_slice2()
285290
}
286291
}
287-
impl<T> AsSlice2<T> for MutSlice2<'_, T> {
292+
impl<T> AsSlice2 for MutSlice2<'_, T> {
293+
type Elem = T;
288294
#[inline]
289295
fn as_slice2(&self) -> Slice2<'_, T> {
290296
self.0.as_slice2()
291297
}
292298
}
293299

294-
impl<T> AsMutSlice2<T> for Buf2<T> {
300+
impl<T> AsMutSlice2 for Buf2<T> {
301+
type Elem = T;
295302
#[inline]
296303
fn as_mut_slice2(&mut self) -> MutSlice2<'_, T> {
297304
self.0.as_mut_slice2()
298305
}
299306
}
300-
impl<T> AsMutSlice2<T> for &mut Buf2<T> {
307+
impl<T> AsMutSlice2 for &mut Buf2<T> {
308+
type Elem = T;
301309
#[inline]
302310
fn as_mut_slice2(&mut self) -> MutSlice2<'_, T> {
303311
self.0.as_mut_slice2()
304312
}
305313
}
306-
impl<T> AsMutSlice2<T> for MutSlice2<'_, T> {
314+
impl<T> AsMutSlice2 for MutSlice2<'_, T> {
315+
type Elem = T;
307316
#[inline]
308317
fn as_mut_slice2(&mut self) -> MutSlice2<'_, T> {
309318
self.0.as_mut_slice2()
@@ -637,7 +646,7 @@ pub mod inner {
637646
/// # Panics
638647
/// if the dimensions of `self` and `other` do not match.
639648
#[doc(alias = "blit")]
640-
pub fn copy_from(&mut self, other: impl AsSlice2<T>)
649+
pub fn copy_from(&mut self, other: impl AsSlice2<Elem = T>)
641650
where
642651
T: Copy,
643652
{
@@ -1060,7 +1069,7 @@ mod tests {
10601069

10611070
#[test]
10621071
fn buf_ref_as_slice_mut() {
1063-
fn foo<T: AsMutSlice2<u32>>(mut buf: T) {
1072+
fn foo<T: AsMutSlice2<Elem = u32>>(mut buf: T) {
10641073
buf.as_mut_slice2()[[1, 1]] = 42;
10651074
}
10661075
let mut buf = Buf2::new((2, 2));

core/src/util/pnm.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,10 @@ pub fn parse_pnm(input: impl IntoIterator<Item = u8>) -> Result<Buf2<Color3>> {
278278
/// # Errors
279279
/// Returns [`std::io::Error`] if an error occurs while writing.
280280
#[cfg(feature = "std")]
281-
pub fn save_ppm<T>(
282-
path: impl AsRef<Path>,
283-
data: impl AsSlice2<T>,
284-
) -> io::Result<()>
281+
pub fn save_ppm<D>(path: impl AsRef<Path>, data: D) -> io::Result<()>
285282
where
286-
T: IntoPixel<[u8; 3], Rgb888> + Copy,
283+
D: AsSlice2,
284+
D::Elem: IntoPixel<[u8; 3], Rgb888> + Copy,
287285
{
288286
let out = BufWriter::new(File::create(path)?);
289287
write_ppm(out, data)
@@ -295,12 +293,10 @@ where
295293
/// # Errors
296294
/// Returns [`std::io::Error`] if an error occurs while writing.
297295
#[cfg(feature = "std")]
298-
pub fn write_ppm<T>(
299-
mut out: impl io::Write,
300-
data: impl AsSlice2<T>,
301-
) -> io::Result<()>
296+
pub fn write_ppm<D>(mut out: impl io::Write, data: D) -> io::Result<()>
302297
where
303-
T: IntoPixel<[u8; 3], Rgb888> + Copy,
298+
D: AsSlice2,
299+
D::Elem: IntoPixel<[u8; 3], Rgb888> + Copy,
304300
{
305301
let slice = data.as_slice2();
306302
Header {

front/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,19 @@ pub mod dims {
8989
pub const UWQHD_3440_1440: Dims = (3440, 1440);
9090
}
9191

92-
impl<W, C: AsMutSlice2<u32>, F: Copy, Z: AsMutSlice2<f32>>
93-
Frame<'_, W, &RefCell<Framebuf<Colorbuf<C, F>, Z>>>
92+
impl<Win, Fmt, Cbuf, Zbuf>
93+
Frame<'_, Win, &RefCell<Framebuf<Colorbuf<Cbuf, Fmt>, Zbuf>>>
9494
where
95-
Color4: IntoPixel<u32, F>,
95+
Fmt: Copy,
96+
Cbuf: AsMutSlice2<Elem: Clone>,
97+
Zbuf: AsMutSlice2<Elem = f32>,
98+
Color4: IntoPixel<Cbuf::Elem, Fmt>,
9699
{
97100
/// Clears the color buffer if [color clearing][Context::color_clear]
98101
/// is enabled and the depth buffer if [depth clearing][Context::depth_clear]
99102
/// is enabled.
100103
pub fn clear(&mut self) {
101104
if let Some(c) = self.ctx.color_clear {
102-
// TODO Assumes pixel format
103105
self.buf
104106
.borrow_mut()
105107
.color_buf

0 commit comments

Comments
 (0)