-
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.
NAUM-9 Add v2::dim::Dimension and IsCommensurableWith
- Loading branch information
1 parent
3979196
commit 03a8f89
Showing
15 changed files
with
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ mod hash; | |
mod partial_eq; | ||
mod reducible; | ||
|
||
#[cfg(feature = "v2")] | ||
mod v2; | ||
|
||
#[cfg(test)] | ||
mod atom_test; | ||
|
||
|
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 @@ | ||
mod dim; |
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,7 @@ | ||
use crate::{v2::dim::Dimension, Atom, Composable, Composition}; | ||
|
||
impl Dimension<Composition> for Atom { | ||
fn dimension(&self) -> Composition { | ||
Composable::composition(self) | ||
} | ||
} |
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 convert; | ||
mod dim; |
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,22 @@ | ||
use crate::{ | ||
v2::dim::{Dimension, IsCommensurableWith}, | ||
Composable, Composition, IsCompatibleWith, Measurement, Unit, | ||
}; | ||
|
||
impl Dimension<Composition> for Measurement { | ||
fn dimension(&self) -> Composition { | ||
Composable::composition(self) | ||
} | ||
} | ||
|
||
impl IsCommensurableWith<Composition> for Measurement { | ||
fn is_commensurable_with(&self, rhs: &Self) -> bool { | ||
IsCompatibleWith::is_compatible_with(self, rhs) | ||
} | ||
} | ||
|
||
impl IsCommensurableWith<Composition, Unit> for Measurement { | ||
fn is_commensurable_with(&self, rhs: &Unit) -> 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
mod dim; |
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,34 @@ | ||
use std::borrow::Cow; | ||
|
||
use crate::{ | ||
v2::dim::{Dimension, IsCommensurableWith}, | ||
Composable, Composition, IsCompatibleWith, Term, | ||
}; | ||
|
||
impl Dimension<Composition> for Term { | ||
fn dimension(&self) -> Composition { | ||
Composable::composition(self) | ||
} | ||
} | ||
|
||
impl IsCommensurableWith<Composition> for Term { | ||
/// See `impl IsCompatibleWith for Term` as to why this has a special implementation here. | ||
/// | ||
fn is_commensurable_with(&self, rhs: &Self) -> bool { | ||
IsCompatibleWith::is_compatible_with(self, rhs) | ||
} | ||
} | ||
|
||
impl<'a> Dimension<Composition> for Cow<'a, [Term]> { | ||
fn dimension(&self) -> Composition { | ||
Composable::composition(self) | ||
} | ||
} | ||
|
||
impl<'a> IsCommensurableWith<Composition> for Cow<'a, [Term]> { | ||
/// See `impl<'a> IsCompatibleWith for Cow<'a, [Term]>` as to why this has a special implementation here. | ||
/// | ||
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
mod convert; | ||
mod dim; |
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,22 @@ | ||
use crate::{ | ||
v2::dim::{Dimension, IsCommensurableWith}, | ||
Composable, Composition, IsCompatibleWith, Measurement, Unit, | ||
}; | ||
|
||
impl Dimension<Composition> for Unit { | ||
fn dimension(&self) -> Composition { | ||
Composable::composition(self) | ||
} | ||
} | ||
|
||
impl IsCommensurableWith<Composition> for Unit { | ||
fn is_commensurable_with(&self, rhs: &Self) -> bool { | ||
IsCompatibleWith::is_compatible_with(self, rhs) | ||
} | ||
} | ||
|
||
impl IsCommensurableWith<Composition, Measurement> for Unit { | ||
fn is_commensurable_with(&self, rhs: &Measurement) -> 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod convert; | ||
pub mod dim; |
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,35 @@ | ||
//! Types here deal with core aspects of dimensional analysis of other types. | ||
//! | ||
use std::ops::Mul; | ||
|
||
/// A type that can represent the dimension of another type must implement this. | ||
/// | ||
pub trait Dimension<D> | ||
where | ||
D: PartialEq + Mul<i32, Output = D>, | ||
{ | ||
/// Returns the dimension of `self`, which may be a combination of more than one dimension () | ||
/// | ||
fn dimension(&self) -> D; | ||
} | ||
|
||
/// Trait for determining if two things are dimensionally equal, aka commensurable. Per [Dimensional analysis#Dimensional homogeneity | ||
/// (commensurability)](https://en.wikipedia.org/wiki/Dimensional_analysis#Dimensional_homogeneity_(commensurability): | ||
/// | ||
/// > The most basic rule of dimensional analysis is that of dimensional homogeneity.[6] | ||
/// > Only commensurable quantities (physical quantities having the same dimension) may be | ||
/// > compared, equated, added, or subtracted. | ||
/// | ||
/// Will replace `crate::IsCompatibleWith`. | ||
/// | ||
pub trait IsCommensurableWith<D, Rhs = Self>: Dimension<D> | ||
where | ||
Rhs: IsCommensurableWith<D>, | ||
D: PartialEq + Mul<i32, Output = D>, | ||
{ | ||
#[inline] | ||
fn is_commensurable_with(&self, rhs: &Rhs) -> bool { | ||
self.dimension() == rhs.dimension() | ||
} | ||
} |
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