Struct const_units::SI

source ·
pub struct SI {
    pub scale: (i128, u128),
    pub s: (i32, u32),
    pub m: (i32, u32),
    pub kg: (i32, u32),
    pub A: (i32, u32),
    pub K: (i32, u32),
    pub mol: (i32, u32),
    pub cd: (i32, u32),
}
Expand description

Represents SI units

Each field contains the exponent of the corresponding dimension. Exponents are fractions where the first element of the tuple is the numerator and the second is the denominator. Ensure the fractions are fully simplified if you’re creating an instance directly.

Fields§

§scale: (i128, u128)

A scale factor - used for units that are constant multiples of other units; ie. minutes or kilometers

§s: (i32, u32)

Seconds

§m: (i32, u32)

Meters

§kg: (i32, u32)

Kilograms

§A: (i32, u32)

Amperes

§K: (i32, u32)

Kelvin

§mol: (i32, u32)

Moles

§cd: (i32, u32)

Candelas

Implementations§

source§

impl SI

source

pub const fn scale_by(self, scale: (i128, u128)) -> Self

Multiply the scale factor by a given constant

assert_eq!(second.scale_by((60, 1)), minute)
source

pub const fn mul(self, rhs: Self) -> Self

Multiply SI units

assert_eq!(newton.mul(meter), joule)
source

pub const fn div(self, rhs: Self) -> Self

Divide SI units

assert_eq!(joule.div(second), watt);
source

pub const fn powi(self, v: i32) -> Self

Raise SI units to an integer power

assert_eq!(second.powi(-1), hertz);
source

pub const fn powf(self, v: (i32, u32)) -> Self

Raise SI units to a fractional power

assert_eq!(meter.powi(2).powf((1, 2)), meter);
Panics

Panics if scale can’t be raised to the given power without losing precision

meter.scale_by((2, 1)).powf((1, 2)); // `2` isn't a square number
source

pub const fn checked_powf(self, v: (i32, u32)) -> Option<Self>

A checked version of pow

assert_eq!(meter.powi(2).checked_powf((1, 2)), Some(meter));
assert_eq!(meter.scale_by((2, 1)).checked_powf((1, 2)), None);
source

pub const fn const_eq(self, other: SI) -> bool

Compare units in constant functions

source

pub const fn same_dimension(self, other: SI) -> bool

Determine whether two units measure the same dimension

use const_units::si;
assert!(si("min").same_dimension(si("s")));
assert!(!si("cd").same_dimension(si("s")));

Trait Implementations§

source§

impl Clone for SI

source§

fn clone(&self) -> SI

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 Debug for SI

source§

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

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

impl Display for SI

source§

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

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

impl PartialEq for SI

source§

fn eq(&self, other: &SI) -> 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 ConstParamTy for SI

source§

impl Copy for SI

source§

impl Eq for SI

source§

impl StructuralEq for SI

source§

impl StructuralPartialEq for SI

Auto Trait Implementations§

§

impl RefUnwindSafe for SI

§

impl Send for SI

§

impl Sync for SI

§

impl Unpin for SI

§

impl UnwindSafe for SI

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.