Skip to content

Commit

Permalink
NAUM-9 Add v2::dim::Dimension and IsCommensurableWith
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed May 24, 2024
1 parent 3979196 commit 03a8f89
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 4 deletions.
2 changes: 2 additions & 0 deletions crates/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
implemented for `Measurement`.
- NAUM-8: Added traits `v2::convert::ToReduced<T>` and `TryToReduced<T>`, and implemented for `Unit`
and `Measurement`, respectively.
- NAUM-9: Added traits `v2::dim::Dimension<D>` and `IsCommensurableWith<D, Rhs>` and implemented,
respectively for `Atom`, `Measurement`, `Term`, `Cow<'a, [Term]'>`, `Unit`.
- Added `Unit::into_terms()` for cases where you only need the `Term`s of the `Unit`.
- Added `unit` constant: `UNITY`
- Added `term` constants: `UNITY`, `UNITY_ARRAY`, and `UNITY_ARRAY_REF`.
Expand Down
3 changes: 3 additions & 0 deletions crates/api/src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ mod hash;
mod partial_eq;
mod reducible;

#[cfg(feature = "v2")]
mod v2;

#[cfg(test)]
mod atom_test;

Expand Down
1 change: 1 addition & 0 deletions crates/api/src/atom/v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod dim;
7 changes: 7 additions & 0 deletions crates/api/src/atom/v2/dim.rs
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)
}
}
1 change: 1 addition & 0 deletions crates/api/src/measurement/v2.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod convert;
mod dim;
22 changes: 22 additions & 0 deletions crates/api/src/measurement/v2/dim.rs
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)
}
}
2 changes: 2 additions & 0 deletions crates/api/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod partial_eq;
mod partial_ord;
mod reducible;
mod ucum_unit;
#[cfg(feature = "v2")]
mod v2;

use std::borrow::Cow;

Expand Down
5 changes: 1 addition & 4 deletions crates/api/src/term/is_compatible_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ impl IsCompatibleWith for Term {

impl<'a> IsCompatibleWith for Cow<'a, [Term]> {
fn is_compatible_with(&self, rhs: &Self) -> bool {
let lhs_annotation_composition = self.annotation_composition();
let rhs_annotation_composition = rhs.annotation_composition();

self.composition() == rhs.composition()
&& rhs_annotation_composition == lhs_annotation_composition
&& self.annotation_composition() == rhs.annotation_composition()
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/api/src/term/v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod dim;
34 changes: 34 additions & 0 deletions crates/api/src/term/v2/dim.rs
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)
}
}
1 change: 1 addition & 0 deletions crates/api/src/unit/v2.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod convert;
mod dim;
22 changes: 22 additions & 0 deletions crates/api/src/unit/v2/dim.rs
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)
}
}
1 change: 1 addition & 0 deletions crates/api/src/v2.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod convert;
pub mod dim;
35 changes: 35 additions & 0 deletions crates/api/src/v2/dim.rs
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()
}
}
8 changes: 8 additions & 0 deletions crates/atom_generator/src/generator/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ pub(super) fn generate_file_body(atom_list: &RustAtomList) -> String {
#![allow(clippy::unreadable_literal, clippy::too_many_lines, clippy::match_same_arms)]

mod composable;
mod definition;
mod display;
mod function_set;
mod hash;
mod partial_eq;
mod reducible;

#[cfg(feature = "v2")]
mod v2;

#[cfg(test)]
mod atom_test;

use crate::{
is_compatible_with::DefaultCompatibility, reducible::Reducible, Classification, Property,
UcumSymbol, UcumUnit, Unit,
Expand Down

0 comments on commit 03a8f89

Please sign in to comment.