@@ -84,36 +84,18 @@ pub trait Pixel: Copy + Into<f64> {
8484 }
8585}
8686
87- impl Pixel for u8 {
88- fn from_f64 ( f : f64 ) -> Self {
89- round ( f) as u8
90- }
91- }
92- impl Pixel for u16 {
93- fn from_f64 ( f : f64 ) -> Self {
94- round ( f) as u16
95- }
96- }
97- impl Pixel for u32 {
98- fn from_f64 ( f : f64 ) -> Self {
99- round ( f) as u32
100- }
101- }
102- impl Pixel for i8 {
103- fn from_f64 ( f : f64 ) -> Self {
104- round ( f) as i8
105- }
106- }
107- impl Pixel for i16 {
108- fn from_f64 ( f : f64 ) -> Self {
109- round ( f) as i16
110- }
111- }
112- impl Pixel for i32 {
113- fn from_f64 ( f : f64 ) -> Self {
114- round ( f) as i32
115- }
87+ macro_rules! pixel_int_impl {
88+ ( $( $t: ty) ,* ) => { $(
89+ impl Pixel for $t {
90+ fn from_f64( f: f64 ) -> Self {
91+ round( f) as $t
92+ }
93+ }
94+ ) * }
11695}
96+
97+ pixel_int_impl ! ( u8 , u16 , u32 , i8 , i16 , i32 ) ;
98+
11799impl Pixel for f32 {
118100 fn from_f64 ( f : f64 ) -> Self {
119101 f as f32
@@ -378,6 +360,48 @@ impl<P: Pixel> From<LogicalUnit<P>> for PixelUnit {
378360 }
379361}
380362
363+ macro_rules! vec2_from_impls {
364+ ( $t: ident, $a: ident, $b: ident, $mint_ty: ident) => {
365+ impl <P : Pixel , X : Pixel > From <( X , X ) > for $t<P > {
366+ fn from( ( $a, $b) : ( X , X ) ) -> Self {
367+ Self :: new( $a. cast( ) , $b. cast( ) )
368+ }
369+ }
370+
371+ impl <P : Pixel , X : Pixel > From <$t<P >> for ( X , X ) {
372+ fn from( p: $t<P >) -> Self {
373+ ( p. $a. cast( ) , p. $b. cast( ) )
374+ }
375+ }
376+
377+ impl <P : Pixel , X : Pixel > From <[ X ; 2 ] > for $t<P > {
378+ fn from( [ $a, $b] : [ X ; 2 ] ) -> Self {
379+ Self :: new( $a. cast( ) , $b. cast( ) )
380+ }
381+ }
382+
383+ impl <P : Pixel , X : Pixel > From <$t<P >> for [ X ; 2 ] {
384+ fn from( p: $t<P >) -> Self {
385+ [ p. $a. cast( ) , p. $b. cast( ) ]
386+ }
387+ }
388+
389+ #[ cfg( feature = "mint" ) ]
390+ impl <P : Pixel > From <mint:: $mint_ty<P >> for $t<P > {
391+ fn from( p: mint:: $mint_ty<P >) -> Self {
392+ Self :: new( p. x, p. y)
393+ }
394+ }
395+
396+ #[ cfg( feature = "mint" ) ]
397+ impl <P : Pixel > From <$t<P >> for mint:: $mint_ty<P > {
398+ fn from( p: $t<P >) -> Self {
399+ Self { x: p. $a, y: p. $b }
400+ }
401+ }
402+ } ;
403+ }
404+
381405/// A position represented in logical pixels.
382406///
383407/// The position is stored as floats, so please be careful. Casting floats to integers truncates the
@@ -420,43 +444,7 @@ impl<P: Pixel> LogicalPosition<P> {
420444 }
421445}
422446
423- impl < P : Pixel , X : Pixel > From < ( X , X ) > for LogicalPosition < P > {
424- fn from ( ( x, y) : ( X , X ) ) -> LogicalPosition < P > {
425- LogicalPosition :: new ( x. cast ( ) , y. cast ( ) )
426- }
427- }
428-
429- impl < P : Pixel , X : Pixel > From < LogicalPosition < P > > for ( X , X ) {
430- fn from ( p : LogicalPosition < P > ) -> ( X , X ) {
431- ( p. x . cast ( ) , p. y . cast ( ) )
432- }
433- }
434-
435- impl < P : Pixel , X : Pixel > From < [ X ; 2 ] > for LogicalPosition < P > {
436- fn from ( [ x, y] : [ X ; 2 ] ) -> LogicalPosition < P > {
437- LogicalPosition :: new ( x. cast ( ) , y. cast ( ) )
438- }
439- }
440-
441- impl < P : Pixel , X : Pixel > From < LogicalPosition < P > > for [ X ; 2 ] {
442- fn from ( p : LogicalPosition < P > ) -> [ X ; 2 ] {
443- [ p. x . cast ( ) , p. y . cast ( ) ]
444- }
445- }
446-
447- #[ cfg( feature = "mint" ) ]
448- impl < P : Pixel > From < mint:: Point2 < P > > for LogicalPosition < P > {
449- fn from ( p : mint:: Point2 < P > ) -> Self {
450- Self :: new ( p. x , p. y )
451- }
452- }
453-
454- #[ cfg( feature = "mint" ) ]
455- impl < P : Pixel > From < LogicalPosition < P > > for mint:: Point2 < P > {
456- fn from ( p : LogicalPosition < P > ) -> Self {
457- mint:: Point2 { x : p. x , y : p. y }
458- }
459- }
447+ vec2_from_impls ! ( LogicalPosition , x, y, Point2 ) ;
460448
461449/// A position represented in physical pixels.
462450#[ derive( Debug , Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Default , Hash ) ]
@@ -496,43 +484,7 @@ impl<P: Pixel> PhysicalPosition<P> {
496484 }
497485}
498486
499- impl < P : Pixel , X : Pixel > From < ( X , X ) > for PhysicalPosition < P > {
500- fn from ( ( x, y) : ( X , X ) ) -> PhysicalPosition < P > {
501- PhysicalPosition :: new ( x. cast ( ) , y. cast ( ) )
502- }
503- }
504-
505- impl < P : Pixel , X : Pixel > From < PhysicalPosition < P > > for ( X , X ) {
506- fn from ( p : PhysicalPosition < P > ) -> ( X , X ) {
507- ( p. x . cast ( ) , p. y . cast ( ) )
508- }
509- }
510-
511- impl < P : Pixel , X : Pixel > From < [ X ; 2 ] > for PhysicalPosition < P > {
512- fn from ( [ x, y] : [ X ; 2 ] ) -> PhysicalPosition < P > {
513- PhysicalPosition :: new ( x. cast ( ) , y. cast ( ) )
514- }
515- }
516-
517- impl < P : Pixel , X : Pixel > From < PhysicalPosition < P > > for [ X ; 2 ] {
518- fn from ( p : PhysicalPosition < P > ) -> [ X ; 2 ] {
519- [ p. x . cast ( ) , p. y . cast ( ) ]
520- }
521- }
522-
523- #[ cfg( feature = "mint" ) ]
524- impl < P : Pixel > From < mint:: Point2 < P > > for PhysicalPosition < P > {
525- fn from ( p : mint:: Point2 < P > ) -> Self {
526- Self :: new ( p. x , p. y )
527- }
528- }
529-
530- #[ cfg( feature = "mint" ) ]
531- impl < P : Pixel > From < PhysicalPosition < P > > for mint:: Point2 < P > {
532- fn from ( p : PhysicalPosition < P > ) -> Self {
533- mint:: Point2 { x : p. x , y : p. y }
534- }
535- }
487+ vec2_from_impls ! ( PhysicalPosition , x, y, Point2 ) ;
536488
537489/// A size represented in logical pixels.
538490#[ derive( Debug , Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Default , Hash ) ]
@@ -572,43 +524,7 @@ impl<P: Pixel> LogicalSize<P> {
572524 }
573525}
574526
575- impl < P : Pixel , X : Pixel > From < ( X , X ) > for LogicalSize < P > {
576- fn from ( ( x, y) : ( X , X ) ) -> LogicalSize < P > {
577- LogicalSize :: new ( x. cast ( ) , y. cast ( ) )
578- }
579- }
580-
581- impl < P : Pixel , X : Pixel > From < LogicalSize < P > > for ( X , X ) {
582- fn from ( s : LogicalSize < P > ) -> ( X , X ) {
583- ( s. width . cast ( ) , s. height . cast ( ) )
584- }
585- }
586-
587- impl < P : Pixel , X : Pixel > From < [ X ; 2 ] > for LogicalSize < P > {
588- fn from ( [ x, y] : [ X ; 2 ] ) -> LogicalSize < P > {
589- LogicalSize :: new ( x. cast ( ) , y. cast ( ) )
590- }
591- }
592-
593- impl < P : Pixel , X : Pixel > From < LogicalSize < P > > for [ X ; 2 ] {
594- fn from ( s : LogicalSize < P > ) -> [ X ; 2 ] {
595- [ s. width . cast ( ) , s. height . cast ( ) ]
596- }
597- }
598-
599- #[ cfg( feature = "mint" ) ]
600- impl < P : Pixel > From < mint:: Vector2 < P > > for LogicalSize < P > {
601- fn from ( v : mint:: Vector2 < P > ) -> Self {
602- Self :: new ( v. x , v. y )
603- }
604- }
605-
606- #[ cfg( feature = "mint" ) ]
607- impl < P : Pixel > From < LogicalSize < P > > for mint:: Vector2 < P > {
608- fn from ( s : LogicalSize < P > ) -> Self {
609- mint:: Vector2 { x : s. width , y : s. height }
610- }
611- }
527+ vec2_from_impls ! ( LogicalSize , width, height, Vector2 ) ;
612528
613529/// A size represented in physical pixels.
614530#[ derive( Debug , Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Default , Hash ) ]
@@ -645,43 +561,7 @@ impl<P: Pixel> PhysicalSize<P> {
645561 }
646562}
647563
648- impl < P : Pixel , X : Pixel > From < ( X , X ) > for PhysicalSize < P > {
649- fn from ( ( x, y) : ( X , X ) ) -> PhysicalSize < P > {
650- PhysicalSize :: new ( x. cast ( ) , y. cast ( ) )
651- }
652- }
653-
654- impl < P : Pixel , X : Pixel > From < PhysicalSize < P > > for ( X , X ) {
655- fn from ( s : PhysicalSize < P > ) -> ( X , X ) {
656- ( s. width . cast ( ) , s. height . cast ( ) )
657- }
658- }
659-
660- impl < P : Pixel , X : Pixel > From < [ X ; 2 ] > for PhysicalSize < P > {
661- fn from ( [ x, y] : [ X ; 2 ] ) -> PhysicalSize < P > {
662- PhysicalSize :: new ( x. cast ( ) , y. cast ( ) )
663- }
664- }
665-
666- impl < P : Pixel , X : Pixel > From < PhysicalSize < P > > for [ X ; 2 ] {
667- fn from ( s : PhysicalSize < P > ) -> [ X ; 2 ] {
668- [ s. width . cast ( ) , s. height . cast ( ) ]
669- }
670- }
671-
672- #[ cfg( feature = "mint" ) ]
673- impl < P : Pixel > From < mint:: Vector2 < P > > for PhysicalSize < P > {
674- fn from ( v : mint:: Vector2 < P > ) -> Self {
675- Self :: new ( v. x , v. y )
676- }
677- }
678-
679- #[ cfg( feature = "mint" ) ]
680- impl < P : Pixel > From < PhysicalSize < P > > for mint:: Vector2 < P > {
681- fn from ( s : PhysicalSize < P > ) -> Self {
682- mint:: Vector2 { x : s. width , y : s. height }
683- }
684- }
564+ vec2_from_impls ! ( PhysicalSize , width, height, Vector2 ) ;
685565
686566/// A size that's either physical or logical.
687567#[ derive( Debug , Copy , Clone , PartialEq ) ]
0 commit comments