Skip to main content

BigInt

Struct BigInt 

Source
pub struct BigInt {
    pub negative: bool,
    pub magnitude: Vec<u8>,
}
Expand description

An integer of arbitrary size.

This is a well-known extension type (see ext) for integers that do not fit into 128 bits. It holds the sign and the magnitude as big-endian bytes. Leading zero bytes are permitted, zero is never negative.

The fallback is the decimal representation as string. Integers that fit into 64 or 128 bits are not represented as BigInt: they are passed through deser as U64, I64, u128 or i128 instead. That is what the num-bigint support does. When deserializing, all of these as well as strings are accepted.

use deser::ext::BigInt;

let value: BigInt =
    "-340282366920938463463374607431768211456".parse().unwrap();
assert!(value.negative);
assert_eq!(
    value.magnitude,
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
);
assert_eq!(value.to_string(), "-340282366920938463463374607431768211456");

Values are compared and hashed by their numeric value.

Fields§

§negative: bool

true if the integer is negative.

§magnitude: Vec<u8>

The magnitude as big-endian bytes.

Implementations§

Source§

impl BigInt

Source

pub fn from_i128(value: i128) -> BigInt

Creates a big integer from an i128.

Source

pub fn from_u128(value: u128) -> BigInt

Creates a big integer from an u128.

Source

pub fn significant_magnitude(&self) -> &[u8] ⓘ

Returns the magnitude without leading zero bytes.

Source

pub fn is_zero(&self) -> bool

Returns true if the value is zero.

Source

pub fn is_negative(&self) -> bool

Returns true if the value is negative (and not zero).

Source

pub fn to_u128(&self) -> Option<u128>

Returns the value as u128 if it fits.

Source

pub fn to_i128(&self) -> Option<i128>

Returns the value as i128 if it fits.

Source

pub fn into_atom(self) -> Atom<'static>

Converts the value into the smallest atom that can hold it.

This is U64 or I64 if the value fits into 64 bits, an u128 or i128 extension value if it fits into 128 bits and a BigInt extension value otherwise.

Trait Implementations§

Source§

impl Clone for BigInt

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

Source§

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

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

impl Default for BigInt

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for BigInt

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 Display for BigInt

Source§

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

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

impl Eq for BigInt

Source§

impl Extension for BigInt

Source§

fn name(&self) -> &str

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

fn fallback(&self) -> Atom<'_>

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

impl From<BigInt> for Decimal

Source§

fn from(value: BigInt) -> Decimal

Converts to this type from the input type.
Source§

impl From<i64> for BigInt

Source§

fn from(value: i64) -> BigInt

Converts to this type from the input type.
Source§

impl From<i128> for BigInt

Source§

fn from(value: i128) -> BigInt

Converts to this type from the input type.
Source§

impl From<u64> for BigInt

Source§

fn from(value: u64) -> BigInt

Converts to this type from the input type.
Source§

impl From<u128> for BigInt

Source§

fn from(value: u128) -> BigInt

Converts to this type from the input type.
Source§

impl FromStr for BigInt

Source§

fn from_str(s: &str) -> Result<BigInt, Error>

Parses a decimal integer with an optional sign.

Source§

type Err = Error

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

impl Hash for BigInt

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 BigInt

Source§

fn cmp(&self, other: &BigInt) -> 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§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for BigInt

Source§

fn eq(&self, other: &BigInt) -> 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 BigInt

Source§

fn partial_cmp(&self, other: &BigInt) -> 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 BigInt

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

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> BorrowedExtension for T
where T: Extension,

Source§

type Value<'a> = T

The type of the values of this extension.
Source§

fn name(value: &T) -> &str

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

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

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

fn to_static(value: &T) -> T

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

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

Shortens the lifetime of a 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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

Compare self to key and return true if they are equal.
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.