Skip to content

Commit

Permalink
PLCC-287 impl Neg for Measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed Feb 14, 2024
1 parent 82480ac commit 48a9e83
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 85 deletions.
10 changes: 6 additions & 4 deletions crates/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- PLCC-287: `impl num_traits::Inv for Measurement`, `Unit`, and `Term`.
- PLCC-287: `impl num_traits::One for Measurement`.
- PLCC-287: `impl From<&Measurement> for f64`.
- PLCC-287: `impl num_traits::ToPrimitive for Measurement`.
- PLCC-287: `impl num_traits::FromPrimitive for Measurement`.
- PLCC-287: `impl num_traits::ToPrimitive for Measurement`, `Prefix`.
- PLCC-287: `impl num_traits::FromPrimitive for Measurement`, `Prefix`.
- PLCC-287: `impl num_traits::NumCast for Measurement`.
- Added `Unit::into_terms()` for cases where you just need the `Term`s of the `Unit`.
- PLCC-287: `impl num_traits::Pow<i32> for Measurement`, `Unit`, `Term`.
- PLCC-287: `impl std::ops::Neg for Measurement`.
- 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`.
- Added `measurement!()` macro for wrapping `Measurement::try_new().unwrap()`.
Expand All @@ -41,7 +43,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Implemented `AsRef<Self>` for `Measurement` and `Unit`. Allows for downstream wrapper types to
implement other functionality for all types via the `AsRef` implementation.
- Implemented `TryFrom<&str> for Unit` (which just calls `from_str()`) to allow for downstream
- Implemented `TryFrom<&str> for Unit` (which only calls `from_str()`) to allow for downstream
wrapper implementations around `Unit`.
- New `const` `Composition` methods: `new_dimless()`, `new_any()`.
- New `composition` `const`s for common dimensional compositions.
Expand Down
14 changes: 7 additions & 7 deletions crates/api/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro_rules! term {

($prefix:ident, $atom:ident, $($attribute_name:ident: $attribute_value:expr),+) => {
{
let mut term = Term::new(Some(Prefix::$prefix), Some(Atom::$atom));
let mut term = $crate::Term::new(Some($crate::Prefix::$prefix), Some($crate::Atom::$atom));
$(
term!(@params term, $attribute_name: $attribute_value);
)+
Expand All @@ -28,12 +28,12 @@ macro_rules! term {
};

($prefix:ident, $atom:ident) => {
Term::new(Some(Prefix::$prefix), Some(Atom::$atom))
$crate::Term::new(Some(Prefix::$prefix), Some($crate::Atom::$atom))
};

($atom:ident, $($attribute_name:ident: $attribute_value:expr),+) => {
{
let mut term = Term::new(None, Some(Atom::$atom));
let mut term = $crate::Term::new(None, Some($crate::Atom::$atom));
$(
term!(@params term, $attribute_name: $attribute_value);
)+
Expand All @@ -42,12 +42,12 @@ macro_rules! term {
};

($atom:ident) => {
Term::new(None, Some(Atom::$atom))
$crate::Term::new(None, Some($crate::Atom::$atom))
};

($($attribute_name:ident: $attribute_value:expr),+) => {
{
let mut term = Term::default();
let mut term = $crate::Term::default();
$(
term!(@params term, $attribute_name: $attribute_value);
)+
Expand All @@ -56,14 +56,14 @@ macro_rules! term {
};

() => {
Term::default()
$crate::Term::default()
};
}

#[macro_export]
macro_rules! terms {
($($term:expr),*) => {
std::borrow::Cow::<[Term]>::Owned(vec![$($term)*])
std::borrow::Cow::<[$crate::Term]>::Owned(vec![$($term)*])
};
}

Expand Down
24 changes: 24 additions & 0 deletions crates/api/src/measurement/num_traits/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ impl<'a> Pow<i32> for &'a mut Measurement {
self
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn owned_test() {
let subject = measurement!(10.0, "L");
assert_eq!(Pow::pow(subject, 2), measurement!(100.0, "L2"));
}

#[test]
fn borrowed_test() {
let subject = measurement!(10.0, "L");
assert_eq!(Pow::pow(&subject, 2), measurement!(100.0, "L2"));
}

#[test]
fn mut_borrowed_test() {
let mut subject = measurement!(10.0, "L");
let _ = Pow::pow(&mut subject, 2);
assert_eq!(subject, measurement!(100.0, "L2"));
}
}
71 changes: 69 additions & 2 deletions crates/api/src/measurement/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,41 @@
#![allow(clippy::result_large_err)]

use crate::{convertible::Convertible, error::Error, measurement::Measurement};
use std::ops::{Add, Div, Mul, Sub};
use std::ops::{Add, Div, Mul, Neg, Sub};

// ╭──────────╮
// │ impl Neg │
// ╰──────────╯
impl Neg for Measurement {
type Output = Self;

fn neg(self) -> Self::Output {
Self {
value: self.value.neg(),
unit: self.unit,
}
}
}

impl<'a> Neg for &'a Measurement {
type Output = Measurement;

fn neg(self) -> Self::Output {
Measurement {
value: self.value.neg(),
unit: self.unit.clone(),
}
}
}

impl<'a> Neg for &'a mut Measurement {
type Output = Self;

fn neg(self) -> Self::Output {
self.value = self.value.neg();
self
}
}

//-----------------------------------------------------------------------------
// impl Add
Expand Down Expand Up @@ -327,8 +361,41 @@ mod tests {
};
}

mod neg {
use std::ops::Neg;

#[test]
fn owned_test() {
let subject = measurement!(10.0, "L");
assert_eq!(Neg::neg(subject), measurement!(-10.0, "L"));

let subject = measurement!(-10.0, "L");
assert_eq!(Neg::neg(subject), measurement!(10.0, "L"));
}

#[test]
fn borrowed_test() {
let subject = measurement!(10.0, "L");
assert_eq!(Neg::neg(&subject), measurement!(-10.0, "L"));

let subject = measurement!(-10.0, "L");
assert_eq!(Neg::neg(&subject), measurement!(10.0, "L"));
}

#[test]
fn mut_borrowed_test() {
let mut subject = measurement!(10.0, "L");
let _ = Neg::neg(&mut subject);
assert_eq!(subject, measurement!(-10.0, "L"));

let mut subject = measurement!(-10.0, "L");
let _ = Neg::neg(&mut subject);
assert_eq!(subject, measurement!(10.0, "L"));
}
} /* neg */

mod add {
use crate::{Term, Unit};
use crate::Unit;

use super::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/parser/atom_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::{
parser::{
term::UNITY_ARRAY, Atom, Classification, Composable, Composition, Dimension, Prefix, Term,
term::UNITY_ARRAY, Atom, Classification, Composable, Composition, Dimension, Prefix,
UcumSymbol,
},
ucum_unit::UcumUnit,
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/parser/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub(crate) mod u128 {
pub(crate) const YOTTA: u128 = 1_000_000_000_000_000_000_000_000;

pub(crate) const KIBI: u128 = 1024;
pub(crate) const GIBI: u128 = 1_073_741_824;
pub(crate) const MEBI: u128 = 1_048_576;
pub(crate) const GIBI: u128 = 1_073_741_824;
pub(crate) const TEBI: u128 = 1_099_511_627_776;
}

Expand Down
Loading

0 comments on commit 48a9e83

Please sign in to comment.