From c754fa9e4ce5c26e1b04d0b377602f2432132b0d Mon Sep 17 00:00:00 2001 From: Mike Boutin Date: Sun, 26 Nov 2017 09:04:36 -0500 Subject: [PATCH] Explicitly implement `{Mul,Div} for V`. Explicit implementations allow for a literal left hand side. Part of #29. --- src/si/frequency.rs | 8 +++---- src/system.rs | 55 ++++++++++++++++++++++++++++----------------- src/tests.rs | 2 +- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/si/frequency.rs b/src/si/frequency.rs index 32c1a4de..a930c479 100644 --- a/src/si/frequency.rs +++ b/src/si/frequency.rs @@ -50,8 +50,8 @@ mod tests { #[test] fn check_dimension() { - let _: Time = Frequency::new::(V::one()).recip(); - let _: Frequency = Time::new::(V::one()).recip(); + let _: Time = V::one() / Frequency::new::(V::one()); + let _: Frequency = V::one() / Time::new::(V::one()); } #[test] @@ -80,10 +80,10 @@ mod tests { // TODO #17 Convert to == once PartialEq is implemented. fn test, F: f::Conversion>(t: T, f: F) { - assert_ulps_eq!((Time::new::(V::one()).recip()).get(f), + assert_ulps_eq!((V::one() / Time::new::(V::one())).get(f), Frequency::new::(V::one()).get(f), epsilon = V::epsilon()); assert_ulps_eq!(Time::new::(V::one()).get(t), - (Frequency::new::(V::one()).recip()).get(t), epsilon = V::epsilon()); + (V::one() / Frequency::new::(V::one())).get(t), epsilon = V::epsilon()); } } } diff --git a/src/system.rs b/src/system.rs index 16de1fae..c9a68499 100644 --- a/src/system.rs +++ b/src/system.rs @@ -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 $crate::stdlib::ops::$AddSubTrait> for Quantity @@ -425,24 +426,6 @@ macro_rules! system { } } - // impl $crate::stdlib::ops::$MulDivTrait> for V - // where - // D: Dimension + ?Sized, - // U: Units + ?Sized, - // V: $crate::num::Num + $crate::stdlib::ops::$MulDivTrait, - // { - // type Output = Quantity; - - // #[inline(always)] - // fn $muldiv_fun(self, rhs: Quantity) -> Self::Output { - // Quantity { - // dimension: $crate::stdlib::marker::PhantomData, - // units: $crate::stdlib::marker::PhantomData, - // value: self $muldiv_op rhs.value, - // } - // } - // } - impl $crate::stdlib::ops::$MulDivAssignTrait for Quantity where D: Dimension + ?Sized, @@ -455,13 +438,43 @@ macro_rules! system { self.value $muldivassign_op rhs; } } + + #[doc(hidden)] + mod $Mod { + storage_types! { + use super::super::*; + + impl $crate::stdlib::ops::$MulDivTrait> for V + where + D: Dimension + ?Sized, + U: Units + ?Sized, + $($crate::typenum::Z0: $crate::stdlib::ops::$AddSubTrait,)+ + { + type Output = Quantity< + $quantities< + $($crate::typenum::$AddSubAlias< + $crate::typenum::Z0, + D::$symbol>,)+>, + U, V>; + + #[inline(always)] + fn $muldiv_fun(self, rhs: Quantity) -> 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 Quantity where diff --git a/src/tests.rs b/src/tests.rs index 26bdf67c..95db3328 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -245,7 +245,7 @@ storage_types! { format!("{:?}", TLength::new::(V::one()))); assert_eq!( format!("{:?} m^-1", V::one()), - format!("{:?}", TLength::new::(V::one()).recip())); + format!("{:?}", V::one() / TLength::new::(V::one()))); assert_eq!( format!("{:.2?} m^1", V::one()), format!("{:.2?}", TLength::new::(V::one())));