[−][src]Struct json::number::Number
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.
Methods
impl Number[src]
impl Numberpub unsafe fn from_parts_unchecked(
positive: bool,
mantissa: u64,
exponent: i16
) -> Self[src]
pub unsafe fn from_parts_unchecked(
positive: bool,
mantissa: u64,
exponent: i16
) -> SelfConstruct 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.
pub fn from_parts(positive: bool, mantissa: u64, exponent: i16) -> Self[src]
pub fn from_parts(positive: bool, mantissa: u64, exponent: i16) -> SelfConstruct 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);
pub fn as_parts(&self) -> (bool, u64, i16)[src]
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);
pub fn is_sign_positive(&self) -> bool[src]
pub fn is_sign_positive(&self) -> boolpub fn is_zero(&self) -> bool[src]
pub fn is_zero(&self) -> boolpub fn is_nan(&self) -> bool[src]
pub fn is_nan(&self) -> boolpub fn is_empty(&self) -> bool[src]
pub fn is_empty(&self) -> boolTest if the number is NaN or has a zero value.
pub fn as_fixed_point_u64(&self, point: u16) -> Option<u64>[src]
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));
pub fn as_fixed_point_i64(&self, point: u16) -> Option<i64>[src]
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
impl Clone for Number[src]
impl Clone for Numberfn clone(&self) -> Number[src]
fn clone(&self) -> NumberReturns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0[src]
fn clone_from(&mut self, source: &Self)Performs copy-assignment from source. Read more
impl Copy for Number[src]
impl Copy for Numberimpl From<Number> for JsonValue[src]
impl From<Number> for JsonValueimpl From<Number> for f64[src]
impl From<Number> for f64impl From<Number> for f32[src]
impl From<Number> for f32impl From<f64> for Number[src]
impl From<f64> for Numberimpl From<f32> for Number[src]
impl From<f32> for Numberimpl From<isize> for Number[src]
impl From<isize> for Numberimpl From<Number> for isize[src]
impl From<Number> for isizeimpl From<i8> for Number[src]
impl From<i8> for Numberimpl From<Number> for i8[src]
impl From<Number> for i8impl From<i16> for Number[src]
impl From<i16> for Numberimpl From<Number> for i16[src]
impl From<Number> for i16impl From<i32> for Number[src]
impl From<i32> for Numberimpl From<Number> for i32[src]
impl From<Number> for i32impl From<i64> for Number[src]
impl From<i64> for Numberimpl From<Number> for i64[src]
impl From<Number> for i64impl From<usize> for Number[src]
impl From<usize> for Numberimpl From<Number> for usize[src]
impl From<Number> for usizeimpl From<u8> for Number[src]
impl From<u8> for Numberimpl From<Number> for u8[src]
impl From<Number> for u8impl From<u16> for Number[src]
impl From<u16> for Numberimpl From<Number> for u16[src]
impl From<Number> for u16impl From<u32> for Number[src]
impl From<u32> for Numberimpl From<Number> for u32[src]
impl From<Number> for u32impl From<u64> for Number[src]
impl From<u64> for Numberimpl From<Number> for u64[src]
impl From<Number> for u64impl PartialEq<Number> for JsonValue[src]
impl PartialEq<Number> for JsonValuefn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl<'a> PartialEq<Number> for &'a JsonValue[src]
impl<'a> PartialEq<Number> for &'a JsonValuefn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<JsonValue> for Number[src]
impl PartialEq<JsonValue> for Numberfn eq(&self, other: &JsonValue) -> bool[src]
fn eq(&self, other: &JsonValue) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for Number[src]
impl PartialEq<Number> for Numberfn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<f64> for Number[src]
impl PartialEq<f64> for Numberfn eq(&self, other: &f64) -> bool[src]
fn eq(&self, other: &f64) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<f32> for Number[src]
impl PartialEq<f32> for Numberfn eq(&self, other: &f32) -> bool[src]
fn eq(&self, other: &f32) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for f64[src]
impl PartialEq<Number> for f64fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for f32[src]
impl PartialEq<Number> for f32fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<isize> for Number[src]
impl PartialEq<isize> for Numberfn eq(&self, other: &isize) -> bool[src]
fn eq(&self, other: &isize) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for isize[src]
impl PartialEq<Number> for isizefn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<i8> for Number[src]
impl PartialEq<i8> for Numberfn eq(&self, other: &i8) -> bool[src]
fn eq(&self, other: &i8) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for i8[src]
impl PartialEq<Number> for i8fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<i16> for Number[src]
impl PartialEq<i16> for Numberfn eq(&self, other: &i16) -> bool[src]
fn eq(&self, other: &i16) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for i16[src]
impl PartialEq<Number> for i16fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<i32> for Number[src]
impl PartialEq<i32> for Numberfn eq(&self, other: &i32) -> bool[src]
fn eq(&self, other: &i32) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for i32[src]
impl PartialEq<Number> for i32fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<i64> for Number[src]
impl PartialEq<i64> for Numberfn eq(&self, other: &i64) -> bool[src]
fn eq(&self, other: &i64) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for i64[src]
impl PartialEq<Number> for i64fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<usize> for Number[src]
impl PartialEq<usize> for Numberfn eq(&self, other: &usize) -> bool[src]
fn eq(&self, other: &usize) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for usize[src]
impl PartialEq<Number> for usizefn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<u8> for Number[src]
impl PartialEq<u8> for Numberfn eq(&self, other: &u8) -> bool[src]
fn eq(&self, other: &u8) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for u8[src]
impl PartialEq<Number> for u8fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<u16> for Number[src]
impl PartialEq<u16> for Numberfn eq(&self, other: &u16) -> bool[src]
fn eq(&self, other: &u16) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for u16[src]
impl PartialEq<Number> for u16fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<u32> for Number[src]
impl PartialEq<u32> for Numberfn eq(&self, other: &u32) -> bool[src]
fn eq(&self, other: &u32) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for u32[src]
impl PartialEq<Number> for u32fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<u64> for Number[src]
impl PartialEq<u64> for Numberfn eq(&self, other: &u64) -> bool[src]
fn eq(&self, other: &u64) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl PartialEq<Number> for u64[src]
impl PartialEq<Number> for u64fn eq(&self, other: &Number) -> bool[src]
fn eq(&self, other: &Number) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl Display for Number[src]
impl Display for Numberfn fmt(&self, f: &mut Formatter) -> Result[src]
fn fmt(&self, f: &mut Formatter) -> ResultFormats the value using the given formatter. Read more
impl Debug for Number[src]
impl Debug for Numberfn fmt(&self, f: &mut Formatter) -> Result[src]
fn fmt(&self, f: &mut Formatter) -> ResultFormats the value using the given formatter. Read more
impl Neg for Number[src]
impl Neg for NumberAuto Trait Implementations
Blanket Implementations
impl<T> From for T[src]
impl<T> From for Timpl<T, U> Into for T where
U: From<T>, [src]
impl<T, U> Into for T where
U: From<T>, impl<T> ToString for T where
T: Display + ?Sized, [src]
impl<T> ToString for T where
T: Display + ?Sized, impl<T> ToOwned for T where
T: Clone, [src]
impl<T> ToOwned for T where
T: Clone, type Owned = T
fn to_owned(&self) -> T[src]
fn to_owned(&self) -> TCreates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut T)[src]
fn clone_into(&self, target: &mut T)🔬 This is a nightly-only experimental API. (toowned_clone_into)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T, U> TryFrom for T where
T: From<U>, [src]
impl<T, U> TryFrom for T where
T: From<U>, type Error = !
try_from)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>try_from)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized, [src]
impl<T> Borrow for T where
T: ?Sized, impl<T> BorrowMut for T where
T: ?Sized, [src]
impl<T> BorrowMut for T where
T: ?Sized, fn borrow_mut(&mut self) -> &mut T[src]
fn borrow_mut(&mut self) -> &mut TMutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>, [src]
impl<T, U> TryInto for T where
U: TryFrom<T>, type Error = <U as TryFrom<T>>::Error
try_from)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>try_from)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized, [src]
impl<T> Any for T where
T: 'static + ?Sized, fn get_type_id(&self) -> TypeId[src]
fn get_type_id(&self) -> TypeId🔬 This is a nightly-only experimental API. (get_type_id)
this method will likely be replaced by an associated static
Gets the TypeId of self. Read more