Struct EngineeringQuantity

Source
pub struct EngineeringQuantity<T: EQSupported<T>> { /* private fields */ }
Expand description

A helper type for expressing numbers in engineering notation.

These numbers may be converted to and from integers, strings, and num_rational::Ratio. They may also be converted to floats.

§Type parameter

The type parameter T is the underlying storage type used for the significand of the number. That is to say, an EngineeringQuantity<u32> uses a u32 to store the numeric part.

Implementations§

Source§

impl<T: EQSupported<T>> EngineeringQuantity<T>

Source

pub fn with_precision( &self, max_significant_figures: usize, ) -> DisplayAdapter<T>

Creates a standard DisplayAdapter for this object, with the given precision.

use engineering_repr::EngineeringQuantity as EQ;
let ee = EQ::<i32>::from(1234567);
assert_eq!(ee.with_precision(2).to_string(), "1.2M");
Source

pub fn rkm_with_precision( &self, max_significant_figures: usize, ) -> DisplayAdapter<T>

Creates an RKM DisplayAdapter for this object in RKM mode, with the given precision.

use engineering_repr::EngineeringQuantity as EQ;
let ee = EQ::<i32>::from(1234567);
assert_eq!(ee.rkm_with_precision(2).to_string(), "1M2");
Source

pub fn with_strict_precision( &self, max_significant_figures: usize, ) -> DisplayAdapter<T>

Creates a DisplayAdapter for this object, with strict precision. The requested digits will always be output, even trailing zeroes.

use engineering_repr::EngineeringQuantity as EQ;
let ee = EQ::<i32>::from(1_200);
assert_eq!(ee.with_strict_precision(3).to_string(), "1.20k");
Source§

impl<T: EQSupported<T>> EngineeringQuantity<T>

Source

pub fn from_raw(significand: T, exponent: i8) -> Result<Self, Error>

Raw constructor from component parts

Construction fails if the number would overflow the storage type T.

Source

pub fn to_raw(self) -> (T, i8)

Raw accessor to retrieve the component parts

Source§

impl<T: EQSupported<T>> EngineeringQuantity<T>

Source

pub fn convert<U: EQSupported<U> + From<T>>(&self) -> EngineeringQuantity<U>

Conversion to a different storage type. If you can convert from type A to type B, then you can convert from EngineeringQuantity<A> to EngineeringQuantity<B>.

use engineering_repr::EngineeringQuantity as EQ;
let q = EQ::from_raw(42u32, 0).unwrap();
let q2 = q.convert::<u64>();
assert_eq!(q2.to_raw(), (42u64, 0));
Source

pub fn try_convert<U: EQSupported<U> + TryFrom<T>>( &self, ) -> Result<EngineeringQuantity<U>, Error>

Fallible conversion to a different storage type.

Conversion fails if the number cannot be represented in the the destination storage type.

type EQ = engineering_repr::EngineeringQuantity<u32>;
let million = EQ::from_raw(1, 2).unwrap();
let r1 = million.try_convert::<u32>().unwrap();
let r2 = million.try_convert::<u16>().expect_err("overflow"); // Overflow, because 1_000_000 won't fit into a u16
Source

pub fn normalise(self) -> Self

Scales the number to remove any unnecessary groups of trailing zeroes.

Trait Implementations§

Source§

impl<T: Clone + EQSupported<T>> Clone for EngineeringQuantity<T>

Source§

fn clone(&self) -> EngineeringQuantity<T>

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<T: Debug + EQSupported<T>> Debug for EngineeringQuantity<T>

Source§

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

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

impl<T: Default + EQSupported<T>> Default for EngineeringQuantity<T>

Source§

fn default() -> EngineeringQuantity<T>

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

impl<'de, T: EQSupported<T> + FromStr + TryFrom<u128> + TryFrom<i128>> Deserialize<'de> for EngineeringQuantity<T>

Available on feature serde only.
Source§

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

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

impl<T: EQSupported<T>> Display for EngineeringQuantity<T>

Source§

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

Default behaviour is to output to 3 significant figures, skip unnecessary trailing zeros, standard (not RKM) mode. See EngineeringQuantity::default().

§Examples
use engineering_repr::EngineeringQuantity as EQ;
let ee1 = EQ::<i32>::from(1200);
assert_eq!(ee1.to_string(), "1.2k");
let ee2 = EQ::<i32>::from(123456);
assert_eq!(ee2.to_string(), "123k");
Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for i128
where i128: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for i16
where i16: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for i32
where i32: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for i64
where i64: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for isize
where isize: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for u128
where u128: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for u16
where u16: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for u32
where u32: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for u64
where u64: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>> From<EngineeringQuantity<T>> for usize
where usize: From<T>,

Source§

fn from(eq: EngineeringQuantity<T>) -> Self

Conversion to the same storage type (or a larger type) is infallible due to the checks at construction time.

This is a lossy conversion, any fractional part will be truncated.

Note that if you have num_traits in scope, you may need to rephrase the conversion as TryInto::<T>::try_into().

Source§

impl<T: EQSupported<T>, U> From<T> for EngineeringQuantity<U>
where U: From<T> + EQSupported<U>,

Source§

fn from(value: T) -> Self

Integers can always be promoted on conversion to EngineeringQuantity. (For demotions, you have to convert the primitive yourself and handle any failures.)

let i = 42u32;
let _e = engineering_repr::EngineeringQuantity::<u64>::from(i);
Source§

impl<T: EQSupported<T> + FromStr> FromStr for EngineeringQuantity<T>

Source§

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

§Example
use engineering_repr::EngineeringQuantity as EQ;
use std::str::FromStr as _;
let eq = EQ::<i64>::from_str("1.5k").unwrap();
assert_eq!(i64::try_from(eq).unwrap(), 1500);
// RKM style strings
let eq2 = EQ::<i64>::from_str("1k5").unwrap();
assert_eq!(eq, eq2);
Source§

type Err = Error

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

impl<T: EQSupported<T> + From<EngineeringQuantity<T>>> Ord for EngineeringQuantity<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

use engineering_repr::EngineeringQuantity as EQ;
use assertables::assert_lt;
let q2 = EQ::from_raw(41999,0).unwrap();
let q3 = EQ::from_raw(42,1).unwrap();
let q4 = EQ::from_raw(42001,0).unwrap();
assert_lt!(q2, q3);
assert_lt!(q3, q4);
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: EQSupported<T> + From<EngineeringQuantity<T>>> PartialEq for EngineeringQuantity<T>

Source§

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

use engineering_repr::EngineeringQuantity as EQ;
let q1 = EQ::from_raw(42u32,0);
let q2 = EQ::from_raw(42u32,0);
assert_eq!(q1, q2);
let q3 = EQ::from_raw(42,1);
let q4 = EQ::from_raw(42000,0);
assert_eq!(q3, q4);
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<T: EQSupported<T> + From<EngineeringQuantity<T>>> PartialOrd for EngineeringQuantity<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: EQSupported<T>> Serialize for EngineeringQuantity<T>

Available on feature serde only.
Source§

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

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

impl<T: EQSupported<T>> ToPrimitive for EngineeringQuantity<T>

Source§

fn to_i64(&self) -> Option<i64>

Converts self to an i64. If the value cannot be represented by an i64, then None is returned.

use num_traits::cast::ToPrimitive as _;
let e = engineering_repr::EngineeringQuantity::<u32>::from(65_537u32);
assert_eq!(e.to_u128(), Some(65_537));
assert_eq!(e.to_u64(), Some(65_537));
assert_eq!(e.to_u16(), None); // overflow
assert_eq!(e.to_i128(), Some(65_537));
assert_eq!(e.to_i64(), Some(65_537));
assert_eq!(e.to_i16(), None); // overflow
Source§

fn to_i128(&self) -> Option<i128>

Converts self to an i128. If the value cannot be represented by an i128, then None is returned.

Source§

fn to_u128(&self) -> Option<u128>

Converts self to a u128. If the value cannot be represented by a u128, then None is returned.

Source§

fn to_f64(&self) -> Option<f64>

Converts self to an f64. If the value cannot be represented by an f64, then None is returned.

As ever, if you need to compare floating point numbers, beware of epsilon issues. If a precise comparison is needed then converting to a num_rational::Ratio may suit.

use engineering_repr::EngineeringQuantity as EQ;
use std::str::FromStr as _;
let eq = EQ::<u32>::from_str("123m").unwrap();

// TryFrom conversion
assert_eq!(f64::try_from(eq), Ok(0.123));

// Conversion via ToPrimitive
use num_traits::cast::ToPrimitive as _;
assert_eq!(eq.to_f32(), Some(0.123));
assert_eq!(eq.to_f64(), Some(0.123));
Source§

fn to_u64(&self) -> Option<u64>

Converts the value of self to a u64. If the value cannot be represented by a u64, then None is returned.
Source§

fn to_isize(&self) -> Option<isize>

Converts the value of self to an isize. If the value cannot be represented by an isize, then None is returned.
Source§

fn to_i8(&self) -> Option<i8>

Converts the value of self to an i8. If the value cannot be represented by an i8, then None is returned.
Source§

fn to_i16(&self) -> Option<i16>

Converts the value of self to an i16. If the value cannot be represented by an i16, then None is returned.
Source§

fn to_i32(&self) -> Option<i32>

Converts the value of self to an i32. If the value cannot be represented by an i32, then None is returned.
Source§

fn to_usize(&self) -> Option<usize>

Converts the value of self to a usize. If the value cannot be represented by a usize, then None is returned.
Source§

fn to_u8(&self) -> Option<u8>

Converts the value of self to a u8. If the value cannot be represented by a u8, then None is returned.
Source§

fn to_u16(&self) -> Option<u16>

Converts the value of self to a u16. If the value cannot be represented by a u16, then None is returned.
Source§

fn to_u32(&self) -> Option<u32>

Converts the value of self to a u32. If the value cannot be represented by a u32, then None is returned.
Source§

fn to_f32(&self) -> Option<f32>

Converts the value of self to an f32. Overflows may map to positive or negative inifinity, otherwise None is returned if the value cannot be represented by an f32.
Source§

impl<T: EQSupported<T> + Integer + From<EngineeringQuantity<T>>> TryFrom<EngineeringQuantity<T>> for Ratio<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: EngineeringQuantity<T>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: EQSupported<T>> TryFrom<EngineeringQuantity<T>> for f64

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: EngineeringQuantity<T>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T> TryFrom<Ratio<T>> for EngineeringQuantity<T>
where T: Integer + EQSupported<T>,

Source§

fn try_from(value: Ratio<T>) -> Result<Self, Self::Error>

This is a precise conversion, which only succeeds if the denominator of the input Ratio is a power of 1000.

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

impl<T: Copy + EQSupported<T>> Copy for EngineeringQuantity<T>

Source§

impl<T: EQSupported<T> + From<EngineeringQuantity<T>>> Eq for EngineeringQuantity<T>

Auto Trait Implementations§

§

impl<T> Freeze for EngineeringQuantity<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for EngineeringQuantity<T>
where T: RefUnwindSafe,

§

impl<T> Send for EngineeringQuantity<T>
where T: Send,

§

impl<T> Sync for EngineeringQuantity<T>
where T: Sync,

§

impl<T> Unpin for EngineeringQuantity<T>
where T: Unpin,

§

impl<T> UnwindSafe for EngineeringQuantity<T>
where T: 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> 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,