Skip to content

Commit

Permalink
Explore implementations of (&+_), (_+&) and (&+&)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacg committed May 25, 2022
1 parent b7906fd commit 891aa9e
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<D, Ul, Ur, V> $crate::lib::ops::$AddSubTrait<Quantity<D, Ur, V>>
for Quantity<D, Ul, V>
Expand Down Expand Up @@ -550,6 +558,76 @@ macro_rules! system {
}
}

// 2. impl<'b> Op< Quantity<...>> for &'b Quantity<..>
autoconvert! {
impl<D, Ul, Ur, V> $crate::lib::ops::$AddSubTrait<Quantity<D, Ur, V>>
for &Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
D::Kind: $crate::marker::$AddSubTrait,
Ul: Units<V> + ?Sized,
Ur: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V> + Copy,
{
type Output = Quantity<D, Ul, V>;

#[inline(always)]
fn $addsub_fun(self, rhs: Quantity<D, Ur, V>) -> Self::Output {
Quantity {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: self.value $addsub_op change_base::<D, Ul, Ur, V>(&rhs.value),
}
}
}}

// 3. impl<'a> Op<&'a Quantity<...>> for Quantity<..>
autoconvert! {
impl<D, Ul, Ur, V> $crate::lib::ops::$AddSubTrait<&Quantity<D, Ur, V>>
for Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
D::Kind: $crate::marker::$AddSubTrait,
Ul: Units<V> + ?Sized,
Ur: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V>,
{
type Output = Quantity<D, Ul, V>;

#[inline(always)]
fn $addsub_fun(self, rhs: &Quantity<D, Ur, V>) -> Self::Output {
Quantity {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: self.value $addsub_op change_base::<D, Ul, Ur, V>(&rhs.value),
}
}
}}

// 4. impl<'a, 'b> Op<&'a Quantity<...>> for &'b Quantity<..>
autoconvert! {
impl<D, Ul, Ur, V> $crate::lib::ops::$AddSubTrait<&Quantity<D, Ur, V>>
for &Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
D::Kind: $crate::marker::$AddSubTrait,
Ul: Units<V> + ?Sized,
Ur: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V> + Copy,
{
type Output = Quantity<D, Ul, V>;

#[inline(always)]
fn $addsub_fun(self, rhs: &Quantity<D, Ur, V>) -> Self::Output {
Quantity {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: self.value $addsub_op change_base::<D, Ul, Ur, V>(&rhs.value),
}
}
}}


#[doc(hidden)]
mod $Mod {
storage_types! {
Expand Down

0 comments on commit 891aa9e

Please sign in to comment.