Skip to main content

Number

Struct Number 

Source
pub struct Number<'a> { /* private fields */ }
Expand description

A number literal from a text format.

This is a well-known borrowing extension (see ext) for numbers that text formats parsed as floats (or integers too large for the data model). It holds the text of the literal in the syntax of JSON numbers together with its value as f64. The fallback is the f64, so consumers that do not know about exact numbers are not affected. Consumers that need the exact value (like Decimal and BigInt and the types of rust_decimal, bigdecimal and num-bigint) use the text.

use deser::ext::{Decimal, Number};

let number = Number::parse("0.10").unwrap();
assert_eq!(number.as_str(), "0.10");
assert_eq!(number.value(), 0.1);

The text borrows from the input for formats that support it, extension values of it are created with ExtValue::borrowed_value::<Number> and looked up with ExtValue::downcast_value_ref::<Number>.

Implementations§

Source§

impl<'a> Number<'a>

Source

pub fn new<T: Into<Cow<'a, str>>>(text: T, value: f64) -> Number<'a>

Creates a number from its text and value.

The text has to follow the syntax of JSON numbers and the value has to be the (possibly approximated) value of the text. This is not checked in release builds. Use parse to validate the text and compute the value.

Source

pub fn parse<T: Into<Cow<'a, str>>>(text: T) -> Result<Number<'a>, Error>

Parses a number from its text.

The text has to follow the syntax of JSON numbers.

Source

pub fn as_str(&self) -> &str

Returns the text of the number.

Source

pub fn value(&self) -> f64

Returns the value of the number as f64.

This might be an approximation of the text.

Source

pub fn is_integer(&self) -> bool

Returns true if the text is an integer (no fraction or exponent).

Source

pub fn into_static(self) -> Number<'static>

Detaches the number from the data it borrows.

Trait Implementations§

Source§

impl BorrowedExtension for Number<'static>

Source§

type Value<'a> = Number<'a>

The type of the values of this extension.
Source§

fn name<'v>(_value: &'v Number<'_>) -> &'v str

Returns the human readable name of the extension type. Read more
Source§

fn fallback<'v>(value: &'v Number<'_>) -> Atom<'v>

Lowers the value into the core data model. Read more
Source§

fn to_static(value: &Number<'_>) -> Number<'static>

Detaches a value from the data it borrows. Read more
Source§

fn shorten<'s, 'l: 's>(value: &'s Number<'l>) -> &'s Number<'s>

Shortens the lifetime of a value. Read more
Source§

impl<'a> Clone for Number<'a>

Source§

fn clone(&self) -> Self

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

Source§

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

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

impl<'de, 'a> Deserialize<'de> for Number<'a>

Numbers are deserialized from numbers (and number extension values) and strings with the syntax of JSON numbers. The text is always owned.

Source§

fn deserialize_into<'out>( out: &'out mut Option<Self>, state: &mut State, ) -> SinkHandle<'out, 'de>

Creates a sink that deserializes the value into the given slot. Read more
Source§

fn initial_value() -> Option<Self>

Provides the value of a missing struct field. Read more
Source§

fn deserialize_update<'out>( value: &'out mut Self, state: &mut State, ) -> SinkHandle<'out, 'de>

Creates a sink that updates an existing value. Read more
Source§

impl<'a> Display for Number<'a>

Source§

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

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

impl<'a> PartialEq for Number<'a>

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<'a> Serialize for Number<'a>

Source§

fn serialize(&self, _state: &mut State) -> Result<Chunk<'_>, Error>

Serializes this serializable.
Source§

fn finish(&self, _state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn is_optional(&self) -> bool

Checks if the current value that would be serialized represents an optional value. Read more
Source§

fn describe(&self, d: &mut dyn Describe)

Describes the Rust shape of this value. Read more
Source§

fn container_shape(&self) -> ContainerShape

Returns the shape of this value if it’s a map or sequence. Read more
Source§

impl<'a> StructuralPartialEq for Number<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Number<'a>

§

impl<'a> RefUnwindSafe for Number<'a>

§

impl<'a> Send for Number<'a>

§

impl<'a> Sync for Number<'a>

§

impl<'a> Unpin for Number<'a>

§

impl<'a> UnsafeUnpin for Number<'a>

§

impl<'a> UnwindSafe for Number<'a>

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<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 = !

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

fn try_from(value: U) -> Result<T, !>

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.