Struct const_units::Quantity
source · pub struct Quantity<V, const UNITS: SI>(pub V);Expand description
A value with dimensionality
Tuple Fields§
§0: VImplementations§
source§impl<V, const UNITS: SI> Quantity<V, UNITS>
impl<V, const UNITS: SI> Quantity<V, UNITS>
sourcepub fn powi<const EXP: i32>(self) -> Quantity<V::Output, { _ }>
pub fn powi<const EXP: i32>(self) -> Quantity<V::Output, { _ }>
Take the power to an integer
assert_eq!(Quantity::<f64, { meter }>(2.).powi::<2>(), Quantity::<f64, { meter.powi(2) }>(4.));sourcepub fn powf<const EXP: (i32, u32)>(self) -> Quantity<V::Output, { _ }>
pub fn powf<const EXP: (i32, u32)>(self) -> Quantity<V::Output, { _ }>
Take the power to a fraction
assert_eq!(Quantity::<f64, { meter }>(4.).powf::<{(1, 2)}>(), Quantity::<f64, { meter.powf((1, 2)) }>(2.));sourcepub fn convert_to<const NEW_UNITS: SI>(self) -> Quantity<V, NEW_UNITS>
pub fn convert_to<const NEW_UNITS: SI>(self) -> Quantity<V, NEW_UNITS>
Convert a Quantity to a different set of units
Okay
// Converting minutes to seconds
assert_eq!(Quantity::<f64, { minute }>(1.).convert_to::<{ second }>(), Quantity::<f64, { second }>(60.))Broken
// Can't convert meters to seconds
assert_eq!(Quantity::<f64, { meter }>(1.).convert_to::<{ second }>(), Quantity::<f64, { second }>(1.))source§impl<V: Display, const UNITS: SI> Quantity<V, UNITS>
impl<V: Display, const UNITS: SI> Quantity<V, UNITS>
sourcepub fn write_as<const AS: &'static str>(
&self,
f: &mut Formatter<'_>
) -> Result<(), Error>
pub fn write_as<const AS: &'static str>( &self, f: &mut Formatter<'_> ) -> Result<(), Error>
Write the quantity to a formatter using the given units. Must be parseable by crate::si.
For copy/paste purposes: ⋅
sourcepub fn format_as<const AS: &'static str>(&self) -> String
pub fn format_as<const AS: &'static str>(&self) -> String
Format the quantity using the given units. Must be parseable by crate::si.
Multiplication symbol for copy/paste purposes: ⋅
Okay:
assert_eq!(Quantity::<f64, newton>(1.).format_as::<"N">(), "1 N");Broken:
assert_eq!(Quantity::<f64, newton>(1.).format_as::<"K">(), "1 K");Trait Implementations§
source§impl<R, O, T: Add<R, Output = O>, const UNITS: SI> Add<Quantity<R, UNITS>> for Quantity<T, UNITS>
impl<R, O, T: Add<R, Output = O>, const UNITS: SI> Add<Quantity<R, UNITS>> for Quantity<T, UNITS>
Units must be the same for addition
source§impl<R, T: AddAssign<R>, const UNITS: SI> AddAssign<Quantity<R, UNITS>> for Quantity<T, UNITS>
impl<R, T: AddAssign<R>, const UNITS: SI> AddAssign<Quantity<R, UNITS>> for Quantity<T, UNITS>
Units must be the same for addition
source§fn add_assign(&mut self, rhs: Quantity<R, UNITS>)
fn add_assign(&mut self, rhs: Quantity<R, UNITS>)
+= operation. Read moresource§impl<V> Deref for Quantity<V, DIMENSIONLESS>
impl<V> Deref for Quantity<V, DIMENSIONLESS>
Arbitrary math operations are only reasonable on dimensionless values
source§impl<V> DerefMut for Quantity<V, DIMENSIONLESS>
impl<V> DerefMut for Quantity<V, DIMENSIONLESS>
Arbitrary math operations are only reasonable on dimensionless values
source§impl<R, O, T: Div<R, Output = O>, const LHS_UNITS: SI, const RHS_UNITS: SI> Div<Quantity<R, RHS_UNITS>> for Quantity<T, LHS_UNITS>
impl<R, O, T: Div<R, Output = O>, const LHS_UNITS: SI, const RHS_UNITS: SI> Div<Quantity<R, RHS_UNITS>> for Quantity<T, LHS_UNITS>
Division divides the units
source§impl<R, T: DivAssign<R>, const UNITS: SI> DivAssign<Quantity<R, DIMENSIONLESS>> for Quantity<T, UNITS>
impl<R, T: DivAssign<R>, const UNITS: SI> DivAssign<Quantity<R, DIMENSIONLESS>> for Quantity<T, UNITS>
Scaling by a dimensionless value is OK
source§fn div_assign(&mut self, rhs: Quantity<R, DIMENSIONLESS>)
fn div_assign(&mut self, rhs: Quantity<R, DIMENSIONLESS>)
/= operation. Read moresource§impl<R, O, T: Mul<R, Output = O>, const LHS_UNITS: SI, const RHS_UNITS: SI> Mul<Quantity<R, RHS_UNITS>> for Quantity<T, LHS_UNITS>
impl<R, O, T: Mul<R, Output = O>, const LHS_UNITS: SI, const RHS_UNITS: SI> Mul<Quantity<R, RHS_UNITS>> for Quantity<T, LHS_UNITS>
Multiplications multiplies the units
source§impl<R, T: MulAssign<R>, const UNITS: SI> MulAssign<Quantity<R, DIMENSIONLESS>> for Quantity<T, UNITS>
impl<R, T: MulAssign<R>, const UNITS: SI> MulAssign<Quantity<R, DIMENSIONLESS>> for Quantity<T, UNITS>
Scaling by a dimensionless value is OK
source§fn mul_assign(&mut self, rhs: Quantity<R, DIMENSIONLESS>)
fn mul_assign(&mut self, rhs: Quantity<R, DIMENSIONLESS>)
*= operation. Read moresource§impl<V: Ord, const UNITS: SI> Ord for Quantity<V, UNITS>
impl<V: Ord, const UNITS: SI> Ord for Quantity<V, UNITS>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<V: PartialEq, const UNITS: SI> PartialEq for Quantity<V, UNITS>
impl<V: PartialEq, const UNITS: SI> PartialEq for Quantity<V, UNITS>
source§impl<V: PartialOrd, const UNITS: SI> PartialOrd for Quantity<V, UNITS>
impl<V: PartialOrd, const UNITS: SI> PartialOrd for Quantity<V, UNITS>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl<R, O, T: Sub<R, Output = O>, const UNITS: SI> Sub<Quantity<R, UNITS>> for Quantity<T, UNITS>
impl<R, O, T: Sub<R, Output = O>, const UNITS: SI> Sub<Quantity<R, UNITS>> for Quantity<T, UNITS>
Units must be the same for addition
source§impl<R, T: SubAssign<R>, const UNITS: SI> SubAssign<Quantity<R, UNITS>> for Quantity<T, UNITS>
impl<R, T: SubAssign<R>, const UNITS: SI> SubAssign<Quantity<R, UNITS>> for Quantity<T, UNITS>
Units must be the same for addition
source§fn sub_assign(&mut self, rhs: Quantity<R, UNITS>)
fn sub_assign(&mut self, rhs: Quantity<R, UNITS>)
-= operation. Read more