Skip to content

Commit

Permalink
Implement num::Saturating.
Browse files Browse the repository at this point in the history
Part of #35.
  • Loading branch information
radix committed Dec 28, 2017
1 parent 641ebe5 commit 6d169a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,21 @@ 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::Num + $crate::Conversion<V> + $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<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,7 @@ mod quantity_macro {
}

mod system_macro {

storage_types! {
use tests::*;

Expand Down Expand Up @@ -530,6 +531,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<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)))
}
}
}
}

mod float {
storage_types! {
types: Float;
Expand Down

0 comments on commit 6d169a7

Please sign in to comment.