Unit

Struct Unit 

Source
pub struct Unit<ExponentsType = DynDimensionExponents> {
    pub name: &'static str,
    pub symbols: &'static [&'static str],
    pub scale: ScaleExponents,
    pub conversion_factor: f64,
    pub affine_offset: f64,
    pub exponents: ExponentsType,
    pub system: System,
}
Expand description

A unit.

Each unit is assigned a “storage unit”. The storage unit is the unit of a value stored with this unit. Storage units are always a well defined multiple of an SI base unit.

The logarithmic scale encoding in the type system uses only powers of 2, 3, 5, and pi. This means that the storage unit must be a multiple of an SI base unit and a product of powers of 2, 3, 5, and pi. For example,

  • “kilometer” has a scale factor of 10^3 = 2^3 * 5^3
  • “degree” has a scale factor of π/180 = 2^-2 * 3^-2 * 5^-1 * pi^1

Units that differ from identity in their conversion_factor are “non-storage” units. Non-storage units are not stored in their native scale; upon declaration they are converted to their “nearest neighbor” power-of-10 multiple of a SI base unit. For example

  • “inch” is multiplied by 2.54 and stored as “centimeters”
  • “yard” is multiplied by 0.9144 and stored as “meters”
  • “mile” is multiplied by 1.609344 and stored as “kilometers”

Fields§

§name: &'static str

Name of the unit.

§symbols: &'static [&'static str]

Symbols associated with the unit.

Symbols are also used for lookup, so they must be unique within the unit system.

Because SI has a systematic prefixing semantics, symbols must be kept from colliding not just with the SI base symbols, but with any legal prefixing thereof. “SI base symbols” are defined as the first symbol of the first unit in each dimension, by declaration order.

§scale: ScaleExponents

Base unit per storage unit.

To convert from a value in the storage unit to a SI base unit value, multiply it by scale.

For example, a scale of 1000 in dimension length would store the value as kilometers. If we have the value 123 stored then that means in meters it is 123 * 1000 = 123,000 meters.

§conversion_factor: f64

Storage unit per this unit.

Difference from identity canonically identifies a unit as a “non-storage” unit. Non-storage units are not stored in their native scale, as arbitrary float scaling factors are not part of the logarithmic scale encoding in the type system.

To convert from a value in this unit to a storage unit value, multiply it by conversion_factor.

For example, the “inch” unit has a conversion_factor of 2.54, which means that a value of 1 in inches is stored as 2.54 (accordingly, the scale is 10^-2).

Non-storage units are always given a storage scale of their “nearest neighbor” power-of-10 multiple of a SI base unit. For example,

  • “inch” is multiplied by 2.54 and stored as “centimeters”
  • “yard” is multiplied by 0.9144 and stored as “meters”
  • “mile” is multiplied by 1.609344 and stored as “kilometers”
§affine_offset: f64

The “zero point offset” of this unit’s measurement scale from the numerical zero of the storage unit.

To convert from a value in the unit to the storage unit, add the affine offset to the value.

For example, the “celsius” unit has an affine offset of 273.15, which means that a value of 0 in celsius is stored as 273.15 in kelvin.

§exponents: ExponentsType

Dimensional exponent vector of the dimension this unit belongs to.

§system: System

Which “unit system” this unit belongs to. This determines the name of the declarator trait in which this unit’s nominal declarators will live (e.g. “ImperialLength” or “MetricMass”).

Implementations§

Source§

impl<ExponentsType> Unit<ExponentsType>

Source

pub fn has_conversion(&self) -> bool

Check if the unit has a conversion factor.

Source

pub fn has_affine_offset(&self) -> bool

Check if the unit has an affine offset.

Source§

impl<const MASS_EXP: i16, const LENGTH_EXP: i16, const TIME_EXP: i16, const CURRENT_EXP: i16, const TEMPERATURE_EXP: i16, const AMOUNT_EXP: i16, const LUMINOSITY_EXP: i16, const ANGLE_EXP: i16> Unit<TypeDimensionExponents<_M<MASS_EXP>, _L<LENGTH_EXP>, _T<TIME_EXP>, _I<CURRENT_EXP>, <TEMPERATURE_EXP>, _N<AMOUNT_EXP>, _J<LUMINOSITY_EXP>, _A<ANGLE_EXP>>>

Source

pub const fn erase(&self) -> Unit

Source§

impl Unit

Lists of units.

Source

pub const BASES: [Self; 8]

Source

pub fn multiple_to_base_unit( multiple: &str, ) -> Option<(&'static Self, Option<&'static SiPrefix>)>

Convert multiple names to their base unit.

This function takes a scale type name (like “Kilogram”, “Millimeter”) and returns the corresponding base unit (like “g”, “m”).

Source

pub fn find_base(symbol: &str) -> Option<&'static Self>

Find base unit by symbol.

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<0>, _T<0>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Mass

Source

pub const GRAM: Self

Source

pub const GRAIN: Self

Source

pub const CARAT: Self

Source

pub const OUNCE: Self

Source

pub const TROY_OUNCE: Self

Source

pub const TROY_POUND: Self

Source

pub const POUND: Self

Source

pub const STONE: Self

Source

pub const SLUG: Self

Source

pub const TON: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<1>, _T<0>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Length

Source

pub const METER: Self

Source

pub const INCH: Self

Source

pub const FOOT: Self

Source

pub const YARD: Self

Source

pub const FATHOM: Self

Source

pub const FURLONG: Self

Source

pub const MILE: Self

Source

pub const NAUTICAL_MILE: Self

Source

pub const ASTRONOMICAL_UNIT: Self

Source

pub const LIGHT_YEAR: Self

Source

pub const PARSEC: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<1>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Time

Source

pub const SECOND: Self

Source

pub const MINUTE: Self

Source

pub const HOUR: Self

Source

pub const DAY: Self

Source

pub const WEEK: Self

Source

pub const MONTH: Self

Source

pub const YEAR: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<0>, _I<1>, <0>, _N<0>, _J<0>, _A<0>>>

Current

Source

pub const AMPERE: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<0>, _I<0>, <1>, _N<0>, _J<0>, _A<0>>>

Temperature

Source

pub const KELVIN: Self

Source

pub const CELSIUS: Self

Source

pub const RANKINE: Self

Source

pub const FAHRENHEIT: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<0>, _I<0>, <0>, _N<1>, _J<0>, _A<0>>>

Amount

Source

pub const MOLE: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<0>, _I<0>, <0>, _N<0>, _J<0>, _A<1>>>

Angle

Source

pub const RADIAN: Self

Source

pub const DEGREE: Self

Source

pub const GRADIAN: Self

Source

pub const TURN: Self

Source

pub const ARCMINUTE: Self

Source

pub const ARCSECOND: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<-1>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Frequency

Source

pub const HERTZ: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<1>, _T<-2>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Force

Source

pub const NEWTON: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<2>, _T<-2>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Energy and work

Source

pub const JOULE: Self

Source

pub const NEWTON_METER: Self

Source

pub const ELECTRONVOLT: Self

Source

pub const ERG: Self

Source

pub const CALORIE: Self

Source

pub const FOOT_POUND: Self

Source

pub const KILOWATT_HOUR: Self

Source

pub const THERM: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<2>, _T<-3>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Power

Source

pub const WATT: Self

Source

pub const HORSEPOWER: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<-1>, _T<-2>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Pressure

Source

pub const PASCAL: Self

Source

pub const TORR: Self

Source

pub const PSI: Self

Source

pub const BAR: Self

Source

pub const ATMOSPHERE: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<1>, _I<1>, <0>, _N<0>, _J<0>, _A<0>>>

Electric charge

Source

pub const COULOMB: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<2>, _T<-3>, _I<-1>, <0>, _N<0>, _J<0>, _A<0>>>

Electric potential

Source

pub const VOLT: Self

Source§

impl Unit<TypeDimensionExponents<_M<-1>, _L<-2>, _T<4>, _I<2>, <0>, _N<0>, _J<0>, _A<0>>>

Capacitance

Source

pub const FARAD: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<2>, _T<-3>, _I<-2>, <0>, _N<0>, _J<0>, _A<0>>>

Source

pub const OHM: Self

Source§

impl Unit<TypeDimensionExponents<_M<-1>, _L<-2>, _T<3>, _I<2>, <0>, _N<0>, _J<0>, _A<0>>>

Electric conductance

Source

pub const SIEMENS: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<2>, _T<-2>, _I<-2>, <0>, _N<0>, _J<0>, _A<0>>>

Inductance

Source

pub const HENRY: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<0>, _T<-2>, _I<-1>, <0>, _N<0>, _J<0>, _A<0>>>

Magnetic field

Source

pub const TESLA: Self

Source

pub const GAUSS: Self

Source§

impl Unit<TypeDimensionExponents<_M<1>, _L<2>, _T<-2>, _I<-1>, <0>, _N<0>, _J<0>, _A<0>>>

Magnetic flex

Source

pub const WEBER: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<-2>, _T<0>, _I<0>, <0>, _N<0>, _J<1>, _A<0>>>

Illuminance

Source

pub const LUX: Self

Source

pub const LUMEN: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<0>, _I<0>, <0>, _N<0>, _J<1>, _A<0>>>

Luminous intensity

Source

pub const CANDELA: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<2>, _T<-1>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Kinematic viscosity

Source

pub const STOKES: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<2>, _T<0>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Area

Source

pub const HECTARE: Self

Source

pub const ACRE: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<3>, _T<0>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Volume

Source

pub const LITER: Self

Source

pub const GALLON_US: Self

Source

pub const GALLON_UK: Self

Source

pub const QUART_US: Self

Source

pub const QUART_UK: Self

Source

pub const PINT_US: Self

Source

pub const PINT_UK: Self

Source

pub const CUP_US: Self

Source

pub const CUP_UK: Self

Source

pub const FLUID_OUNCE_US: Self

Source

pub const FLUID_OUNCE_UK: Self

Source

pub const METRIC_FLUID_OUNCE: Self

Source

pub const TABLESPOON_US: Self

Source

pub const TABLESPOON_UK: Self

Source

pub const TEASPOON_US: Self

Source

pub const TEASPOON_UK: Self

Source

pub const BUSHEL: Self

Source§

impl Unit<TypeDimensionExponents<_M<0>, _L<0>, _T<0>, _I<0>, <0>, _N<0>, _J<0>, _A<0>>>

Dimensionless

Source

pub const DIMENSIONLESS: Self

Trait Implementations§

Source§

impl<ExponentsType: Clone> Clone for Unit<ExponentsType>

Source§

fn clone(&self) -> Unit<ExponentsType>

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<ExponentsType: Debug> Debug for Unit<ExponentsType>

Source§

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

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

impl<ExponentsType: PartialEq> PartialEq for Unit<ExponentsType>

Source§

fn eq(&self, other: &Unit<ExponentsType>) -> 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<ExponentsType: Copy> Copy for Unit<ExponentsType>

Source§

impl<ExponentsType> StructuralPartialEq for Unit<ExponentsType>

Auto Trait Implementations§

§

impl<ExponentsType> Freeze for Unit<ExponentsType>
where ExponentsType: Freeze,

§

impl<ExponentsType> RefUnwindSafe for Unit<ExponentsType>
where ExponentsType: RefUnwindSafe,

§

impl<ExponentsType> Send for Unit<ExponentsType>
where ExponentsType: Send,

§

impl<ExponentsType> Sync for Unit<ExponentsType>
where ExponentsType: Sync,

§

impl<ExponentsType> Unpin for Unit<ExponentsType>
where ExponentsType: Unpin,

§

impl<ExponentsType> UnwindSafe for Unit<ExponentsType>
where ExponentsType: 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> 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.