engineering_repr

Struct EngineeringQuantity

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

Helper type for expressing numbers in engineering notation

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

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

Source

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

Raw constructor from component parts

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);
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>, <U as TryFrom<T>>::Error>

Fallible conversion to a different storage type.

Note that conversion only fails if the significand doesn’t fit into the destination storage type, without reference to the exponent. This means that two numbers, which might be equal, may not both be convertible to the same destination type if they are not normalised. For example:

use engineering_repr::EngineeringQuantity as EQ;
let million1 = EQ::from_raw(1, 2); // 1e6
let million2 = EQ::from_raw(1_000_000, 0);
assert_eq!(million1, million2);
let r1 = million1.try_convert::<u16>().unwrap(); // OK, because stored as (1,2)
let r2 = million2.try_convert::<u16>().expect_err("overflow"); // Overflow, because 1_000_000 won't fit into a u16
Source§

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

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> Deserialize<'de> for EngineeringQuantity<T>

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, 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.20k");
let ee2 = EQ::<i32>::from(123456);
assert_eq!(ee2.to_string(), "123k");
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> + TryFrom<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> + TryFrom<EngineeringQuantity<T>>> PartialOrd for EngineeringQuantity<T>

Source§

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

use engineering_repr::EngineeringQuantity as EQ;
use more_asserts::assert_lt;
let q2 = EQ::from_raw(41999,0);
let q3 = EQ::from_raw(42,1);
let q4 = EQ::from_raw(42001,0);
assert_lt!(q2, q3);
assert_lt!(q3, q4);
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>

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<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for i128
where i128: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(i128::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for i16
where i16: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(i16::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for i32
where i32: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(i32::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for i64
where i64: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(i64::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for isize
where isize: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(isize::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for u128
where u128: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(u128::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for u16
where u16: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(u16::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for u32
where u32: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(u32::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for u64
where u64: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(u64::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<U: EQSupported<U>> TryFrom<EngineeringQuantity<U>> for usize
where usize: TryFrom<U>,

Source§

fn try_from(eq: EngineeringQuantity<U>) -> Result<Self, Error>

Conversion to integer is always fallible, as the exponent might cause us to under or overflow.

use engineering_repr::EngineeringQuantity;
use engineering_repr::Error as EErr;
let i = EngineeringQuantity::<u32>::from_raw(11, 1);
assert_eq!(usize::try_from(i).unwrap(), 11000);
Source§

type Error = Error

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

impl<T: Copy + EQSupported<T>> Copy 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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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>,