@@ -148,19 +148,18 @@ pub trait Pixel:
148148 /// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
149149 /// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
150150 ///
151- /// let f = |color: u8| {
152- /// u16::from(color) * 10
153- /// };
151+ /// let widen = |b: u8| u16::from(b) << 8 | u16::from(b);
154152 ///
155- /// assert_eq!(rgb.map(f ), Rgb { r: 0, g: 100, b: 1000 });
156- /// assert_eq!(rgba.map(f ), Rgba { r: 0, g: 100, b: 1000, a: 500 });
153+ /// assert_eq!(rgb.map(widen ), Rgb { r: 0, g: 100, b: 1000 });
154+ /// assert_eq!(rgba.map(widen ), Rgba { r: 0, g: 100, b: 1000, a: 500 });
157155 /// ```
158156 fn map < U > ( & self , f : impl FnMut ( Self :: Component ) -> U ) -> Self :: SelfType < U , U > where U : Copy ;
159157
160158 /// Maps each of the pixel's components with a function `f` to the same component type.
161159 ///
162- /// See [`Pixel::map()`] if you want to map the components to a
163- /// different type.
160+ /// Use [`Pixel::map()`] if you want to map the components to a
161+ /// different type. `map()` can also be used to keep the same component type,
162+ /// but due to limitations of Rust's type system,`map_same()` may be required in generic contexts.
164163 ///
165164 /// # Examples
166165 ///
@@ -170,12 +169,10 @@ pub trait Pixel:
170169 /// let rgb = Rgb { r: 0_u8, g: 10, b: 100 };
171170 /// let rgba = Rgba { r: 0_u8, g: 10, b: 100, a: 50 };
172171 ///
173- /// let f = |color: u8| {
174- /// color / 2
175- /// };
172+ /// let halved = |color: u8| color / 2;
176173 ///
177- /// assert_eq!(rgb.map_same(f ), Rgb { r: 0, g: 5, b: 50 });
178- /// assert_eq!(rgba.map_same(f ), Rgba { r: 0, g: 5, b: 50, a: 25 });
174+ /// assert_eq!(rgb.map_same(halved ), Rgb { r: 0, g: 5, b: 50 });
175+ /// assert_eq!(rgba.map_same(halved ), Rgba { r: 0, g: 5, b: 50, a: 25 });
179176 /// ```
180177 fn map_same ( & self , f : impl FnMut ( Self :: Component ) -> Self :: Component ) -> Self ;
181178}
@@ -299,19 +296,19 @@ with_alpha!(GrayAlpha_v08, 2, [0, 1]);
299296
300297#[ test]
301298fn as_refs ( ) {
302- let mut r = Rgba :: new ( 1u8 , 2 , 3 , 4u8 ) ;
303- assert_eq ! ( & [ 1 , 2 , 3 , 4 ] , r. as_array( ) ) ;
304- assert_eq ! ( & [ 1 , 2 , 3 , 4 ] , AsRef :: <[ u8 ; 4 ] >:: as_ref( & r) ) ;
305- assert_eq ! ( & [ 1 , 2 , 3 , 4 ] , r. as_ref( ) ) ;
306- assert_eq ! ( [ 1 , 2 , 3 , 4 ] , * r. as_array_mut( ) ) ;
307- assert_eq ! ( [ 1 , 2 , 3 , 4 ] , * AsMut :: <[ u8 ; 4 ] >:: as_mut( & mut r) ) ;
308- assert_eq ! ( [ 1 , 2 , 3 , 4 ] , * r. as_mut( ) ) ;
309-
310- let mut r = GrayA :: new ( 1u8 , 4u8 ) ;
311- assert_eq ! ( & [ 1 , 4 ] , r. as_array( ) ) ;
312- assert_eq ! ( & [ 1 , 4 ] , AsRef :: <[ u8 ; 2 ] >:: as_ref( & r) ) ;
313- assert_eq ! ( & [ 1 , 4 ] , r. as_ref( ) ) ;
314- assert_eq ! ( [ 1 , 4 ] , * r. as_array_mut( ) ) ;
315- assert_eq ! ( [ 1 , 4 ] , * AsMut :: <[ u8 ; 2 ] >:: as_mut( & mut r) ) ;
316- assert_eq ! ( [ 1 , 4 ] , * r. as_mut( ) ) ;
299+ let mut r = Rgba :: new ( 1_u8 , 2 , 3 , 4u8 ) ;
300+ assert_eq ! ( & [ 1 , 2 , 3 , 4 ] , r. as_array( ) ) ;
301+ assert_eq ! ( & [ 1 , 2 , 3 , 4 ] , AsRef :: <[ u8 ; 4 ] >:: as_ref( & r) ) ;
302+ assert_eq ! ( & [ 1 , 2 , 3 , 4 ] , r. as_ref( ) ) ;
303+ assert_eq ! ( [ 1 , 2 , 3 , 4 ] , * r. as_array_mut( ) ) ;
304+ assert_eq ! ( [ 1 , 2 , 3 , 4 ] , * AsMut :: <[ u8 ; 4 ] >:: as_mut( & mut r) ) ;
305+ assert_eq ! ( [ 1 , 2 , 3 , 4 ] , * r. as_mut( ) ) ;
306+
307+ let mut r = GrayA :: new ( 1_u8 , 4u8 ) ;
308+ assert_eq ! ( & [ 1 , 4 ] , r. as_array( ) ) ;
309+ assert_eq ! ( & [ 1 , 4 ] , AsRef :: <[ u8 ; 2 ] >:: as_ref( & r) ) ;
310+ assert_eq ! ( & [ 1 , 4 ] , r. as_ref( ) ) ;
311+ assert_eq ! ( [ 1 , 4 ] , * r. as_array_mut( ) ) ;
312+ assert_eq ! ( [ 1 , 4 ] , * AsMut :: <[ u8 ; 2 ] >:: as_mut( & mut r) ) ;
313+ assert_eq ! ( [ 1 , 4 ] , * r. as_mut( ) ) ;
317314}
0 commit comments