From 04e0f607ceb959630cff4a6d451c66634ae2dc48 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Fri, 25 Mar 2022 21:22:48 +0100 Subject: [PATCH] Add Quantity::as_ and Quantity::cast for changing the underlying storage type. --- src/lib.rs | 4 +++- src/system.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index be430b4a..1c45e5ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -269,7 +269,9 @@ pub mod num { #[cfg(not(feature = "std"))] pub use num_traits::float::FloatCore as Float; - pub use num_traits::{pow, FromPrimitive, Num, One, Saturating, Signed, ToPrimitive, Zero}; + pub use num_traits::{ + pow, AsPrimitive, FromPrimitive, Num, NumCast, One, Saturating, Signed, ToPrimitive, Zero, + }; #[cfg(feature = "bigint-support")] pub use num_bigint::{BigInt, BigUint}; diff --git a/src/system.rs b/src/system.rs index 63245537..18ffebe6 100644 --- a/src/system.rs +++ b/src/system.rs @@ -378,6 +378,72 @@ macro_rules! system { .value() }} + impl Quantity, V> + where + D: Dimension + ?Sized, + $units: Units, + V: $crate::num::Num + $crate::Conversion, + { + /// Cast to a quantity with the same dimension and unit but a different underlying + /// storage type allowing for narrowing and precision loss. + /// + /// # Examples + #[cfg_attr(all(feature = "si", feature = "f32", feature = "i32"), doc = " ```rust")] + #[cfg_attr(not(all(feature = "si", feature = "f32", feature = "i32")), doc = " ```rust,ignore")] + /// # use uom::si::{f32, i32}; + /// # use uom::si::time::second; + /// let t = f32::Time::new::(1.5); + /// + /// let t: i32::Time = t.as_(); + /// + /// assert_eq!(t.get::(), 1); + /// ``` + #[inline(always)] + pub fn as_(self) -> Quantity, W> + where + V: $crate::num::AsPrimitive, + $units: Units, + W: $crate::num::Num + $crate::Conversion + Copy + 'static, + { + Quantity { + dimension: $crate::lib::marker::PhantomData, + units: $crate::lib::marker::PhantomData, + value: self.value.as_(), + } + } + + /// Cast to a quantity with the same dimension and unit but a different underlying + /// storage type allowing for precision loss and truncation. + /// + /// # Examples + #[cfg_attr(all(feature = "si", feature = "f32", feature = "i32"), doc = " ```rust")] + #[cfg_attr(not(all(feature = "si", feature = "f32", feature = "i32")), doc = " ```rust,ignore")] + /// # use uom::si::{f32, i32}; + /// # use uom::si::time::second; + /// let t1 = f32::Time::new::(1.5); + /// let t2 = f32::Time::new::(1.0e32); + /// + /// let t1: Option = t1.cast(); + /// let t2: Option = t2.cast(); + /// + /// assert_eq!(t1, Some(i32::Time::new::(1))); + /// assert_eq!(t2, None); + /// ``` + #[inline(always)] + pub fn cast(self) -> Option, W>> + where + V: $crate::num::NumCast, + $units: Units, + W: $crate::num::Num + $crate::Conversion + $crate::num::NumCast, + { + Some(Quantity { + dimension: $crate::lib::marker::PhantomData, + units: $crate::lib::marker::PhantomData, + value: $crate::num::NumCast::from(self.value)?, + }) + } + } + #[doc(hidden)] macro_rules! impl_ops { (