Skip to main content

Unit

Struct Unit 

Source
#[repr(C)]
pub struct Unit { pub second: i32, pub meter: i32, pub kilogram: i32, pub ampere: i32, pub kelvin: i32, pub mol: i32, pub candela: i32, }
Expand description

Struct representing a unit of measurement in the SI system via the exponents of the base units. The unit is purely defined by the values of its fields, meaning that it can change at runtime. The struct implements basic arithmetic functions such as multiplication and division (via the Mul, MulAssign, Div, DivAssign traits), exponentiation (Unit::powi) and a fallible version of root calculation (Unit::try_nthroot).

§Serialization and deserialization

If the serde feature is enabled, this struct can be serialized and deserialized. Serialization creates the “standard” serde representation one would expect from the Serialize macro.

An Unit can be deserialized from both its “standard” serialized representation and from a PredefUnit variant:

use dyn_quantity::Unit;

// Direct deserialization
let str = "---\nsecond: -3\nmeter: 2\nkilogram: 1\nampere: -1\nkelvin: 0\nmol: 0\ncandela: 0";
let unit_direct: Unit = serde_yaml::from_str(str).unwrap();

// Deserialization from PredefUnit
let str = "ElectricVoltage";
let unit_predef: Unit = serde_yaml::from_str(str).unwrap();
assert_eq!(unit_predef, unit_direct);

Fields§

§second: i32

Exponent for the SI base unit of time.

§meter: i32

Exponent for the SI base unit of length.

§kilogram: i32

Exponent for the SI base unit of mass.

§ampere: i32

Exponent for the SI base unit of electrical current.

§kelvin: i32

Exponent for the SI base unit of temperature.

§mol: i32

Exponent for the SI base unit of amount of substance.

§candela: i32

Exponent for the SI base unit of luminous intensity

Implementations§

Source§

impl Unit

Source

pub fn powi(self, n: i32) -> Unit

Raises self to an integer power.

§Examples
use dyn_quantity::Unit;

let exponents = Unit::from([0, 1, 0, 2, 0, -2, 0]);
let array: [i32; 7] = exponents.powi(2).into();
assert_eq!(array, [0, 2, 0, 4, 0, -4, 0]);
Source

pub fn try_nthroot(self, n: i32) -> Result<Unit, RootError>

Tries to calculate the nth root of self. This operation fails if any of the exponents is not divisible by n.

§Examples
use dyn_quantity::Unit;

let unit = Unit::from([0, 2, 0, 2, 0, -4, 0]);

// It is possible to calculate the square root:
let array: [i32; 7] = unit.clone().try_nthroot(2).unwrap().into();
assert_eq!(array, [0, 1, 0, 1, 0, -2, 0]);

// But not the cubic root (not all exponents are divisible by 3):
assert!(unit.try_nthroot(3).is_err());
Source

pub fn is_dimensionless(&self) -> bool

Returns whether Unit is dimensionless (all exponents are zero) or not.

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<(), Error>

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

impl Default for Unit

Source§

fn default() -> Unit

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Unit

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Unit, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Unit

Source§

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

Formats the value using the given formatter. 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 as Div>::Output

Performs the / operation. Read more
Source§

impl DivAssign for Unit

Source§

fn div_assign(&mut self, rhs: Unit)

Performs the /= operation. Read more
Source§

impl From<[i32; 7]> for Unit

Source§

fn from(array: [i32; 7]) -> Unit

Converts an array of seven i32 values into Unit.

The individual array elements are interpreted as follows:

  • array[0]: Exponent of second
  • array[1]: Exponent of meter
  • array[2]: Exponent of kilogram
  • array[3]: Exponent of ampere
  • array[4]: Exponent of kelvin
  • array[5]: Exponent of mol
  • array[6]: Exponent of candela
Source§

impl From<PredefUnit> for Unit

Source§

fn from(value: PredefUnit) -> Unit

Converts to this type from the input type.
Source§

impl From<Unit> for [i32; 7]

Source§

fn from(value: Unit) -> [i32; 7]

Converts an Unit into an array of seven i32.

The exponents are put into the array in the following order:

  • array[0]: Exponent of second
  • array[1]: Exponent of meter
  • array[2]: Exponent of kilogram
  • array[3]: Exponent of ampere
  • array[4]: Exponent of kelvin
  • array[5]: Exponent of mol
  • array[6]: Exponent of candela
Source§

impl Hash for Unit

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. 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 as Mul>::Output

Performs the * operation. Read more
Source§

impl MulAssign for Unit

Source§

fn mul_assign(&mut self, rhs: 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 Serialize for Unit

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Unit

Source§

impl Eq for Unit

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> SendAlias for T

Source§

impl<T> SyncAlias for T