From 891aa9efff6d2359dadfb418a7224e76b1e14693 Mon Sep 17 00:00:00 2001 From: Jacek Generowicz Date: Wed, 25 May 2022 17:34:45 +0200 Subject: [PATCH] Explore implementations of (&+_), (_+&) and (&+&) --- src/system.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/system.rs b/src/system.rs index 016f714c..c5e57755 100644 --- a/src/system.rs +++ b/src/system.rs @@ -388,6 +388,14 @@ macro_rules! system { $MulDivAssignTrait:ident, $muldivassign_fun:ident, $muldivassign_op:tt, $Mod:ident ) => { + // The following implementations come in 4 varieties: + // 1. impl Op< Quantity<...>> for Quantity<..> + // 2. impl<'b> Op< Quantity<...>> for &'b Quantity<..> + // 3. impl<'a> Op<&'a Quantity<...>> for Quantity<..> + // 4. impl<'a, 'b> Op<&'a Quantity<...>> for &'b Quantity<..> + + + // 1. impl Op< Quantity<...>> for Quantity<..> autoconvert! { impl $crate::lib::ops::$AddSubTrait> for Quantity @@ -550,6 +558,76 @@ macro_rules! system { } } + // 2. impl<'b> Op< Quantity<...>> for &'b Quantity<..> + autoconvert! { + impl $crate::lib::ops::$AddSubTrait> + for &Quantity + where + D: Dimension + ?Sized, + D::Kind: $crate::marker::$AddSubTrait, + Ul: Units + ?Sized, + Ur: Units + ?Sized, + V: $crate::num::Num + $crate::Conversion + Copy, + { + type Output = Quantity; + + #[inline(always)] + fn $addsub_fun(self, rhs: Quantity) -> Self::Output { + Quantity { + dimension: $crate::lib::marker::PhantomData, + units: $crate::lib::marker::PhantomData, + value: self.value $addsub_op change_base::(&rhs.value), + } + } + }} + + // 3. impl<'a> Op<&'a Quantity<...>> for Quantity<..> + autoconvert! { + impl $crate::lib::ops::$AddSubTrait<&Quantity> + for Quantity + where + D: Dimension + ?Sized, + D::Kind: $crate::marker::$AddSubTrait, + Ul: Units + ?Sized, + Ur: Units + ?Sized, + V: $crate::num::Num + $crate::Conversion, + { + type Output = Quantity; + + #[inline(always)] + fn $addsub_fun(self, rhs: &Quantity) -> Self::Output { + Quantity { + dimension: $crate::lib::marker::PhantomData, + units: $crate::lib::marker::PhantomData, + value: self.value $addsub_op change_base::(&rhs.value), + } + } + }} + + // 4. impl<'a, 'b> Op<&'a Quantity<...>> for &'b Quantity<..> + autoconvert! { + impl $crate::lib::ops::$AddSubTrait<&Quantity> + for &Quantity + where + D: Dimension + ?Sized, + D::Kind: $crate::marker::$AddSubTrait, + Ul: Units + ?Sized, + Ur: Units + ?Sized, + V: $crate::num::Num + $crate::Conversion + Copy, + { + type Output = Quantity; + + #[inline(always)] + fn $addsub_fun(self, rhs: &Quantity) -> Self::Output { + Quantity { + dimension: $crate::lib::marker::PhantomData, + units: $crate::lib::marker::PhantomData, + value: self.value $addsub_op change_base::(&rhs.value), + } + } + }} + + #[doc(hidden)] mod $Mod { storage_types! {