Struct js_int::UInt

source · []
pub struct UInt(_);
Expand description

An integer limited to the range of non-negative integers that can be represented exactly by an f64.

Implementations

The smallest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(UInt::MIN, uint!(0));

The largest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(UInt::MAX, UInt::try_from(9_007_199_254_740_991u64).unwrap());

Try to create a UInt from the provided u64, returning None if it is larger than MAX_SAFE_UINT.

This is the same as the TryFrom<u64> implementation for UInt, except that it returns an Option instead of a Result.

Examples

Basic usage:

assert_eq!(UInt::new(js_int::MAX_SAFE_UINT), Some(UInt::MAX));
assert_eq!(UInt::new(js_int::MAX_SAFE_UINT + 1), None);

Create a UInt from the provided u64, wrapping at MAX_SAFE_UINT.

Examples

Basic usage:

assert_eq!(UInt::new_wrapping(js_int::MAX_SAFE_UINT), UInt::MAX);
assert_eq!(UInt::new_wrapping(js_int::MAX_SAFE_UINT + 1), uint!(0));

Creates an UInt from the given u64 capped at MAX_SAFE_UINT.

Examples

Basic usage:

assert_eq!(UInt::new_saturating(0), uint!(0));
assert_eq!(UInt::new_saturating(js_int::MAX_SAFE_UINT), UInt::MAX);
assert_eq!(UInt::new_saturating(js_int::MAX_SAFE_UINT + 1), UInt::MAX);
👎 Deprecated:

Use UInt::MIN instead.

Returns the smallest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(UInt::min_value(), uint!(0));
👎 Deprecated:

Use UInt::MAX instead.

Returns the largest value that can be represented by this integer type.

Examples

Basic usage:

assert_eq!(UInt::max_value(), UInt::try_from(9_007_199_254_740_991u64).unwrap());

Returns true if and only if self == 2^k for some k.

Examples

Basic usage:

assert!(uint!(16).is_power_of_two());
assert!(!uint!(10).is_power_of_two());

Returns the smallest power of two greater than or equal to n. If the next power of two is greater than the type’s maximum value, None is returned, otherwise the power of two is wrapped in Some.

Examples

Basic usage:

assert_eq!(uint!(2).checked_next_power_of_two(), Some(uint!(2)));
assert_eq!(uint!(3).checked_next_power_of_two(), Some(uint!(4)));
assert_eq!(UInt::MAX.checked_next_power_of_two(), None);

Converts a string slice in a given base to an integer.

The string is expected to be an optional + sign followed by digits. Leading and trailing whitespace represent an error. Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z
Panics

This function panics if radix is not in the range from 2 to 36.

Examples

Basic usage:

assert_eq!(UInt::from_str_radix("A", 16), Ok(uint!(10)));

Checked integer addition. Computes self + rhs, returning None if overflow occurred.

assert_eq!(
    (UInt::MAX - uint!(2)).checked_add(uint!(1)),
    Some(UInt::MAX - uint!(1))
);
assert_eq!((UInt::MAX - uint!(2)).checked_add(uint!(3)), None);

Checked integer subtraction. Computes self - rhs, returning None if overflow occurred.

Examples

Basic usage:

assert_eq!(uint!(1).checked_sub(uint!(1)), Some(uint!(0)));
assert_eq!(uint!(0).checked_sub(uint!(1)), None);

Checked integer multiplication. Computes self * rhs, returning None if overflow occurred.

Examples

Basic usage:

assert_eq!(uint!(5).checked_mul(uint!(1)), Some(uint!(5)));
assert_eq!(UInt::MAX.checked_mul(uint!(2)), None);

Checked integer division. Computes self / rhs, returning None if rhs == 0.

Examples

Basic usage:

assert_eq!(uint!(128).checked_div(uint!(2)), Some(uint!(64)));
assert_eq!(uint!(1).checked_div(uint!(0)), None);

Checked integer remainder. Computes self % rhs, returning None if rhs == 0.

Examples

Basic usage:

assert_eq!(uint!(5).checked_rem(uint!(2)), Some(uint!(1)));
assert_eq!(uint!(5).checked_rem(uint!(0)), None);

Checked negation. Computes -self, returning None unless self == 0.

Note that negating any positive integer will overflow.

Examples

Basic usage:

assert_eq!(uint!(0).checked_neg(), Some(uint!(0)));
assert_eq!(uint!(1).checked_neg(), None);

Checked exponentiation. Computes self.pow(exp), returning None if overflow or underflow occurred.

Examples

Basic usage:

assert_eq!(uint!(0).checked_pow(2), Some(uint!(0)));
assert_eq!(uint!(8).checked_pow(2), Some(uint!(64)));
assert_eq!(uint!(1_000_000_000u32).checked_pow(2), None);
assert_eq!(UInt::MAX.checked_pow(2), None);

Saturating integer addition. Computes self + rhs, saturating at the numeric bounds instead of overflowing.

Examples

Basic usage:

assert_eq!(uint!(100).saturating_add(uint!(1)), uint!(101));
assert_eq!(UInt::MAX.saturating_add(uint!(1)), UInt::MAX);

Saturating integer subtraction. Computes self - rhs, saturating at the numeric bounds instead of underflowing.

Examples

Basic usage:

assert_eq!(uint!(100).saturating_sub(uint!(1)), uint!(99));
assert_eq!(uint!(1).saturating_sub(uint!(2)), uint!(0));

Saturating integer multiplication. Computes self * rhs, saturating at the numeric bounds instead of overflowing.

Examples

Basic usage:

assert_eq!(uint!(100).saturating_mul(uint!(2)), uint!(200));
assert_eq!(UInt::MAX.saturating_mul(uint!(2)), UInt::MAX);
assert_eq!(UInt::MAX.saturating_mul(UInt::MAX), UInt::MAX);

Saturating integer exponentiation. Computes self.pow(exp), saturating at the numeric bounds instead of overflowing or underflowing.

Examples

Basic usage:

assert_eq!(uint!(5).saturating_pow(2), uint!(25));
assert_eq!(UInt::MAX.saturating_pow(2), UInt::MAX);

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

The associated error which can be returned from parsing.

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

Feeds this value into the given Hasher. Read more

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

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

Performs the %= operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.