From 40d5e820bc3c6fe5d99689f8725902edbbc98e0d Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Sat, 21 Dec 2024 23:36:40 -0800 Subject: [PATCH] grid: additional impls of Coord2d --- utils/grid/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/utils/grid/lib.rs b/utils/grid/lib.rs index ff75b74..6fe07dc 100644 --- a/utils/grid/lib.rs +++ b/utils/grid/lib.rs @@ -39,6 +39,16 @@ impl Add for &Coord2d { } } +impl Add<&T> for Coord2d { + type Output = Coord2d; + fn add(self, rhs: &T) -> Self::Output { + Coord2d { + x: self.x() + rhs.x(), + y: self.y() + rhs.y(), + } + } +} + impl AsCoord2d for Coord2d { fn to_coord(self) -> Coord2d { self @@ -82,6 +92,25 @@ where } } +impl AsCoord2d for &(T, T) +where + T: Copy + TryInto, + >::Error: Debug, +{ + fn to_coord(self) -> Coord2d { + Coord2d { + x: self.0.try_into().unwrap(), + y: self.1.try_into().unwrap(), + } + } + fn x(&self) -> i64 { + self.0.try_into().unwrap() + } + fn y(&self) -> i64 { + self.1.try_into().unwrap() + } +} + impl From for (i64, i64) { fn from(value: Coord2d) -> Self { (value.x, value.y)