Skip to content

Commit

Permalink
implement Saturating when the backend type supports Saturating.
Browse files Browse the repository at this point in the history
  • Loading branch information
radix committed Dec 24, 2017
1 parent f809cee commit 027e8be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,20 @@ macro_rules! system {
}
}

impl<D, U, V> $crate::num::Saturating for Quantity<D, U, V>
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
V: $crate::num::Saturating + $crate::num::Num + $crate::Conversion<V>,
{
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<D, U, V> $crate::num::Zero for Quantity<D, U, V>
where
D: Dimension + ?Sized,
Expand Down
29 changes: 28 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -379,6 +379,33 @@ mod quantity_macro {
}

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<V>, r: A<V>) -> bool {
Test::eq(&(l.saturating_add(*r)),
&(Length::new::<meter>((*l).clone())
.saturating_add(Length::new::<meter>((*r).clone())).get(meter)))
}

#[allow(trivial_casts)]
fn saturating_sub(l: A<V>, r: A<V>) -> bool {
Test::eq(&(l.saturating_sub(*r)),
&(Length::new::<meter>((*l).clone())
.saturating_sub(Length::new::<meter>((*r).clone())).get(meter)))
}
}
}
}

storage_types! {
use tests::*;

Expand Down

0 comments on commit 027e8be

Please sign in to comment.