[−][src]Enum kserd::Number
A numerical value.
Number
captures Rust numerical primitives: unsigned integers, signed integers, and
floating point decimal numbers. The data is stored inside an enum housing the maximum size of
each numerical type (128 bits for integers, 64 bits for floats). The numbers are also
canonicalized, that is Eq
and Ord
are implemented and comparisons can be made between
integers and floats.
TODO: Operations (add, subtract, etc) are to be added in the future.
The number line extends from negative infinity, through zero, to positive infinity. Nan is
above positive infinity. All zeroes are treated equally (-0 == +0
), as well as all Nans.
[ -∞, .., 0, .., +∞, NaN ]
Examples
Number
can be constructed straight from any of the Rust numbers using the From
trait.
let n: Number = 123456u32.into(); assert_eq!(n, Number::Uint(123456));
Comparisons can be made between different number types.
let n = Number::from(100u8); assert_eq!(n, Number::from(100.0f32)); assert_eq!(n, Number::from(100i32)); assert_ne!(n, Number::from(99.99f64));
PartialEq
is also implemented for each Rust number.
let n = Number::from(100u8); assert_eq!(n, 100.0f32); assert_eq!(n, 100i32); assert_ne!(n, 99.99);
Ordering is likewise implemented.
let n = Number::from(100u8); assert!(n < Number::from(101)); assert!(n > Number::from(-100)); assert!(n > Number::from(99.99)); // Likewise with literal numbers assert!(n < 101); assert!(n > -100); assert!(n > 99.99);
An exmaple of the canonicalized ordering.
use std::f64::{INFINITY, NEG_INFINITY, NAN}; // use an ordered set let mut set = std::collections::BTreeSet::new(); set.insert(Number::from(0)); set.insert((-0.0).into()); set.insert((-1.0).into()); set.insert(0.5.into()); set.insert(INFINITY.into()); set.insert((-100).into()); set.insert(NAN.into()); set.insert(NAN.into()); set.insert((-0.0).into()); set.insert(NEG_INFINITY.into()); set.insert(100.0.into()); let expected: Vec<Number> = vec![ NEG_INFINITY, -100.0, -1.0, 0.0, 0.5, 100.0, INFINITY, NAN, ].into_iter().map(Number::from).collect(); assert_eq!(set.into_iter().collect::<Vec<_>>(), expected);
Variants
Uint(u128)
Int(i128)
Float(f64)
Methods
impl Number
[src]
pub fn as_u128(&self) -> Result<u128, IntoIntError>
[src]
Represent Number
as an unsigned integer.
Example
use kserd::ds::IntoIntError; use std::f64::{INFINITY, NEG_INFINITY, NAN}; // generally the conversion will work assert_eq!(Number::from(100i32).as_u128(), Ok(100)); assert_eq!(Number::from(100.0).as_u128(), Ok(100)); // can also work if fractional part of float is smaller than valid amount assert_eq!(Number::from(3.0 + 5e-11).as_u128(), Ok(3)); // can fail if outside assert_eq!(Number::from(-100i32).as_u128(), Err(IntoIntError)); assert_eq!(Number::from(0.5).as_u128(), Err(IntoIntError)); assert_eq!(Number::from(INFINITY).as_u128(), Err(IntoIntError)); assert_eq!(Number::from(NEG_INFINITY).as_u128(), Err(IntoIntError)); assert_eq!(Number::from(NAN).as_u128(), Err(IntoIntError));
pub fn as_i128(&self) -> Result<i128, IntoIntError>
[src]
Represent Number
as a signed integer.
Example
use kserd::ds::IntoIntError; use std::f64::{INFINITY, NEG_INFINITY, NAN}; // generally the conversion will work assert_eq!(Number::from(100u32).as_i128(), Ok(100)); assert_eq!(Number::from(100.0).as_i128(), Ok(100)); // can also work if fractional part of float is smaller than valid amount assert_eq!(Number::from(3.0 + 5e-11).as_i128(), Ok(3)); // can fail if outside assert_eq!(Number::from(0.5).as_i128(), Err(IntoIntError)); assert_eq!(Number::from(INFINITY).as_i128(), Err(IntoIntError)); assert_eq!(Number::from(NEG_INFINITY).as_i128(), Err(IntoIntError)); assert_eq!(Number::from(NAN).as_i128(), Err(IntoIntError));
pub fn as_f64(&self) -> f64
[src]
Represent Number
as a floating point decimal.
Does not fail, but is a lossy conversion if an integer.
Examples
assert_eq!(Number::from(100u8).as_f64(), 100.0); assert_eq!(Number::from(-100).as_f64(), -100.0); // lossy conversion if integers get large. assert_eq!(Number::from(u128::max_value()).as_f64(), 3402823669209385e23); assert_eq!(Number::from(i128::max_value()).as_f64(), 17014118346046923e22); assert_eq!(Number::from(i128::min_value()).as_f64(), -17014118346046923e22);
Trait Implementations
impl From<usize> for Number
[src]
impl From<u8> for Number
[src]
impl From<u16> for Number
[src]
impl From<u32> for Number
[src]
impl From<u64> for Number
[src]
impl From<u128> for Number
[src]
impl From<isize> for Number
[src]
impl From<i8> for Number
[src]
impl From<i16> for Number
[src]
impl From<i32> for Number
[src]
impl From<i64> for Number
[src]
impl From<i128> for Number
[src]
impl From<f32> for Number
[src]
impl From<f64> for Number
[src]
impl Ord for Number
[src]
fn cmp(&self, other: &Number) -> Ordering
[src]
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
fn clamp(self, min: Self, max: Self) -> Self
[src]
clamp
)Restrict a value to a certain interval. Read more
impl PartialOrd<Number> for Number
[src]
fn partial_cmp(&self, other: &Number) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<usize> for Number
[src]
fn partial_cmp(&self, rhs: &usize) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<u8> for Number
[src]
fn partial_cmp(&self, rhs: &u8) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<u16> for Number
[src]
fn partial_cmp(&self, rhs: &u16) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<u32> for Number
[src]
fn partial_cmp(&self, rhs: &u32) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<u64> for Number
[src]
fn partial_cmp(&self, rhs: &u64) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<u128> for Number
[src]
fn partial_cmp(&self, rhs: &u128) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<isize> for Number
[src]
fn partial_cmp(&self, rhs: &isize) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<i8> for Number
[src]
fn partial_cmp(&self, rhs: &i8) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<i16> for Number
[src]
fn partial_cmp(&self, rhs: &i16) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<i32> for Number
[src]
fn partial_cmp(&self, rhs: &i32) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<i64> for Number
[src]
fn partial_cmp(&self, rhs: &i64) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<i128> for Number
[src]
fn partial_cmp(&self, rhs: &i128) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<f32> for Number
[src]
fn partial_cmp(&self, rhs: &f32) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialOrd<f64> for Number
[src]
fn partial_cmp(&self, rhs: &f64) -> Option<Ordering>
[src]
#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl PartialEq<Number> for Number
[src]
fn eq(&self, other: &Number) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<usize> for Number
[src]
fn eq(&self, rhs: &usize) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<u8> for Number
[src]
fn eq(&self, rhs: &u8) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<u16> for Number
[src]
fn eq(&self, rhs: &u16) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<u32> for Number
[src]
fn eq(&self, rhs: &u32) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<u64> for Number
[src]
fn eq(&self, rhs: &u64) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<u128> for Number
[src]
fn eq(&self, rhs: &u128) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<isize> for Number
[src]
fn eq(&self, rhs: &isize) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<i8> for Number
[src]
fn eq(&self, rhs: &i8) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<i16> for Number
[src]
fn eq(&self, rhs: &i16) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<i32> for Number
[src]
fn eq(&self, rhs: &i32) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<i64> for Number
[src]
fn eq(&self, rhs: &i64) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<i128> for Number
[src]
fn eq(&self, rhs: &i128) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<f32> for Number
[src]
fn eq(&self, rhs: &f32) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl PartialEq<f64> for Number
[src]
fn eq(&self, rhs: &f64) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl Clone for Number
[src]
fn clone(&self) -> Number
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Eq for Number
[src]
impl Copy for Number
[src]
impl Display for Number
[src]
impl Debug for Number
[src]
Auto Trait Implementations
impl Send for Number
impl Unpin for Number
impl Sync for Number
impl UnwindSafe for Number
impl RefUnwindSafe for Number
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,