grid: additional impls of Coord2d

This commit is contained in:
Keenan Tims 2024-12-21 23:36:40 -08:00
parent 5d518248a8
commit 40d5e820bc
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -39,6 +39,16 @@ impl<T: AsCoord2d> Add<T> for &Coord2d {
} }
} }
impl<T: AsCoord2d> 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 { impl AsCoord2d for Coord2d {
fn to_coord(self) -> Coord2d { fn to_coord(self) -> Coord2d {
self self
@ -82,6 +92,25 @@ where
} }
} }
impl<T> AsCoord2d for &(T, T)
where
T: Copy + TryInto<i64>,
<T as TryInto<i64>>::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<Coord2d> for (i64, i64) { impl From<Coord2d> for (i64, i64) {
fn from(value: Coord2d) -> Self { fn from(value: Coord2d) -> Self {
(value.x, value.y) (value.x, value.y)