Struct Number

Source
pub struct Number { /* private fields */ }
Expand description

Number representation used inside JsonValue. You can easily convert the Number type into native Rust number types and back, or use the equality operator with another number type.

let foo: Number = 3.14.into();
let bar: f64 = foo.into();

assert_eq!(foo, 3.14);
assert_eq!(bar, 3.14);

More often than not you will deal with JsonValue::Number variant that wraps around this type, instead of using the methods here directly.

Implementations§

Source§

impl Number

Source

pub unsafe fn from_parts_unchecked( positive: bool, mantissa: u64, exponent: i16, ) -> Self

Construct a new Number from parts. This can’t create a NaN value.

let pi = unsafe { Number::from_parts_unchecked(true, 3141592653589793, -15) };

assert_eq!(pi, 3.141592653589793);

While this method is marked unsafe, it doesn’t actually perform any unsafe operations. THe goal of the ‘unsafe’ is to deter from using this method in favor of its safe equivalent from_parts, at least in context when the associated performance cost is negligible.

Source

pub fn from_parts(positive: bool, mantissa: u64, exponent: i16) -> Self

Construct a new Number from parts, stripping unnecessary trailing zeroes. This can’t create a NaN value.

let one = Number::from_parts(true, 1000, -3);
let (positive, mantissa, exponent) = one.as_parts();

assert_eq!(true, positive);
assert_eq!(1, mantissa);
assert_eq!(0, exponent);
Source

pub fn as_parts(&self) -> (bool, u64, i16)

Reverse to from_parts - obtain parts from an existing Number.

let pi = Number::from(3.141592653589793);
let (positive, mantissa, exponent) = pi.as_parts();

assert_eq!(positive, true);
assert_eq!(mantissa, 3141592653589793);
assert_eq!(exponent, -15);
Source

pub fn is_sign_positive(&self) -> bool

Source

pub fn is_zero(&self) -> bool

Source

pub fn is_nan(&self) -> bool

Source

pub fn is_empty(&self) -> bool

Test if the number is NaN or has a zero value.

Source

pub fn as_fixed_point_u64(&self, point: u16) -> Option<u64>

Obtain an integer at a fixed decimal point. This is useful for converting monetary values and doing arithmetic on them without rounding errors introduced by floating point operations.

Will return None if Number is negative or a NaN.

let price_a = Number::from(5.99);
let price_b = Number::from(7);
let price_c = Number::from(10.2);

assert_eq!(price_a.as_fixed_point_u64(2), Some(599));
assert_eq!(price_b.as_fixed_point_u64(2), Some(700));
assert_eq!(price_c.as_fixed_point_u64(2), Some(1020));
Source

pub fn as_fixed_point_i64(&self, point: u16) -> Option<i64>

Analog to as_fixed_point_u64, except returning a signed i64, properly handling negative numbers.

let balance_a = Number::from(-1.49);
let balance_b = Number::from(42);

assert_eq!(balance_a.as_fixed_point_i64(2), Some(-149));
assert_eq!(balance_b.as_fixed_point_i64(2), Some(4200));

Trait Implementations§

Source§

impl Clone for Number

Source§

fn clone(&self) -> Number

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 Debug for Number

Source§

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

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

impl Display for Number

Source§

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

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

impl From<Number> for JsonValue

Source§

fn from(val: Number) -> JsonValue

Converts to this type from the input type.
Source§

impl From<Number> for f32

Source§

fn from(num: Number) -> f32

Converts to this type from the input type.
Source§

impl From<Number> for f64

Source§

fn from(num: Number) -> f64

Converts to this type from the input type.
Source§

impl From<f32> for Number

Source§

fn from(float: f32) -> Number

Converts to this type from the input type.
Source§

impl From<f64> for Number

Source§

fn from(float: f64) -> Number

Converts to this type from the input type.
Source§

impl From<i16> for Number

Source§

fn from(num: i16) -> Number

Converts to this type from the input type.
Source§

impl From<i32> for Number

Source§

fn from(num: i32) -> Number

Converts to this type from the input type.
Source§

impl From<i64> for Number

Source§

fn from(num: i64) -> Number

Converts to this type from the input type.
Source§

impl From<i8> for Number

Source§

fn from(num: i8) -> Number

Converts to this type from the input type.
Source§

impl From<isize> for Number

Source§

fn from(num: isize) -> Number

Converts to this type from the input type.
Source§

impl From<u16> for Number

Source§

fn from(num: u16) -> Number

Converts to this type from the input type.
Source§

impl From<u32> for Number

Source§

fn from(num: u32) -> Number

Converts to this type from the input type.
Source§

impl From<u64> for Number

Source§

fn from(num: u64) -> Number

Converts to this type from the input type.
Source§

impl From<u8> for Number

Source§

fn from(num: u8) -> Number

Converts to this type from the input type.
Source§

impl From<usize> for Number

Source§

fn from(num: usize) -> Number

Converts to this type from the input type.
Source§

impl Neg for Number

Source§

type Output = Number

The resulting type after applying the - operator.
Source§

fn neg(self) -> Number

Performs the unary - operation. Read more
Source§

impl PartialEq<JsonValue> for Number

Source§

fn eq(&self, other: &JsonValue) -> 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<'a> PartialEq<Number> for &'a JsonValue

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for JsonValue

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for f32

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for f64

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for i16

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for i32

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for i64

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for i8

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for isize

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for u16

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for u32

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for u64

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for u8

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<Number> for usize

Source§

fn eq(&self, other: &Number) -> 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 PartialEq<f32> for Number

Source§

fn eq(&self, other: &f32) -> 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 PartialEq<f64> for Number

Source§

fn eq(&self, other: &f64) -> 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 PartialEq<i16> for Number

Source§

fn eq(&self, other: &i16) -> 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 PartialEq<i32> for Number

Source§

fn eq(&self, other: &i32) -> 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 PartialEq<i64> for Number

Source§

fn eq(&self, other: &i64) -> 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 PartialEq<i8> for Number

Source§

fn eq(&self, other: &i8) -> 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 PartialEq<isize> for Number

Source§

fn eq(&self, other: &isize) -> 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 PartialEq<u16> for Number

Source§

fn eq(&self, other: &u16) -> 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 PartialEq<u32> for Number

Source§

fn eq(&self, other: &u32) -> 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 PartialEq<u64> for Number

Source§

fn eq(&self, other: &u64) -> 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 PartialEq<u8> for Number

Source§

fn eq(&self, other: &u8) -> 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 PartialEq<usize> for Number

Source§

fn eq(&self, other: &usize) -> 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 PartialEq for Number

Source§

fn eq(&self, other: &Number) -> 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 TryFrom<Number> for i16

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for i32

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for i64

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for i8

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for isize

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for u16

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for u32

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for u64

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for u8

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Number> for usize

Source§

type Error = NumberOutOfScope

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

fn try_from(num: Number) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Number

Auto Trait Implementations§

§

impl Freeze for Number

§

impl RefUnwindSafe for Number

§

impl Send for Number

§

impl Sync for Number

§

impl Unpin for Number

§

impl UnwindSafe for Number

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.