Skip to main content

INumber

Struct INumber 

Source
pub struct INumber(/* private fields */);
Expand description

The INumber type represents a JSON number. It is decoupled from any specific representation, and internally uses several. There is no way to determine the internal representation: instead the caller is expected to convert the number using one of the fallible to_xxx functions and handle the cases where the number does not convert to the desired type.

Special floating point values (eg. NaN, Infinity, etc.) cannot be stored within an INumber.

Whilst INumber does not consider 2.0 and 2 to be different numbers (ie. they will compare equal) it does allow you to distinguish them using the method INumber::has_decimal_point(). That said, calling to_i32 on 2.0 will succeed with the value 2.

Small numbers — integers and short decimals alike — are stored inline without a heap allocation. Larger integers (i64/u64) and floating point values that are not short decimals are stored behind a pointer.

Implementations§

Source§

impl INumber

Source

pub fn zero() -> Self

Returns the number zero (without a decimal point). Does not allocate.

Source

pub fn one() -> Self

Returns the number one (without a decimal point). Does not allocate.

Source

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

Converts this number to an i64 if it can be represented exactly.

Source

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

Converts this number to a u64 if it can be represented exactly.

Source

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

Converts this number to an f64 if it can be represented exactly.

Source

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

Converts this number to an f32 if it can be represented exactly.

Source

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

Converts this number to an i32 if it can be represented exactly.

Source

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

Converts this number to a u32 if it can be represented exactly.

Source

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

Converts this number to an isize if it can be represented exactly.

Source

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

Converts this number to a usize if it can be represented exactly.

Source

pub fn to_f64_lossy(&self) -> f64

Converts this number to an f64, potentially losing precision in the process.

Source

pub fn to_f32_lossy(&self) -> f32

Converts this number to an f32, potentially losing precision in the process.

Source

pub fn has_decimal_point(&self) -> bool

This allows distinguishing between 1.0 and 1 in the original JSON. Numeric operations will otherwise treat these two values as equivalent.

Trait Implementations§

Source§

impl AsMut<IValue> for INumber

Source§

fn as_mut(&mut self) -> &mut IValue

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<IValue> for INumber

Source§

fn as_ref(&self) -> &IValue

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<IValue> for INumber

Source§

fn borrow(&self) -> &IValue

Immutably borrows from an owned value. Read more
Source§

impl BorrowMut<IValue> for INumber

Source§

fn borrow_mut(&mut self) -> &mut IValue

Mutably borrows from an owned value. Read more
Source§

impl Clone for INumber

Source§

fn clone(&self) -> INumber

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for INumber

Source§

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

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

impl Default for INumber

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for INumber

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<'de> Deserializer<'de> for &'de INumber

Source§

type Error = Error

The error type that can be returned if some error occurs during deserialization.
Source§

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor<'de>,

Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more
Source§

fn deserialize_newtype_struct<V>( self, _name: &'static str, visitor: V, ) -> Result<V::Value, Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a newtype struct with a particular name.
Source§

fn deserialize_bool<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a bool value.
Source§

fn deserialize_i8<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i8 value.
Source§

fn deserialize_i16<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i16 value.
Source§

fn deserialize_i32<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i32 value.
Source§

fn deserialize_i64<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i64 value.
Source§

fn deserialize_i128<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i128 value. Read more
Source§

fn deserialize_u8<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u8 value.
Source§

fn deserialize_u16<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u16 value.
Source§

fn deserialize_u32<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u32 value.
Source§

fn deserialize_u64<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u64 value.
Source§

fn deserialize_u128<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an u128 value. Read more
Source§

fn deserialize_f32<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f32 value.
Source§

fn deserialize_f64<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f64 value.
Source§

fn deserialize_char<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a char value.
Source§

fn deserialize_str<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_string<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_bytes<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a byte array and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_byte_buf<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a byte array and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_option<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an optional value. Read more
Source§

fn deserialize_unit<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a unit value.
Source§

fn deserialize_unit_struct<V>( self, name: &'static str, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a unit struct with a particular name.
Source§

fn deserialize_seq<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a sequence of values.
Source§

fn deserialize_tuple<V>( self, len: usize, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a sequence of values and knows how many values there are without looking at the serialized data.
Source§

fn deserialize_tuple_struct<V>( self, name: &'static str, len: usize, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a tuple struct with a particular name and number of fields.
Source§

fn deserialize_map<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a map of key-value pairs.
Source§

fn deserialize_struct<V>( self, name: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a struct with a particular name and fields.
Source§

fn deserialize_enum<V>( self, name: &'static str, variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an enum value with a particular name and possible variants.
Source§

fn deserialize_identifier<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting the name of a struct field or the discriminant of an enum variant.
Source§

fn deserialize_ignored_any<V>( self, visitor: V, ) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where V: Visitor<'de>,

Hint that the Deserialize type needs to deserialize a value whose type doesn’t matter because it is ignored. Read more
Source§

fn is_human_readable(&self) -> bool

Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more
Source§

impl Eq for INumber

Source§

impl From<INumber> for IValue

Source§

fn from(other: INumber) -> Self

Converts to this type from the input type.
Source§

impl From<INumber> for Number

Converts an INumber into a serde_json::Number.

Conversion may be lossy if the number is not exactly representable as a serde_json::Number. The exact behaviour in that case (e.g. rounding) is not guaranteed to be stable across versions.

Source§

fn from(n: INumber) -> Self

Converts to this type from the input type.
Source§

impl From<Number> for INumber

Converts a serde_json::Number into an INumber.

Conversion may be lossy if the number is not exactly representable as an INumber. The exact behaviour in that case (e.g. clamping an out-of-range magnitude) is not guaranteed to be stable across versions.

Source§

fn from(n: Number) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for INumber

Source§

fn from(v: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for INumber

Source§

fn from(v: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for INumber

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for INumber

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for INumber

Source§

fn from(v: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for INumber

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for INumber

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for INumber

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for INumber

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for INumber

Source§

fn from(v: usize) -> Self

Converts to this type from the input type.
Source§

impl FromStr for INumber

Parses a JSON number from its textual form, exactly as the JSON grammar defines it (see https://www.json.org/).

A float-shaped number is parsed to the nearest f64 (as deserializing through serde_json would), while a plain in-range integer keeps its exact integer representation (no decimal point).

With the arbitrary_precision feature this instead preserves the exact decimal value where it fits: "0.1" becomes the exact 1 * 10^-1 rather than the f64 approximation — and thus a different value from the f64 0.1 — falling back to the nearest f64 only when the value is too large or too precise to hold exactly.

§Examples

let n: INumber = "1.5".parse().unwrap();
assert_eq!(n.to_f64(), Some(1.5));
assert!(n.has_decimal_point());

let n: INumber = "-42".parse().unwrap();
assert_eq!(n.to_i64(), Some(-42));
assert!(!n.has_decimal_point());

// Invalid JSON numbers are rejected (leading zero, bare '+', trailing '.').
assert!("01".parse::<INumber>().is_err());
assert!("+1".parse::<INumber>().is_err());
assert!("1.".parse::<INumber>().is_err());
Source§

type Err = ParseNumberError

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

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

Parses a string s to return a value of this type. Read more
Source§

impl Hash for INumber

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for INumber

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

impl PartialEq for INumber

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for INumber

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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 (const: unstable) · 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 Serialize for INumber

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<'a> TryFrom<&'a IValue> for &'a INumber

Source§

type Error = ()

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

fn try_from(other: &'a IValue) -> Result<Self, ()>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a mut IValue> for &'a mut INumber

Source§

type Error = ()

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

fn try_from(other: &'a mut IValue) -> Result<Self, ()>

Performs the conversion.
Source§

impl TryFrom<IValue> for INumber

Source§

type Error = IValue

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

fn try_from(other: IValue) -> Result<Self, IValue>

Performs the conversion.
Source§

impl TryFrom<f32> for INumber

Source§

type Error = ()

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

fn try_from(v: f32) -> Result<Self, ()>

Performs the conversion.
Source§

impl TryFrom<f64> for INumber

Source§

type Error = ()

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

fn try_from(v: f64) -> Result<Self, ()>

Performs the conversion.

Auto Trait Implementations§

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

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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, 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.