-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
NAUM-10 Add v2::ops::CommensurableEq trait and impl
- Loading branch information
Showing
16 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod dim; | ||
mod ops; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
use crate::{v2::dim::Dimension, Atom, Composable, Composition}; | ||
use crate::{ | ||
v2::dim::{Dimension, IsCommensurableWith}, | ||
Atom, Composable, Composition, IsCompatibleWith, | ||
}; | ||
|
||
impl Dimension<Composition> for Atom { | ||
fn dimension(&self) -> Composition { | ||
Composable::composition(self) | ||
} | ||
} | ||
|
||
impl IsCommensurableWith<Composition> for Atom { | ||
fn is_commensurable_with(&self, rhs: &Self) -> bool { | ||
IsCompatibleWith::is_compatible_with(self, rhs) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use approx::ulps_eq; | ||
|
||
use crate::{ | ||
v2::{dim::IsCommensurableWith, ops::CommensurableEq}, | ||
Atom, Composition, UcumUnit, | ||
}; | ||
|
||
impl CommensurableEq<Composition> for Atom { | ||
fn commensurable_eq(&self, other: &Self) -> Option<bool> { | ||
if !self.is_commensurable_with(other) { | ||
return None; | ||
} | ||
|
||
Some(ulps_eq!(self.scalar(), other.scalar())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn commensurable_eq_test() { | ||
let lhs = Atom::Meter; | ||
|
||
assert_eq!(lhs.commensurable_eq(&lhs), Some(true)); | ||
assert_eq!(lhs.commensurable_eq(&Atom::FootInternational), Some(false)); | ||
assert!(lhs.commensurable_eq(&Atom::Gram).is_none()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod convert; | ||
mod dim; | ||
mod ops; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use approx::ulps_eq; | ||
|
||
use crate::{ | ||
v2::{dim::IsCommensurableWith, ops::CommensurableEq}, | ||
Composition, Measurement, UcumUnit, | ||
}; | ||
|
||
impl CommensurableEq<Composition> for Measurement { | ||
fn commensurable_eq(&self, other: &Self) -> Option<bool> { | ||
if !self.is_commensurable_with(other) { | ||
return None; | ||
} | ||
|
||
Some(ulps_eq!(self.scalar(), other.scalar())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::testing::const_units::{GRAM, KILOMETER, METER}; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn commensurable_eq_test() { | ||
let lhs = Measurement::new(1000.0, METER); | ||
assert_eq!(lhs.commensurable_eq(&lhs), Some(true)); | ||
|
||
let rhs = Measurement::new(1.0, KILOMETER); | ||
assert_eq!(lhs.commensurable_eq(&rhs), Some(true)); | ||
|
||
let rhs = Measurement::new(1.1, KILOMETER); | ||
assert_eq!(lhs.commensurable_eq(&rhs), Some(false)); | ||
|
||
let rhs = Measurement::new(1000.0, GRAM); | ||
assert_eq!(lhs.commensurable_eq(&rhs), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod dim; | ||
mod ops; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use approx::ulps_eq; | ||
|
||
use crate::{ | ||
v2::{dim::IsCommensurableWith, ops::CommensurableEq}, | ||
Composition, Term, UcumUnit, | ||
}; | ||
|
||
impl CommensurableEq<Composition> for Term { | ||
fn commensurable_eq(&self, other: &Self) -> Option<bool> { | ||
if !self.is_commensurable_with(other) { | ||
return None; | ||
} | ||
|
||
Some(ulps_eq!(self.scalar(), other.scalar())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn commensurable_eq_test() { | ||
let lhs = term!(Meter, factor: 1000); | ||
let rhs = term!(Kilo, Meter); | ||
assert_eq!(lhs.commensurable_eq(&rhs), Some(true)); | ||
|
||
let lhs = term!(Meter, factor: 10); | ||
let rhs = term!(Kilo, Meter); | ||
assert_eq!(lhs.commensurable_eq(&rhs), Some(false)); | ||
|
||
let lhs = term!(Meter); | ||
let rhs = term!(Gram); | ||
assert!(lhs.commensurable_eq(&rhs).is_none()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod convert; | ||
mod dim; | ||
mod ops; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use approx::ulps_eq; | ||
|
||
use crate::{ | ||
v2::{dim::IsCommensurableWith, ops::CommensurableEq}, | ||
Composition, UcumUnit, Unit, | ||
}; | ||
|
||
impl CommensurableEq<Composition> for Unit { | ||
fn commensurable_eq(&self, other: &Self) -> Option<bool> { | ||
if !self.is_commensurable_with(other) { | ||
return None; | ||
} | ||
|
||
Some(ulps_eq!(self.scalar(), other.scalar())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn commensurable_eq_test() { | ||
let lhs = unit!(term!(Meter, factor: 1000)); | ||
let rhs = unit!(term!(Kilo, Meter)); | ||
assert_eq!(lhs.commensurable_eq(&rhs), Some(true)); | ||
|
||
let lhs = unit!(term!(Meter, factor: 10)); | ||
let rhs = unit!(term!(Kilo, Meter)); | ||
assert_eq!(lhs.commensurable_eq(&rhs), Some(false)); | ||
|
||
let lhs = unit!(term!(Meter)); | ||
let rhs = unit!(term!(Gram)); | ||
assert!(lhs.commensurable_eq(&rhs).is_none()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod convert; | ||
pub mod dim; | ||
pub mod ops; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::ops::Mul; | ||
|
||
use super::dim::IsCommensurableWith; | ||
|
||
/// Trait for comparisons that checks for equality first. Equality checks must only be done with | ||
/// types that are commensurable. Return `None` if types are _not_ commensurable, otherwise | ||
/// `Some(result)`. | ||
/// | ||
/// This trait should be (well, should become) the de facto method for checking equality between | ||
/// measurements (notice lower-case "m" there, indicating objects that can measure, not necessarily | ||
/// only `Measurement`), etc. Historically, we've used `PartialEq` implementations, but those | ||
/// should've been reserved for object equality, not semantic equality (this trait checks the | ||
/// latter, not the former). | ||
/// | ||
pub trait CommensurableEq<D, Rhs = Self>: IsCommensurableWith<D, Rhs> | ||
where | ||
Rhs: IsCommensurableWith<D>, | ||
D: PartialEq + Mul<i32, Output = D>, | ||
{ | ||
/// This method tests the commensurability between `self` and `other`, then checks if their | ||
/// scalar values are equal. | ||
/// | ||
fn commensurable_eq(&self, other: &Rhs) -> Option<bool>; | ||
|
||
/// This method tests the commensurability between `self` and `other`, then checks if their | ||
/// scalar values are _not_ equal. | ||
/// | ||
#[inline] | ||
fn commensurable_ne(&self, other: &Rhs) -> Option<bool> { | ||
self.commensurable_eq(other).map(|r| !r) | ||
} | ||
} |