Skip to main content

Unit

Enum Unit 

Source
pub enum Unit {
    Base(BaseUnit),
    Composite(CompositeUnit),
    Dimensionless {
        scale: f64,
    },
}
Expand description

A physical unit.

Units can be base units (like meter), named derived units (like newton), composite units (like m/s), or dimensionless.

§Examples

use iridium_units::prelude::*;

// Base units are Copy — use them directly
let mass = 10.0 * KG;

// Arithmetic creates composite units
let velocity_unit = KM / H;

// Check dimensions
assert_eq!((M / S).dimension(), (KM / H).dimension());

// Convert between compatible units
let speed = 100.0 * &(KM / H);
let in_ms = speed.to(M / S).unwrap();

Variants§

§

Base(BaseUnit)

A base irreducible unit (meter, second, kilogram, etc.)

§

Composite(CompositeUnit)

A composite unit from arithmetic operations (m/s, kg·m/s², etc.)

§

Dimensionless

Dimensionless with a scale factor.

Fields

§scale: f64

Multiplicative scale factor (1.0 for pure dimensionless).

Implementations§

Source§

impl Unit

Source

pub fn dimensionless() -> Self

Create a dimensionless unit with scale 1.

Source

pub fn dimensionless_scaled(scale: f64) -> Self

Create a dimensionless unit with a scale factor.

Source

pub fn from_base(base: &BaseUnit) -> Self

Create a unit from a base unit.

Source

pub fn dimension(&self) -> Dimension

Get the dimension of this unit.

Source

pub fn scale(&self) -> f64

Get the scale factor relative to SI base units.

Source

pub fn is_dimensionless(&self) -> bool

Check if this unit is dimensionless.

Source

pub fn offset(&self) -> f64

Get the additive offset relative to the SI base unit.

Most units have offset 0.0. Offset units like Celsius (273.15) and Fahrenheit (459.67) use this for affine conversions. The formula is: SI_value = (value + offset) * scale.

Offsets are only defined for Unit::Base. Composite units are treated as interval units, so composing an offset base unit does not preserve its additive offset.

Source

pub fn has_offset(&self) -> bool

Check if this unit has an additive offset (e.g., Celsius, Fahrenheit).

Only true for Unit::Base values with a non-zero offset.

Source

pub fn to_si(&self, value: f64) -> f64

Convert a value in this unit to SI base units.

For pure scale units: value * scale For offset base units: (value + offset) * scale

Affine offsets are only applied for Unit::Base. Composite units are converted using scale alone (interval semantics).

Source

pub fn from_si(&self, si_value: f64) -> f64

Convert a value from SI base units to this unit.

For simple units: si_value / scale For offset units: si_value / scale - offset

Source

pub fn conversion_factor(&self, to: &Unit) -> UnitResult<f64>

Get the conversion factor to convert from this unit to another.

This returns a single multiplicative factor, which only works for units without additive offsets. For offset units like Celsius or Fahrenheit, use Quantity::to instead.

Returns Err if the units have incompatible dimensions or if either unit has an additive offset.

Source

pub fn to_composite(&self) -> CompositeUnit

Convert this unit to a composite representation.

Source

pub fn pow(&self, exp: impl Into<Rational16>) -> Unit

Raise this unit to a power.

Source

pub fn sqrt(&self) -> Unit

Take the square root of this unit.

Source

pub fn inv(&self) -> Unit

Invert this unit (raise to power -1).

Source

pub fn symbol(&self) -> String

Get the symbol/string representation for this unit.

Trait Implementations§

Source§

impl Clone for Unit

Source§

fn clone(&self) -> Unit

Returns a duplicate 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 Debug for Unit

Source§

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

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

impl Display for Unit

Source§

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

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

impl Div<&Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: &Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<&Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: &Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<&Unit> for Unit

Source§

type Output = Unit

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Unit) -> Unit

Performs the / operation. Read more
Source§

impl Div<BaseUnit> for &Unit

Source§

type Output = Unit

The resulting type after applying the / operator.
Source§

fn div(self, rhs: BaseUnit) -> Unit

Performs the / operation. Read more
Source§

impl Div<BaseUnit> for Unit

Source§

type Output = Unit

The resulting type after applying the / operator.
Source§

fn div(self, rhs: BaseUnit) -> Unit

Performs the / operation. Read more
Source§

impl Div<Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div<Unit> for &Unit

Source§

type Output = Unit

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Unit) -> Unit

Performs the / operation. Read more
Source§

impl Div<Unit> for BaseUnit

Source§

type Output = Unit

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Unit) -> Unit

Performs the / operation. Read more
Source§

impl Div<Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the / operator.
Source§

fn div(self, unit: Unit) -> Quantity

Performs the / operation. Read more
Source§

impl Div for &Unit

Source§

type Output = Unit

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Unit) -> Unit

Performs the / operation. Read more
Source§

impl Div for Unit

Source§

type Output = Unit

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Unit) -> Unit

Performs the / operation. Read more
Source§

impl From<&BaseUnit> for Unit

Source§

fn from(b: &BaseUnit) -> Unit

Converts to this type from the input type.
Source§

impl From<&Unit> for Unit

Source§

fn from(u: &Unit) -> Unit

Converts to this type from the input type.
Source§

impl From<BaseUnit> for Unit

Source§

fn from(b: BaseUnit) -> Unit

Converts to this type from the input type.
Source§

impl FromStr for Unit

Source§

type Err = UnitError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Mul<&Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: &Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<&Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: &Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<&Unit> for Unit

Source§

type Output = Unit

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Unit) -> Unit

Performs the * operation. Read more
Source§

impl Mul<&Unit> for f64

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: &Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<BaseUnit> for &Unit

Source§

type Output = Unit

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: BaseUnit) -> Unit

Performs the * operation. Read more
Source§

impl Mul<BaseUnit> for Unit

Source§

type Output = Unit

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: BaseUnit) -> Unit

Performs the * operation. Read more
Source§

impl Mul<Unit> for &Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<Unit> for &Unit

Source§

type Output = Unit

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Unit) -> Unit

Performs the * operation. Read more
Source§

impl Mul<Unit> for BaseUnit

Source§

type Output = Unit

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Unit) -> Unit

Performs the * operation. Read more
Source§

impl Mul<Unit> for Quantity

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul<Unit> for f64

Source§

type Output = Quantity

The resulting type after applying the * operator.
Source§

fn mul(self, unit: Unit) -> Quantity

Performs the * operation. Read more
Source§

impl Mul for &Unit

Source§

type Output = Unit

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Unit) -> Unit

Performs the * operation. Read more
Source§

impl Mul for Unit

Source§

type Output = Unit

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Unit) -> Unit

Performs the * operation. Read more
Source§

impl PartialEq for Unit

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 StructuralPartialEq for Unit

Auto Trait Implementations§

§

impl Freeze for Unit

§

impl RefUnwindSafe for Unit

§

impl Send for Unit

§

impl Sync for Unit

§

impl Unpin for Unit

§

impl UnsafeUnpin for Unit

§

impl UnwindSafe for Unit

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> ToString for T
where T: Display + ?Sized,

Source§

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

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.