From 812678b34b21bcfa054077fdb4ced5ae0de231cd Mon Sep 17 00:00:00 2001 From: Zachary Sunforge Date: Fri, 3 Jan 2025 10:43:55 -0800 Subject: [PATCH] Added torr to quantities --- Cargo.toml | 2 +- src/quantity/pressure.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 98e5c34..efa8a50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ members = [ ] [workspace.package] -version = "0.4.3" +version = "0.4.4" edition = "2021" repository = "https://git.bfpower.io/BFPOWER/physical" readme = "README.md" diff --git a/src/quantity/pressure.rs b/src/quantity/pressure.rs index 0e8b679..fdda364 100644 --- a/src/quantity/pressure.rs +++ b/src/quantity/pressure.rs @@ -1,7 +1,7 @@ -use crate::quantity::{Quantity, Value}; +use crate::quantity::{KILO, Quantity, Value}; use generate_quantity::quantity_type; -use super::KILO; +const PASCALS_PER_TORR: f64 = 101325.0 / 760.0; //----- Pascals ---------------------------------- quantity_type! {Pascals, "Pa"} @@ -12,6 +12,13 @@ impl Pascals { let divisor = V::from_u16(KILO).unwrap(); KiloPascals(self.0 / divisor) } + + // Should this be in separate imple with `V` bound to `num_traits::Float`? + #[inline] + pub fn to_torr(self) -> Torr { + let divisor = V::from_f64(PASCALS_PER_TORR).unwrap(); + Torr(self.0 / divisor) + } } //----- Kilopascals ---------------------------------- @@ -25,12 +32,25 @@ impl KiloPascals { } } +//----- Torr ---------------------------------- +quantity_type! {Torr, "Torr"} + +// Should this bound `V` to `num_traits::Float`? +impl Torr { + #[inline] + pub fn to_pascals(self) -> Pascals { + let multiplier = V::from_f64(PASCALS_PER_TORR).unwrap(); + Pascals(self.0 * multiplier) + } +} + // --------------------------------------------------------------------------------------------------------------------- // ----- Tests ------------------------ // --------------------------------------------------------------------------------------------------------------------- #[cfg(test)] mod tests { use super::*; + use float_cmp::assert_approx_eq; #[test] fn convert_u32() { @@ -40,4 +60,13 @@ mod tests { assert_eq!(pascals.0, kilo_pascals.to_pascals().0); assert_eq!(kilo_pascals.0, pascals.to_kilo_pascals().0); } + + #[test] + fn torr_pascal() { + let torr: Torr = 7.5.torr(); + let pascals: Pascals = 999.9177631578947.pascals(); + + assert_approx_eq!(f32, pascals.to_torr().0, torr.0); + assert_approx_eq!(f32, torr.to_pascals().0, pascals.0); + } }