pub struct UnitTable { /* private fields */ }Expand description
A checked unit table, ready to convert. Built with Units::build;
plain data, Clone and Debug.
Implementations§
Source§impl UnitTable
impl UnitTable
Sourcepub fn contains(&self, code: &str) -> bool
pub fn contains(&self, code: &str) -> bool
Whether code is a unit of this table.
use renox::quantity::Units;
let t = Units::new().base("kg").unit("g", "kg", "0.001").build().unwrap();
assert!(t.contains("g"));
assert!(!t.contains("lb"));Sourcepub fn convert<const SCALE: u32>(
&self,
q: Quantity<SCALE>,
from: &str,
to: &str,
rounding: Rounding,
) -> Result<Quantity<SCALE>, QuantityError>
pub fn convert<const SCALE: u32>( &self, q: Quantity<SCALE>, from: &str, to: &str, rounding: Rounding, ) -> Result<Quantity<SCALE>, QuantityError>
Converts q from unit from to unit to at the same scale,
rounding with rounding when digits are lost. An unknown unit is
UnknownUnit, units of different bases are IncompatibleUnits,
a result that does not fit is Overflow.
use renox::money::Rounding;
use renox::quantity::{Quantity, QuantityError, Units};
let t = Units::new()
.base("kg")
.unit("g", "kg", "0.001")
.base("m")
.build()
.unwrap();
let q = Quantity::<3>::from_raw(1);
assert_eq!(t.convert(q, "g", "kg", Rounding::Down).unwrap().raw(), 0);
assert_eq!(t.convert(q, "g", "kg", Rounding::Up).unwrap().raw(), 1);
assert!(matches!(
t.convert(q, "kg", "m", Rounding::Up),
Err(QuantityError::IncompatibleUnits { .. })
));
assert!(matches!(
t.convert(q, "kg", "lb", Rounding::Up),
Err(QuantityError::UnknownUnit(_))
));Trait Implementations§
impl Eq for UnitTable
impl StructuralPartialEq for UnitTable
Auto Trait Implementations§
impl Freeze for UnitTable
impl RefUnwindSafe for UnitTable
impl Send for UnitTable
impl Sync for UnitTable
impl Unpin for UnitTable
impl UnsafeUnpin for UnitTable
impl UnwindSafe for UnitTable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more