From 021d7f9d1f7460ecb2ca917d8d8633d0131f4f9d Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 10 Sep 2024 14:20:07 -0700 Subject: [PATCH] implement mul + div only for scalars (otherwise conflicting implementations) --- linear_ops/src/macros/vector.rs | 37 +++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/linear_ops/src/macros/vector.rs b/linear_ops/src/macros/vector.rs index 8d4c7de..3802f4a 100644 --- a/linear_ops/src/macros/vector.rs +++ b/linear_ops/src/macros/vector.rs @@ -127,13 +127,16 @@ macro_rules! impl_vector { $crate::impl_vector_operator!(Add, add ); $crate::impl_vector_assign_operator!(SubAssign, sub_assign ); $crate::impl_vector_operator!(Sub, sub ); - $crate::impl_vector_assign_operator!(MulAssign, mul_assign ); - $crate::impl_vector_operator!(Mul, mul ); - $crate::impl_vector_assign_operator!(DivAssign, div_assign ); - $crate::impl_vector_operator!(Div, div ); $crate::impl_vector_assign_operator!(RemAssign, rem_assign ); $crate::impl_vector_operator!(Rem, rem ); + // mul and div are special, usually you multiply by a scalar + // and implementing both vec*vec and vec*scalar is conflicting implementations Q_Q + $crate::impl_vector_assign_operator_scalar!(MulAssign, mul_assign ); + $crate::impl_vector_operator_scalar!(Mul, mul ); + $crate::impl_vector_assign_operator_scalar!(DivAssign, div_assign ); + $crate::impl_vector_operator_scalar!(Div, div ); + // Impl bitwise operators $crate::impl_vector_assign_operator!(BitAndAssign, bitand_assign ); $crate::impl_vector_operator!(BitAnd, bitand ); @@ -151,6 +154,19 @@ macro_rules! impl_vector { } #[doc(hidden)] #[macro_export(local_inner_macros)] +macro_rules! impl_vector_operator_scalar { + ($trait: ident, $method: ident ) => { + impl,U:Copy,V> core::ops::$trait for Vector{ + type Output=Vector; + #[inline] + fn $method(self,rhs:U)->Self::Output{ + self.map(|t|t.$method(rhs)) + } + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] macro_rules! impl_vector_operator { ($trait: ident, $method: ident ) => { impl,U,V> core::ops::$trait> for Vector{ @@ -171,6 +187,19 @@ macro_rules! impl_vector_operator { } #[doc(hidden)] #[macro_export(local_inner_macros)] +macro_rules! impl_vector_assign_operator_scalar { + ($trait: ident, $method: ident ) => { + impl,U:Copy> core::ops::$trait for Vector{ + #[inline] + fn $method(&mut self,rhs:U){ + self.array.iter_mut() + .for_each(|t|t.$method(rhs)) + } + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] macro_rules! impl_vector_assign_operator { ($trait: ident, $method: ident ) => { impl,U> core::ops::$trait> for Vector{