#[repr(C)]pub struct Unit {
pub second: i32,
pub meter: i32,
pub kilogram: i32,
pub ampere: i32,
pub kelvin: i32,
pub mol: i32,
pub candela: i32,
}Expand description
Struct representing a unit of measurement in the SI system via the exponents of
the base units. The unit is purely defined by the values of its fields, meaning
that it can change at runtime. The struct implements basic arithmetic functions
such as multiplication and division (via the Mul, MulAssign, Div,
DivAssign traits), exponentiation (Unit::powi) and a fallible version
of root calculation (Unit::try_nthroot).
§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.
An Unit can be deserialized from both its “standard” serialized
representation and from a PredefUnit variant:
use dyn_quantity::Unit;
// Direct deserialization
let str = "---\nsecond: -3\nmeter: 2\nkilogram: 1\nampere: -1\nkelvin: 0\nmol: 0\ncandela: 0";
let unit_direct: Unit = serde_yaml::from_str(str).unwrap();
// Deserialization from PredefUnit
let str = "ElectricVoltage";
let unit_predef: Unit = serde_yaml::from_str(str).unwrap();
assert_eq!(unit_predef, unit_direct);Fields§
§second: i32Exponent for the SI base unit of time.
meter: i32Exponent for the SI base unit of length.
kilogram: i32Exponent for the SI base unit of mass.
ampere: i32Exponent for the SI base unit of electrical current.
kelvin: i32Exponent for the SI base unit of temperature.
mol: i32Exponent for the SI base unit of amount of substance.
candela: i32Exponent for the SI base unit of luminous intensity
Implementations§
Source§impl Unit
impl Unit
Sourcepub fn powi(self, n: i32) -> Unit
pub fn powi(self, n: i32) -> Unit
Raises self to an integer power.
§Examples
use dyn_quantity::Unit;
let exponents = Unit::from([0, 1, 0, 2, 0, -2, 0]);
let array: [i32; 7] = exponents.powi(2).into();
assert_eq!(array, [0, 2, 0, 4, 0, -4, 0]);Sourcepub fn try_nthroot(self, n: i32) -> Result<Unit, RootError>
pub fn try_nthroot(self, n: i32) -> Result<Unit, RootError>
Tries to calculate the nth root of self. This operation fails if any
of the exponents is not divisible by n.
§Examples
use dyn_quantity::Unit;
let unit = Unit::from([0, 2, 0, 2, 0, -4, 0]);
// It is possible to calculate the square root:
let array: [i32; 7] = unit.clone().try_nthroot(2).unwrap().into();
assert_eq!(array, [0, 1, 0, 1, 0, -2, 0]);
// But not the cubic root (not all exponents are divisible by 3):
assert!(unit.try_nthroot(3).is_err());Sourcepub fn is_dimensionless(&self) -> bool
pub fn is_dimensionless(&self) -> bool
Returns whether Unit is dimensionless (all exponents are zero) or not.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Unit
impl<'de> Deserialize<'de> for Unit
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Unit, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Unit, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl DivAssign for Unit
impl DivAssign for Unit
Source§fn div_assign(&mut self, rhs: Unit)
fn div_assign(&mut self, rhs: Unit)
/= operation. Read moreSource§impl From<[i32; 7]> for Unit
impl From<[i32; 7]> for Unit
Source§fn from(array: [i32; 7]) -> Unit
fn from(array: [i32; 7]) -> Unit
Converts an array of seven i32 values into Unit.
The individual array elements are interpreted as follows:
array[0]: Exponent of secondarray[1]: Exponent of meterarray[2]: Exponent of kilogramarray[3]: Exponent of amperearray[4]: Exponent of kelvinarray[5]: Exponent of molarray[6]: Exponent of candela
Source§impl From<PredefUnit> for Unit
impl From<PredefUnit> for Unit
Source§fn from(value: PredefUnit) -> Unit
fn from(value: PredefUnit) -> Unit
Source§impl From<Unit> for [i32; 7]
impl From<Unit> for [i32; 7]
Source§fn from(value: Unit) -> [i32; 7]
fn from(value: Unit) -> [i32; 7]
Converts an Unit into an array of seven i32.
The exponents are put into the array in the following order:
array[0]: Exponent of secondarray[1]: Exponent of meterarray[2]: Exponent of kilogramarray[3]: Exponent of amperearray[4]: Exponent of kelvinarray[5]: Exponent of molarray[6]: Exponent of candela
Source§impl MulAssign for Unit
impl MulAssign for Unit
Source§fn mul_assign(&mut self, rhs: Unit)
fn mul_assign(&mut self, rhs: Unit)
*= operation. Read more