#[repr(C)]pub struct DynQuantity<V>where
V: F64RealOrComplex,{
pub value: V,
pub unit: Unit,
}Expand description
This type represents a physical quantity via its numerical value and a unit of
measurement (field exponents). The unit of measurement is not defined via the
type system, but rather via the values of the Unit. This means that
the unit of measurement is not fixed at compile time, but can change dynamically
at runtime.
This property is very useful when e.g. parsing a user-provided string to a
physical quantity. For this case, DynQuantity implements the
FromStr trait. The module documentation
from_str has more information regarding the
available syntax.
The V generic parameter needs to implement F64RealOrComplex.
Currently, implementors are f64 and Complex<f64>. It is possible to
convert between those two types using the methods provided by F64RealOrComplex.
In general, you likely want V to be f64 except when dealing with complex
quantities such as alternating currents.
This struct can also be pretty-print the quantity it represents via its
std::fmt::Display implementation:
use std::str::FromStr;
use dyn_quantity::DynQuantity;
let quantity = DynQuantity::<f64>::from_str("9.81 m/s^2").expect("parseable");
assert_eq!(quantity.to_string(), "9.81 s^-2 m".to_string());§Conversion into uom Quantity
If the uom feature is enabled, a DynQuantity can be (fallible)
converted into a
Quantity via
TryFrom. In combination with the aforementioned parsing capabilities, this
allows fallible parsing of strings to statically-typed physical quantities.
§Serialization and deserialization
If the serde feature is enabled, this struct can be serialized and
deserialized. Serialization creates the “standard”
serde representation one would expect from the
Serialize macro.
When deserializing however, multiple options are available:
- Using the “standard” serialized representation of a struct. For example, the
yaml representation of a
DynQuantity<f64>looks like this:
---
value: 2.0
exponents:
second: 0
meter: 1
kilogram: 0
ampere: 1
kelvin: 0
mol: 0
candela: 0- Deserializing directly from a string. This uses the
std::str::FromStrimplementation under the hood, see thefrom_strmodule documentation. Only available if thefrom_strfeature is enabled. - Deserialize directly from a real or complex value. This option is mainly here
to allow deserializing a serialized uom quantity
(whose serialized representation is simply its numerical value without any
units). For example, deserializing
5.0intoDynQuantity<f64>produces the same result as deserializing:
---
value: 5.0
exponents:
second: 0
meter: 0
kilogram: 0
ampere: 0
kelvin: 0
mol: 0
candela: 0The three different possibilities are realized via a crate-internal untagged enum.
Fields§
§value: VThe value of the physical quantity.
unit: UnitThe (SI) base units of the physical quantity, represented by their exponents.
Implementations§
Source§impl<V> DynQuantity<V>where
V: F64RealOrComplex,
impl<V> DynQuantity<V>where
V: F64RealOrComplex,
Sourcepub fn new<U>(value: V, unit: U) -> DynQuantity<V>
pub fn new<U>(value: V, unit: U) -> DynQuantity<V>
Returns a new instance of Self.
Sourcepub fn try_add(
&self,
other: &DynQuantity<V>,
) -> Result<DynQuantity<V>, UnitsNotEqual>
pub fn try_add( &self, other: &DynQuantity<V>, ) -> Result<DynQuantity<V>, UnitsNotEqual>
Fallible addition of self and other.
Physical quantities can only be added together if their units are identical.
Hence, this function first compares the .unit fields of self and
other. If they are identical, the value fields are added up and the
resulting quantity is returned. Otherwise, a UnitsNotEqual
error is returned.
§Examples
use std::str::FromStr;
use dyn_quantity::DynQuantity;
let curr1 = DynQuantity::<f64>::from_str("1 A").expect("valid");
let curr2 = DynQuantity::<f64>::from_str("1 V*A / V").expect("valid");
let volt1 = DynQuantity::<f64>::from_str("-5 V").expect("valid");
// The currents can be added ...
let curr_sum = curr1.try_add(&curr2).expect("can be added");
assert_eq!(curr_sum.value, 2.0);
assert_eq!(curr_sum.unit.ampere, 1);
// ... but adding a current to a voltage fails.
assert!(volt1.try_add(&curr1).is_err());Sourcepub fn try_add_assign(
&mut self,
other: &DynQuantity<V>,
) -> Result<(), UnitsNotEqual>
pub fn try_add_assign( &mut self, other: &DynQuantity<V>, ) -> Result<(), UnitsNotEqual>
Like DynQuantity::try_add, but assigns the sum of self and other to
self instead of returning it. If the addition fails, self is not modified.
§Examples
use std::str::FromStr;
use dyn_quantity::DynQuantity;
let mut curr1 = DynQuantity::<f64>::from_str("1 A").expect("valid");
let curr2 = DynQuantity::<f64>::from_str("1 V*A / V").expect("valid");
let mut volt1 = DynQuantity::<f64>::from_str("-5 V").expect("valid");
// curr1 gets overwritten
curr1.try_add_assign(&curr2).expect("can be added");
assert_eq!(curr1.value, 2.0);
assert_eq!(curr1.unit.ampere, 1);
// volt1 does not get modified because the addition failed
let volt_cpy = volt1.clone();
assert!(volt1.try_add_assign(&curr1).is_err());
assert_eq!(volt1, volt_cpy);Sourcepub fn try_sub(
&self,
other: &DynQuantity<V>,
) -> Result<DynQuantity<V>, UnitsNotEqual>
pub fn try_sub( &self, other: &DynQuantity<V>, ) -> Result<DynQuantity<V>, UnitsNotEqual>
Fallible subtraction of self and other.
Physical quantities can only be subtracted from each other if their units are identical.
Hence, this function first compares the .unit fields of self and
other. If they are identical, the value fields are subtracted up and the
resulting quantity is returned. Otherwise, a UnitsNotEqual
error is returned.
§Examples
use std::str::FromStr;
use dyn_quantity::DynQuantity;
let curr1 = DynQuantity::<f64>::from_str("1 A").expect("valid");
let curr2 = DynQuantity::<f64>::from_str("1 V*A / V").expect("valid");
let volt1 = DynQuantity::<f64>::from_str("-5 V").expect("valid");
// The currents can be subtracted ...
let curr_sum = curr1.try_sub(&curr2).expect("can be added");
assert_eq!(curr_sum.value, 0.0);
assert_eq!(curr_sum.unit.ampere, 1);
// ... but sbutracting a current from a voltage fails.
assert!(volt1.try_sub(&curr1).is_err());Sourcepub fn try_sub_assign(
&mut self,
other: &DynQuantity<V>,
) -> Result<(), UnitsNotEqual>
pub fn try_sub_assign( &mut self, other: &DynQuantity<V>, ) -> Result<(), UnitsNotEqual>
Like DynQuantity::try_sub, but assigns the difference of self and other to
self instead of returning it. If the subtraction fails, self is not modified.
§Examples
use std::str::FromStr;
use dyn_quantity::DynQuantity;
let mut curr1 = DynQuantity::<f64>::from_str("1 A").expect("valid");
let curr2 = DynQuantity::<f64>::from_str("1 V*A / V").expect("valid");
let mut volt1 = DynQuantity::<f64>::from_str("-5 V").expect("valid");
// curr1 gets overwritten
curr1.try_sub_assign(&curr2).expect("can be added");
assert_eq!(curr1.value, 0.0);
assert_eq!(curr1.unit.ampere, 1);
// volt1 does not get modified because the addition failed
let volt_cpy = volt1.clone();
assert!(volt1.try_sub_assign(&curr1).is_err());
assert_eq!(volt1, volt_cpy);Sourcepub fn powi(self, n: i32) -> DynQuantity<V>
pub fn powi(self, n: i32) -> DynQuantity<V>
Raises self to an integer power.
§Examples
use std::str::FromStr;
use dyn_quantity::DynQuantity;
let curr = DynQuantity::<f64>::from_str("2 A^2").expect("valid");
let result = curr.powi(3);
assert_eq!(result.value, 8.0);
assert_eq!(result.unit.ampere, 6);Sourcepub fn try_nthroot(self, n: i32) -> Result<DynQuantity<V>, RootError>
pub fn try_nthroot(self, n: i32) -> Result<DynQuantity<V>, RootError>
Tries to calculate the nth root of self.
Trait Implementations§
Source§impl<V> Clone for DynQuantity<V>where
V: Clone + F64RealOrComplex,
impl<V> Clone for DynQuantity<V>where
V: Clone + F64RealOrComplex,
Source§fn clone(&self) -> DynQuantity<V>
fn clone(&self) -> DynQuantity<V>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<V> Debug for DynQuantity<V>where
V: Debug + F64RealOrComplex,
impl<V> Debug for DynQuantity<V>where
V: Debug + F64RealOrComplex,
Source§impl<V> Default for DynQuantity<V>where
V: Default + F64RealOrComplex,
impl<V> Default for DynQuantity<V>where
V: Default + F64RealOrComplex,
Source§fn default() -> DynQuantity<V>
fn default() -> DynQuantity<V>
Source§impl<'de, V> Deserialize<'de> for DynQuantity<V>where
V: F64RealOrComplex + Deserialize<'de>,
impl<'de, V> Deserialize<'de> for DynQuantity<V>where
V: F64RealOrComplex + Deserialize<'de>,
Source§fn deserialize<D>(
deserializer: D,
) -> Result<DynQuantity<V>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<DynQuantity<V>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl<V> Display for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> Display for DynQuantity<V>where
V: F64RealOrComplex,
Source§impl<V> Div<f64> for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> Div<f64> for DynQuantity<V>where
V: F64RealOrComplex,
Source§impl<V> Div for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> Div for DynQuantity<V>where
V: F64RealOrComplex,
Source§type Output = DynQuantity<V>
type Output = DynQuantity<V>
/ operator.Source§fn div(self, rhs: DynQuantity<V>) -> <DynQuantity<V> as Div>::Output
fn div(self, rhs: DynQuantity<V>) -> <DynQuantity<V> as Div>::Output
/ operation. Read moreSource§impl<V> DivAssign<f64> for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> DivAssign<f64> for DynQuantity<V>where
V: F64RealOrComplex,
Source§fn div_assign(&mut self, rhs: f64)
fn div_assign(&mut self, rhs: f64)
/= operation. Read moreSource§impl<V> DivAssign for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> DivAssign for DynQuantity<V>where
V: F64RealOrComplex,
Source§fn div_assign(&mut self, rhs: DynQuantity<V>)
fn div_assign(&mut self, rhs: DynQuantity<V>)
/= operation. Read moreSource§impl From<DynQuantity<f64>> for DynQuantity<Complex<f64>>
impl From<DynQuantity<f64>> for DynQuantity<Complex<f64>>
Source§fn from(value: DynQuantity<f64>) -> DynQuantity<Complex<f64>>
fn from(value: DynQuantity<f64>) -> DynQuantity<Complex<f64>>
Source§impl<L, M, T, I, Th, N, J, K, V> From<Quantity<dyn Dimension<Kind = K, J = J, L = L, Th = Th, M = M, I = I, T = T, N = N>, dyn Units<f64, length = meter, luminous_intensity = candela, amount_of_substance = mole, time = second, mass = kilogram, electric_current = ampere, thermodynamic_temperature = kelvin>, f64>> for DynQuantity<V>
impl<L, M, T, I, Th, N, J, K, V> From<Quantity<dyn Dimension<Kind = K, J = J, L = L, Th = Th, M = M, I = I, T = T, N = N>, dyn Units<f64, length = meter, luminous_intensity = candela, amount_of_substance = mole, time = second, mass = kilogram, electric_current = ampere, thermodynamic_temperature = kelvin>, f64>> for DynQuantity<V>
Source§fn from(
quantitiy: Quantity<dyn Dimension<Kind = K, J = J, L = L, Th = Th, M = M, I = I, T = T, N = N>, dyn Units<f64, length = meter, luminous_intensity = candela, amount_of_substance = mole, time = second, mass = kilogram, electric_current = ampere, thermodynamic_temperature = kelvin>, f64>,
) -> DynQuantity<V>
fn from( quantitiy: Quantity<dyn Dimension<Kind = K, J = J, L = L, Th = Th, M = M, I = I, T = T, N = N>, dyn Units<f64, length = meter, luminous_intensity = candela, amount_of_substance = mole, time = second, mass = kilogram, electric_current = ampere, thermodynamic_temperature = kelvin>, f64>, ) -> DynQuantity<V>
Source§impl<V> FromStr for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> FromStr for DynQuantity<V>where
V: F64RealOrComplex,
Source§type Err = ParseError
type Err = ParseError
Source§fn from_str(s: &str) -> Result<DynQuantity<V>, <DynQuantity<V> as FromStr>::Err>
fn from_str(s: &str) -> Result<DynQuantity<V>, <DynQuantity<V> as FromStr>::Err>
s to return a value of this type. Read moreSource§impl<V> Mul<f64> for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> Mul<f64> for DynQuantity<V>where
V: F64RealOrComplex,
Source§impl<V> Mul for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> Mul for DynQuantity<V>where
V: F64RealOrComplex,
Source§type Output = DynQuantity<V>
type Output = DynQuantity<V>
* operator.Source§fn mul(self, rhs: DynQuantity<V>) -> <DynQuantity<V> as Mul>::Output
fn mul(self, rhs: DynQuantity<V>) -> <DynQuantity<V> as Mul>::Output
* operation. Read moreSource§impl<V> MulAssign<f64> for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> MulAssign<f64> for DynQuantity<V>where
V: F64RealOrComplex,
Source§fn mul_assign(&mut self, rhs: f64)
fn mul_assign(&mut self, rhs: f64)
*= operation. Read moreSource§impl<V> MulAssign for DynQuantity<V>where
V: F64RealOrComplex,
impl<V> MulAssign for DynQuantity<V>where
V: F64RealOrComplex,
Source§fn mul_assign(&mut self, rhs: DynQuantity<V>)
fn mul_assign(&mut self, rhs: DynQuantity<V>)
*= operation. Read more