From 28a20aec102bd33339a10a7047e1ac18f4c8d514 Mon Sep 17 00:00:00 2001 From: Antonino Siena Date: Tue, 26 Nov 2019 23:56:47 +0100 Subject: [PATCH] Dpi Type conversions into/from arrays (#1283) * Added array conversion methods * Cargo fmt * Undo wrong fmt --- src/dpi.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/dpi.rs b/src/dpi.rs index a1a0491c..664b22cb 100644 --- a/src/dpi.rs +++ b/src/dpi.rs @@ -191,6 +191,18 @@ impl Into<(X, X)> for LogicalPosition

{ } } +impl From<[X; 2]> for LogicalPosition

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

{ + LogicalPosition::new(x.cast(), y.cast()) + } +} + +impl Into<[X; 2]> for LogicalPosition

{ + fn into(self: Self) -> [X; 2] { + [self.x.cast(), self.y.cast()] + } +} + /// A position represented in physical pixels. /// /// The position is stored as floats, so please be careful. Casting floats to integers truncates the fractional part, @@ -248,6 +260,18 @@ impl Into<(X, X)> for PhysicalPosition

{ } } +impl From<[X; 2]> for PhysicalPosition

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

{ + PhysicalPosition::new(x.cast(), y.cast()) + } +} + +impl Into<[X; 2]> for PhysicalPosition

{ + fn into(self: Self) -> [X; 2] { + [self.x.cast(), self.y.cast()] + } +} + /// A size represented in logical pixels. /// /// The size is stored as floats, so please be careful. Casting floats to integers truncates the fractional part, @@ -302,6 +326,18 @@ impl Into<(X, X)> for LogicalSize

{ } } +impl From<[X; 2]> for LogicalSize

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

{ + LogicalSize::new(x.cast(), y.cast()) + } +} + +impl Into<[X; 2]> for LogicalSize

{ + fn into(self: Self) -> [X; 2] { + [self.width.cast(), self.height.cast()] + } +} + /// A size represented in physical pixels. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] @@ -352,6 +388,18 @@ impl Into<(X, X)> for PhysicalSize

{ } } +impl From<[X; 2]> for PhysicalSize

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

{ + PhysicalSize::new(x.cast(), y.cast()) + } +} + +impl Into<[X; 2]> for PhysicalSize

{ + fn into(self: Self) -> [X; 2] { + [self.width.cast(), self.height.cast()] + } +} + #[derive(Debug, Copy, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Size {