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
impl Quantity
Sourcepub fn of(value: Rational, unit: &Unit) -> Quantity
pub fn of(value: Rational, unit: &Unit) -> Quantity
A quantity of value of unit — normalized to the SI base (value·scale + offset).
Sourcepub fn scalar(value: Rational) -> Quantity
pub fn scalar(value: Rational) -> Quantity
A dimensionless quantity (a pure number) — for scaling.
Sourcepub fn si(magnitude: Rational, dimension: Dimension) -> Quantity
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).
Sourcepub fn magnitude_si(&self) -> &Rational
pub fn magnitude_si(&self) -> &Rational
The magnitude in the SI base unit.
pub fn dimension(&self) -> Dimension
Sourcepub fn in_unit(&self, unit: &Unit) -> Option<Rational>
pub fn in_unit(&self, unit: &Unit) -> Option<Rational>
The magnitude expressed IN unit — None if the dimensions differ (the forbidden cast).
Linear: (si)/scale; affine: (si − offset)/scale. 1 mile in feet is exactly 5280.
Sourcepub fn add(&self, other: &Quantity) -> Option<Quantity>
pub fn add(&self, other: &Quantity) -> Option<Quantity>
self + other — None unless the dimensions match (you cannot add Length to Mass).
Sourcepub fn sub(&self, other: &Quantity) -> Option<Quantity>
pub fn sub(&self, other: &Quantity) -> Option<Quantity>
self − other — None unless the dimensions match.
Sourcepub fn mul(&self, other: &Quantity) -> Quantity
pub fn mul(&self, other: &Quantity) -> Quantity
self × other — magnitudes multiply, dimensions combine (Length × Length = Area).
Sourcepub fn div(&self, other: &Quantity) -> Option<Quantity>
pub fn div(&self, other: &Quantity) -> Option<Quantity>
self ÷ other — magnitudes divide, dimensions subtract. None on a zero divisor magnitude.
Sourcepub fn in_best_unit<'a>(
&self,
ladder: &'a [Unit],
) -> Option<(Rational, &'a Unit)>
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.