diff --git a/CHANGELOG.md b/CHANGELOG.md index ce4f4f91..6b51264a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Added * [#26](https://github.com/iliekturtles/uom/issues/26) Implement `num::Zero`. + * [#35](https://github.com/iliekturtles/uom/issues/35) Implement `num::Saturating`. ## [v0.16.0] — 2017-12-21 This release contains significant changes in order to support underlying storage types that diff --git a/src/system.rs b/src/system.rs index 6e4a8b39..516a2d99 100644 --- a/src/system.rs +++ b/src/system.rs @@ -839,6 +839,21 @@ macro_rules! system { } } + impl $crate::num::Saturating for Quantity + where + D: Dimension + ?Sized, + U: Units + ?Sized, + V: $crate::num::Num + $crate::Conversion + $crate::num::Saturating, + { + fn saturating_add(self, v: Self) -> Self { + Quantity { value: self.value.saturating_add(v.value), ..self } + } + + fn saturating_sub(self, v: Self) -> Self { + Quantity { value: self.value.saturating_sub(v.value), ..self } + } + } + impl $crate::num::Zero for Quantity where D: Dimension + ?Sized, diff --git a/src/tests.rs b/src/tests.rs index 0eec8d41..6501e4a3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -3,7 +3,7 @@ use self::mass::kilogram; #[allow(unused_imports)] use {Conversion, ConversionFactor}; #[allow(unused_imports)] -use num::{Float, FromPrimitive, One, Signed, Zero}; +use num::{Float, FromPrimitive, One, Saturating, Signed, Zero}; use quickcheck::TestResult; use lib::fmt::Debug; use lib::marker::PhantomData; @@ -530,6 +530,32 @@ mod system_macro { } } + mod int { + storage_types! { + types: PrimInt, BigInt, BigUint; + + use tests::*; + + Q!(tests, V); + + quickcheck! { + #[allow(trivial_casts)] + fn saturating_add(l: A, r: A) -> bool { + Test::eq(&(l.saturating_add(*r)), + &(Length::new::((*l).clone()) + .saturating_add(Length::new::((*r).clone())).get(meter))) + } + + #[allow(trivial_casts)] + fn saturating_sub(l: A, r: A) -> bool { + Test::eq(&(l.saturating_sub(*r)), + &(Length::new::((*l).clone()) + .saturating_sub(Length::new::((*r).clone())).get(meter))) + } + } + } + } + mod float { storage_types! { types: Float;