Struct const_units::Quantity

source ·
pub struct Quantity<V, const UNITS: SI>(pub V);
Expand description

A value with dimensionality

Tuple Fields§

§0: V

Implementations§

source§

impl<V, const UNITS: SI> Quantity<V, UNITS>

source

pub fn powi<const EXP: i32>(self) -> Quantity<V::Output, { _ }>
where V: Pow<i32>,

Take the power to an integer

assert_eq!(Quantity::<f64, { meter }>(2.).powi::<2>(), Quantity::<f64, { meter.powi(2) }>(4.));
source

pub fn powf<const EXP: (i32, u32)>(self) -> Quantity<V::Output, { _ }>
where V: Pow<f64>,

Take the power to a fraction

assert_eq!(Quantity::<f64, { meter }>(4.).powf::<{(1, 2)}>(), Quantity::<f64, { meter.powf((1, 2)) }>(2.));
source

pub fn convert_to<const NEW_UNITS: SI>(self) -> Quantity<V, NEW_UNITS>
where Bool<{ _ }>: True, V: Mul<V, Output = V> + Div<V, Output = V> + NumCast,

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>

source

pub fn write_as<const AS: &'static str>( &self, f: &mut Formatter<'_> ) -> Result<(), Error>
where Bool<{ _ }>: True,

Write the quantity to a formatter using the given units. Must be parseable by crate::si.

For copy/paste purposes:

source

pub fn format_as<const AS: &'static str>(&self) -> String
where Bool<{ _ }>: True,

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>

Units must be the same for addition

§

type Output = Quantity<O, UNITS>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Quantity<R, UNITS>) -> Self::Output

Performs the + operation. Read more
source§

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>)

Performs the += operation. Read more
source§

impl<V: Clone, const UNITS: SI> Clone for Quantity<V, UNITS>

source§

fn clone(&self) -> Quantity<V, UNITS>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<V: Debug, const UNITS: SI> Debug for Quantity<V, UNITS>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<V> Deref for Quantity<V, DIMENSIONLESS>

Arbitrary math operations are only reasonable on dimensionless values

§

type Target = V

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<V> DerefMut for Quantity<V, DIMENSIONLESS>

Arbitrary math operations are only reasonable on dimensionless values

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<V: Display, const UNITS: SI> Display for Quantity<V, UNITS>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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>
where Quantity<O, { _ }>: Sized,

Division divides the units

§

type Output = Quantity<O, { SI::div(LHS_UNITS, RHS_UNITS) }>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Quantity<R, RHS_UNITS>) -> Self::Output

Performs the / operation. Read more
source§

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>)

Performs the /= operation. Read more
source§

impl<V, const UNITS: SI> From<Quantity<V, UNITS>> for DynQuantity<V>

source§

fn from(value: Quantity<V, UNITS>) -> Self

Converts to this type from the input type.
source§

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>
where Quantity<O, { _ }>: Sized,

Multiplications multiplies the units

§

type Output = Quantity<O, { SI::mul(LHS_UNITS, RHS_UNITS) }>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Quantity<R, RHS_UNITS>) -> Self::Output

Performs the * operation. Read more
source§

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>)

Performs the *= operation. Read more
source§

impl<V: Ord, const UNITS: SI> Ord for Quantity<V, UNITS>

source§

fn cmp(&self, other: &Quantity<V, UNITS>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<V: PartialEq, const UNITS: SI> PartialEq for Quantity<V, UNITS>

source§

fn eq(&self, other: &Quantity<V, UNITS>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<V: PartialOrd, const UNITS: SI> PartialOrd for Quantity<V, UNITS>

source§

fn partial_cmp(&self, other: &Quantity<V, UNITS>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

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

§

type Output = Quantity<O, UNITS>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Quantity<R, UNITS>) -> Self::Output

Performs the - operation. Read more
source§

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>)

Performs the -= operation. Read more
source§

impl<V, const UNITS: SI> TryFrom<DynQuantity<V>> for Quantity<V, UNITS>

§

type Error = InconsistentUnits

The type returned in the event of a conversion error.
source§

fn try_from(value: DynQuantity<V>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<V: Copy, const UNITS: SI> Copy for Quantity<V, UNITS>

source§

impl<V: Eq, const UNITS: SI> Eq for Quantity<V, UNITS>

source§

impl<V, const UNITS: SI> StructuralEq for Quantity<V, UNITS>

source§

impl<V, const UNITS: SI> StructuralPartialEq for Quantity<V, UNITS>

Auto Trait Implementations§

§

impl<V, const UNITS: SI> RefUnwindSafe for Quantity<V, UNITS>
where V: RefUnwindSafe,

§

impl<V, const UNITS: SI> Send for Quantity<V, UNITS>
where V: Send,

§

impl<V, const UNITS: SI> Sync for Quantity<V, UNITS>
where V: Sync,

§

impl<V, const UNITS: SI> Unpin for Quantity<V, UNITS>
where V: Unpin,

§

impl<V, const UNITS: SI> UnwindSafe for Quantity<V, UNITS>
where V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.