Skip to content

Commit

Permalink
Implement PartialEq.
Browse files Browse the repository at this point in the history
Resolves #17.
  • Loading branch information
iliekturtles committed Jan 17, 2018
1 parent 014d361 commit 1a6c5ae
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## [Unreleased]

### Added
* [#17](https://github.com/iliekturtles/uom/issues/17) Implement `PartialEq`.
* [#26](https://github.com/iliekturtles/uom/issues/26) Implement `num::Zero`.
* [#35](https://github.com/iliekturtles/uom/issues/35) Implement `num::Saturating`.
* [#37](https://github.com/iliekturtles/uom/issues/35) Implement `serde::Serialize` and
Expand Down
13 changes: 13 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,19 @@ macro_rules! system {
}
}

impl<D, Ul, Ur, V> $crate::lib::cmp::PartialEq<Quantity<D, Ur, V>> for Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
Ul: Units<V> + ?Sized,
Ur: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V>,
{
#[inline(always)]
fn eq(&self, other: &Quantity<D, Ur, V>) -> bool {
self.value == change_base::<D, Ul, Ur, V>(&other.value)
}
}

impl<D, Ul, Ur, V> $crate::lib::ops::Rem<Quantity<D, Ur, V>> for Quantity<D, Ul, V>
where
D: Dimension + ?Sized,
Expand Down
26 changes: 26 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ mod system_macro {
&((*l).clone() / Length::new::<meter>((*r).clone())).value))
}

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

#[allow(trivial_casts)]
fn rem(l: A<V>, r: A<V>) -> TestResult {
if *r == V::zero() {
Expand Down Expand Up @@ -811,6 +817,26 @@ mod system_macro {
}

mod quantities_macro {
storage_types! {
use tests::*;

mod f { Q!(tests, super::V); }
mod k { Q!(tests, super::V, (kilometer, kilogram)); }

quickcheck! {
#[allow(trivial_casts)]
fn eq(l: A<V>, r: A<V>) -> bool {
let a = *l == *r;
let b = f::Length::new::<meter>((*l).clone())
== k::Length::new::<meter>((*r).clone());
let c = k::Length::new::<meter>((*l).clone())
== f::Length::new::<meter>((*r).clone());

a == b && a == c
}
}
}

mod fractional {
storage_types! {
types: Float, Ratio;
Expand Down

0 comments on commit 1a6c5ae

Please sign in to comment.