From 9090fa6a22afcc41d191ad35947ab312bf95918e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Hlusi=C4=8Dka?= Date: Wed, 28 Oct 2020 02:36:49 +0100 Subject: [PATCH 1/2] Add conversion functions to Size and Vector --- core/src/size.rs | 10 ++++++++++ core/src/vector.rs | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/core/src/size.rs b/core/src/size.rs index 7c481935..060f8fa5 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -1,3 +1,4 @@ +use super::vector::Vector; use std::f32; /// An amount of space in 2 dimensions. @@ -57,6 +58,15 @@ impl From<[u16; 2]> for Size { } } +impl From> for Size { + fn from(vector: Vector) -> Self { + Size { + width: vector.x, + height: vector.y, + } + } +} + impl From for [f32; 2] { fn from(size: Size) -> [f32; 2] { [size.width, size.height] diff --git a/core/src/vector.rs b/core/src/vector.rs index def3f8c0..fd00b5ba 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -1,3 +1,5 @@ +use super::size::Size; + /// A 2D vector. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Vector { @@ -65,3 +67,24 @@ where } } } + +impl From<[T; 2]> for Vector { + fn from([x, y]: [T; 2]) -> Self { + Self::new(x, y) + } +} + +impl From> for [T; 2] +where + T: Copy, +{ + fn from(other: Vector) -> Self { + [other.x, other.y] + } +} + +impl From for Vector { + fn from(size: Size) -> Self { + Vector::new(size.width, size.height) + } +} From 16646ffc4172ffb7b1a6ad0f50afe826ddcf4094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Hlusi=C4=8Dka?= Date: Thu, 29 Oct 2020 14:50:17 +0100 Subject: [PATCH 2/2] Apply suggestions --- core/src/size.rs | 8 +++++++- core/src/vector.rs | 8 -------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/core/src/size.rs b/core/src/size.rs index 060f8fa5..5c130978 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -1,4 +1,4 @@ -use super::vector::Vector; +use crate::Vector; use std::f32; /// An amount of space in 2 dimensions. @@ -72,3 +72,9 @@ impl From for [f32; 2] { [size.width, size.height] } } + +impl From for Vector { + fn from(size: Size) -> Self { + Vector::new(size.width, size.height) + } +} diff --git a/core/src/vector.rs b/core/src/vector.rs index fd00b5ba..e31924e7 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -1,5 +1,3 @@ -use super::size::Size; - /// A 2D vector. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Vector { @@ -82,9 +80,3 @@ where [other.x, other.y] } } - -impl From for Vector { - fn from(size: Size) -> Self { - Vector::new(size.width, size.height) - } -}