diff --git a/dpi/src/lib.rs b/dpi/src/lib.rs index f1054b0cd6..f3759a8c2e 100644 --- a/dpi/src/lib.rs +++ b/dpi/src/lib.rs @@ -339,6 +339,48 @@ impl From> for PixelUnit { } } +macro_rules! vec2_from_impls { + ($t:ident, $a:ident, $b:ident, $mint_ty:ident) => { + impl From<(X, X)> for $t

{ + fn from(($a, $b): (X, X)) -> Self { + Self::new($a.cast(), $b.cast()) + } + } + + impl From<$t

> for (X, X) { + fn from(p: $t

) -> Self { + (p.$a.cast(), p.$b.cast()) + } + } + + impl From<[X; 2]> for $t

{ + fn from([$a, $b]: [X; 2]) -> Self { + Self::new($a.cast(), $b.cast()) + } + } + + impl From<$t

> for [X; 2] { + fn from(p: $t

) -> Self { + [p.$a.cast(), p.$b.cast()] + } + } + + #[cfg(feature = "mint")] + impl From> for $t

{ + fn from(p: mint::$mint_ty

) -> Self { + Self::new(p.x, p.y) + } + } + + #[cfg(feature = "mint")] + impl From<$t

> for mint::$mint_ty

{ + fn from(p: $t

) -> Self { + Self { x: p.$a, y: p.$b } + } + } + }; +} + /// A position represented in logical pixels. /// /// The position is stored as floats, so please be careful. Casting floats to integers truncates the @@ -381,43 +423,7 @@ impl LogicalPosition

{ } } -impl From<(X, X)> for LogicalPosition

{ - fn from((x, y): (X, X)) -> LogicalPosition

{ - LogicalPosition::new(x.cast(), y.cast()) - } -} - -impl From> for (X, X) { - fn from(p: LogicalPosition

) -> (X, X) { - (p.x.cast(), p.y.cast()) - } -} - -impl From<[X; 2]> for LogicalPosition

{ - fn from([x, y]: [X; 2]) -> LogicalPosition

{ - LogicalPosition::new(x.cast(), y.cast()) - } -} - -impl From> for [X; 2] { - fn from(p: LogicalPosition

) -> [X; 2] { - [p.x.cast(), p.y.cast()] - } -} - -#[cfg(feature = "mint")] -impl From> for LogicalPosition

{ - fn from(p: mint::Point2

) -> Self { - Self::new(p.x, p.y) - } -} - -#[cfg(feature = "mint")] -impl From> for mint::Point2

{ - fn from(p: LogicalPosition

) -> Self { - mint::Point2 { x: p.x, y: p.y } - } -} +vec2_from_impls!(LogicalPosition, x, y, Point2); /// A position represented in physical pixels. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)] @@ -457,43 +463,7 @@ impl PhysicalPosition

{ } } -impl From<(X, X)> for PhysicalPosition

{ - fn from((x, y): (X, X)) -> PhysicalPosition

{ - PhysicalPosition::new(x.cast(), y.cast()) - } -} - -impl From> for (X, X) { - fn from(p: PhysicalPosition

) -> (X, X) { - (p.x.cast(), p.y.cast()) - } -} - -impl From<[X; 2]> for PhysicalPosition

{ - fn from([x, y]: [X; 2]) -> PhysicalPosition

{ - PhysicalPosition::new(x.cast(), y.cast()) - } -} - -impl From> for [X; 2] { - fn from(p: PhysicalPosition

) -> [X; 2] { - [p.x.cast(), p.y.cast()] - } -} - -#[cfg(feature = "mint")] -impl From> for PhysicalPosition

{ - fn from(p: mint::Point2

) -> Self { - Self::new(p.x, p.y) - } -} - -#[cfg(feature = "mint")] -impl From> for mint::Point2

{ - fn from(p: PhysicalPosition

) -> Self { - mint::Point2 { x: p.x, y: p.y } - } -} +vec2_from_impls!(PhysicalPosition, x, y, Point2); /// A size represented in logical pixels. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)] @@ -533,43 +503,7 @@ impl LogicalSize

{ } } -impl From<(X, X)> for LogicalSize

{ - fn from((x, y): (X, X)) -> LogicalSize

{ - LogicalSize::new(x.cast(), y.cast()) - } -} - -impl From> for (X, X) { - fn from(s: LogicalSize

) -> (X, X) { - (s.width.cast(), s.height.cast()) - } -} - -impl From<[X; 2]> for LogicalSize

{ - fn from([x, y]: [X; 2]) -> LogicalSize

{ - LogicalSize::new(x.cast(), y.cast()) - } -} - -impl From> for [X; 2] { - fn from(s: LogicalSize

) -> [X; 2] { - [s.width.cast(), s.height.cast()] - } -} - -#[cfg(feature = "mint")] -impl From> for LogicalSize

{ - fn from(v: mint::Vector2

) -> Self { - Self::new(v.x, v.y) - } -} - -#[cfg(feature = "mint")] -impl From> for mint::Vector2

{ - fn from(s: LogicalSize

) -> Self { - mint::Vector2 { x: s.width, y: s.height } - } -} +vec2_from_impls!(LogicalSize, width, height, Vector2); /// A size represented in physical pixels. #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Default, Hash)] @@ -606,43 +540,7 @@ impl PhysicalSize

{ } } -impl From<(X, X)> for PhysicalSize

{ - fn from((x, y): (X, X)) -> PhysicalSize

{ - PhysicalSize::new(x.cast(), y.cast()) - } -} - -impl From> for (X, X) { - fn from(s: PhysicalSize

) -> (X, X) { - (s.width.cast(), s.height.cast()) - } -} - -impl From<[X; 2]> for PhysicalSize

{ - fn from([x, y]: [X; 2]) -> PhysicalSize

{ - PhysicalSize::new(x.cast(), y.cast()) - } -} - -impl From> for [X; 2] { - fn from(s: PhysicalSize

) -> [X; 2] { - [s.width.cast(), s.height.cast()] - } -} - -#[cfg(feature = "mint")] -impl From> for PhysicalSize

{ - fn from(v: mint::Vector2

) -> Self { - Self::new(v.x, v.y) - } -} - -#[cfg(feature = "mint")] -impl From> for mint::Vector2

{ - fn from(s: PhysicalSize

) -> Self { - mint::Vector2 { x: s.width, y: s.height } - } -} +vec2_from_impls!(PhysicalSize, width, height, Vector2); /// A size that's either physical or logical. #[derive(Debug, Copy, Clone, PartialEq)]