Skip to main content

Quantity

Struct Quantity 

Source
pub struct Quantity { /* private fields */ }
Expand description

An exact quantity: a magnitude in the SI base unit, tagged with its dimension.

Implementations§

Source§

impl Quantity

Source

pub fn of(value: Rational, unit: &Unit) -> Quantity

A quantity of value of unit — normalized to the SI base (value·scale + offset).

Source

pub fn scalar(value: Rational) -> Quantity

A dimensionless quantity (a pure number) — for scaling.

Source

pub fn si(magnitude: Rational, dimension: Dimension) -> Quantity

A quantity given directly by its SI-base magnitude and dimension — used for physical constants and any value whose dimension has no single named unit (e.g. the gas constant).

Source

pub fn magnitude_si(&self) -> &Rational

The magnitude in the SI base unit.

Source

pub fn dimension(&self) -> Dimension

Source

pub fn in_unit(&self, unit: &Unit) -> Option<Rational>

The magnitude expressed IN unitNone if the dimensions differ (the forbidden cast). Linear: (si)/scale; affine: (si − offset)/scale. 1 mile in feet is exactly 5280.

Source

pub fn add(&self, other: &Quantity) -> Option<Quantity>

self + otherNone unless the dimensions match (you cannot add Length to Mass).

Source

pub fn sub(&self, other: &Quantity) -> Option<Quantity>

self − otherNone unless the dimensions match.

Source

pub fn mul(&self, other: &Quantity) -> Quantity

self × other — magnitudes multiply, dimensions combine (Length × Length = Area).

Source

pub fn div(&self, other: &Quantity) -> Option<Quantity>

self ÷ other — magnitudes divide, dimensions subtract. None on a zero divisor magnitude.

Source

pub fn in_best_unit<'a>( &self, ladder: &'a [Unit], ) -> Option<(Rational, &'a Unit)>

Choose the most human-readable unit from ladder and return (magnitude_in_it, unit). The winner is the unit whose magnitude has the smallest absolute value that is still ≥ 1 (so 1500 m → (1.5, km), 0.5 km → (500, m)); if every candidate is below 1 the largest magnitude (closest to 1 from below) wins. Units of a different dimension are skipped, so a ladder with no same-dimension unit yields None. Exact — the conversion never rounds, and Quantity::of(mag, unit) reconstructs self. This is Design Law #3 (auto-scale to the most human unit), kept pure at the base layer so the interpreter Showable and AOT both reuse it.

Trait Implementations§

Source§

impl Clone for Quantity

Source§

fn clone(&self) -> Quantity

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Quantity

Source§

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

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

impl PartialEq for Quantity

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Quantity

Source§

impl StructuralPartialEq for Quantity

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.