From b9584d5597acbf2effab4c70b84373033b2432bb Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Fri, 20 Dec 2024 10:50:17 +0100 Subject: [PATCH] added multiplication for Kartesians some part of it had to be an new trait --- .../kartesian/external_traits.rs | 10 +++++++ src/coordinate_systems/kartesian/traits.rs | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/coordinate_systems/kartesian/external_traits.rs b/src/coordinate_systems/kartesian/external_traits.rs index 9db8a3f..5ef24dc 100644 --- a/src/coordinate_systems/kartesian/external_traits.rs +++ b/src/coordinate_systems/kartesian/external_traits.rs @@ -149,3 +149,13 @@ impl Display for Velocity { Ok(()) } } + +impl Mul for Kartesian { + type Output = Kartesian; + fn mul(self, rhs: Self) -> Self::Output { + Kartesian { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} diff --git a/src/coordinate_systems/kartesian/traits.rs b/src/coordinate_systems/kartesian/traits.rs index de7b0ba..81b7a19 100644 --- a/src/coordinate_systems/kartesian/traits.rs +++ b/src/coordinate_systems/kartesian/traits.rs @@ -67,3 +67,29 @@ impl ToCoords for Kartesian { self } } + +pub trait IntMul { + type Output; + fn mul(self, rhs: T) -> Self::Output; +} + +macro_rules! impl_mul { + ($subtype:ident) => { + impl IntMul<$subtype> for Kartesian<$subtype> { + type Output = Kartesian<$subtype>; + fn mul(self, rhs: $subtype) -> Self::Output { + Kartesian { + x: self.x * rhs, + y: self.y * rhs, + } + } + } + }; +} + +impl_mul!(u8); +impl_mul!(u16); +impl_mul!(u32); +impl_mul!(u64); +impl_mul!(u128); +impl_mul!(usize);