Skip to content

Commit

Permalink
Explicitly implement {Mul,Div}<Quantity> for V.
Browse files Browse the repository at this point in the history
Explicit implementations allow for a literal left hand side. Part of #29.
  • Loading branch information
iliekturtles committed Dec 18, 2017
1 parent 0c3395e commit c754fa9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/si/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ mod tests {

#[test]
fn check_dimension() {
let _: Time<V> = Frequency::new::<f::hertz>(V::one()).recip();
let _: Frequency<V> = Time::new::<t::second>(V::one()).recip();
let _: Time<V> = V::one() / Frequency::new::<f::hertz>(V::one());
let _: Frequency<V> = V::one() / Time::new::<t::second>(V::one());
}

#[test]
Expand Down Expand Up @@ -80,10 +80,10 @@ mod tests {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<T: t::Conversion<V>, F: f::Conversion<V>>(t: T, f: F) {
assert_ulps_eq!((Time::new::<T>(V::one()).recip()).get(f),
assert_ulps_eq!((V::one() / Time::new::<T>(V::one())).get(f),
Frequency::new::<F>(V::one()).get(f), epsilon = V::epsilon());
assert_ulps_eq!(Time::new::<T>(V::one()).get(t),
(Frequency::new::<F>(V::one()).recip()).get(t), epsilon = V::epsilon());
(V::one() / Frequency::new::<F>(V::one())).get(t), epsilon = V::epsilon());
}
}
}
Expand Down
55 changes: 34 additions & 21 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ macro_rules! system {
$AddSubAssignTrait:ident, $addsubassign_fun:ident, $addsubassign_op:tt,
$AddSubAlias:ident,
$MulDivTrait:ident, $muldiv_fun:ident, $muldiv_op:tt,
$MulDivAssignTrait:ident, $muldivassign_fun:ident, $muldivassign_op:tt
$MulDivAssignTrait:ident, $muldivassign_fun:ident, $muldivassign_op:tt,
$Mod:ident
) => {
impl<D, Ul, Ur, V> $crate::stdlib::ops::$AddSubTrait<Quantity<D, Ur, V>>
for Quantity<D, Ul, V>
Expand Down Expand Up @@ -425,24 +426,6 @@ macro_rules! system {
}
}

// impl<D, U, V> $crate::stdlib::ops::$MulDivTrait<Quantity<D, U, V>> for V
// where
// D: Dimension + ?Sized,
// U: Units<V> + ?Sized,
// V: $crate::num::Num + $crate::stdlib::ops::$MulDivTrait<V>,
// {
// type Output = Quantity<D, U, V>;

// #[inline(always)]
// fn $muldiv_fun(self, rhs: Quantity<D, U, V>) -> Self::Output {
// Quantity {
// dimension: $crate::stdlib::marker::PhantomData,
// units: $crate::stdlib::marker::PhantomData,
// value: self $muldiv_op rhs.value,
// }
// }
// }

impl<D, U, V> $crate::stdlib::ops::$MulDivAssignTrait<V> for Quantity<D, U, V>
where
D: Dimension + ?Sized,
Expand All @@ -455,13 +438,43 @@ macro_rules! system {
self.value $muldivassign_op rhs;
}
}

#[doc(hidden)]
mod $Mod {
storage_types! {
use super::super::*;

impl<D, U> $crate::stdlib::ops::$MulDivTrait<Quantity<D, U, V>> for V
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
$($crate::typenum::Z0: $crate::stdlib::ops::$AddSubTrait<D::$symbol>,)+
{
type Output = Quantity<
$quantities<
$($crate::typenum::$AddSubAlias<
$crate::typenum::Z0,
D::$symbol>,)+>,
U, V>;

#[inline(always)]
fn $muldiv_fun(self, rhs: Quantity<D, U, V>) -> Self::Output {
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self $muldiv_op rhs.value,
}
}
}
}
}
};
}

impl_ops!(Add, add, +, AddAssign, add_assign, +=, Sum,
Mul, mul, *, MulAssign, mul_assign, *=);
Mul, mul, *, MulAssign, mul_assign, *=, add_mul);
impl_ops!(Sub, sub, -, SubAssign, sub_assign, -=, Diff,
Div, div, /, DivAssign, div_assign, /=);
Div, div, /, DivAssign, div_assign, /=, sub_div);

impl<D, U, V> Quantity<D, U, V>
where
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ storage_types! {
format!("{:?}", TLength::new::<meter>(V::one())));
assert_eq!(
format!("{:?} m^-1", V::one()),
format!("{:?}", TLength::new::<meter>(V::one()).recip()));
format!("{:?}", V::one() / TLength::new::<meter>(V::one())));
assert_eq!(
format!("{:.2?} m^1", V::one()),
format!("{:.2?}", TLength::new::<meter>(V::one())));
Expand Down

0 comments on commit c754fa9

Please sign in to comment.