[][src]Struct js_int::UInt

pub struct UInt(_);

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

Methods

impl UInt[src]

pub const MIN: Self[src]

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

Examples

Basic usage:

assert_eq!(UInt::MIN, UInt::from(0u32));

pub const MAX: Self[src]

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());

#[must_use]pub fn new(val: u64) -> Option<Self>[src]

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);

#[must_use]pub fn new_wrapping(val: u64) -> Self[src]

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::from(0u32));

#[must_use]pub const fn min_value() -> Self[src]

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::from(0u32));

#[must_use]pub const fn max_value() -> Self[src]

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());

#[must_use]pub fn is_power_of_two(self) -> bool[src]

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

Examples

Basic usage:

assert!(UInt::from(16u32).is_power_of_two());
assert!(!UInt::from(10u32).is_power_of_two());

#[must_use]pub fn checked_next_power_of_two(self) -> Option<Self>[src]

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::from(2u32).checked_next_power_of_two(), Some(UInt::from(2u32)));
assert_eq!(UInt::from(3u32).checked_next_power_of_two(), Some(UInt::from(4u32)));
assert_eq!(UInt::MAX.checked_next_power_of_two(), None);

pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>[src]

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::from(10u32)));

#[must_use]pub fn checked_add(self, rhs: Self) -> Option<Self>[src]

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

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

#[must_use]pub fn checked_sub(self, rhs: Self) -> Option<Self>[src]

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

Examples

Basic usage:

assert_eq!(UInt::from(1u32).checked_sub(UInt::from(1u32)), Some(UInt::from(0u32)));
assert_eq!(UInt::from(0u32).checked_sub(UInt::from(1u32)), None);

#[must_use]pub fn checked_mul(self, rhs: Self) -> Option<Self>[src]

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

Examples

Basic usage:

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

#[must_use]pub fn checked_div(self, rhs: Self) -> Option<Self>[src]

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

Examples

Basic usage:

assert_eq!(UInt::from(128u32).checked_div(UInt::from(2u32)), Some(UInt::from(64u32)));
assert_eq!(UInt::from(1u32).checked_div(UInt::from(0u32)), None);

#[must_use]pub fn checked_rem(self, rhs: Self) -> Option<Self>[src]

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

Examples

Basic usage:

assert_eq!(UInt::from(5u32).checked_rem(UInt::from(2u32)), Some(UInt::from(1u32)));
assert_eq!(UInt::from(5u32).checked_rem(UInt::from(0u32)), None);

#[must_use]pub fn checked_neg(self) -> Option<Self>[src]

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

Note that negating any positive integer will overflow.

Examples

Basic usage:

assert_eq!(UInt::from(0u32).checked_neg(), Some(UInt::from(0u32)));
assert_eq!(UInt::from(1u32).checked_neg(), None);

#[must_use]pub fn checked_pow(self, exp: u32) -> Option<Self>[src]

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

Examples

Basic usage:

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

#[must_use]pub fn saturating_add(self, rhs: Self) -> Self[src]

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

Examples

Basic usage:

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

#[must_use]pub fn saturating_sub(self, rhs: Self) -> Self[src]

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

Examples

Basic usage:

assert_eq!(UInt::from(100u32).saturating_sub(UInt::from(1u32)), UInt::from(99u32));
assert_eq!(UInt::from(1u32).saturating_sub(UInt::from(2u32)), UInt::from(0u32));

#[must_use]pub fn saturating_mul(self, rhs: Self) -> Self[src]

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

Examples

Basic usage:

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

#[must_use]pub fn saturating_pow(self, exp: u32) -> Self[src]

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

Examples

Basic usage:

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

Trait Implementations

impl Add<UInt> for UInt[src]

type Output = Self

The resulting type after applying the + operator.

impl AddAssign<UInt> for UInt[src]

impl Clone for UInt[src]

impl Copy for UInt[src]

impl Debug for UInt[src]

impl Default for UInt[src]

impl Display for UInt[src]

impl Div<UInt> for UInt[src]

type Output = Self

The resulting type after applying the / operator.

impl DivAssign<UInt> for UInt[src]

impl Eq for UInt[src]

impl From<UInt> for u64[src]

impl From<UInt> for u128[src]

impl From<UInt> for f64[src]

impl From<u16> for UInt[src]

impl From<u32> for UInt[src]

impl From<u8> for UInt[src]

impl FromStr for UInt[src]

type Err = ParseIntError

The associated error which can be returned from parsing.

impl Hash for UInt[src]

impl Mul<UInt> for UInt[src]

type Output = Self

The resulting type after applying the * operator.

impl MulAssign<UInt> for UInt[src]

impl Ord for UInt[src]

impl PartialEq<UInt> for UInt[src]

impl PartialOrd<UInt> for UInt[src]

impl<'a> Product<&'a UInt> for UInt[src]

impl Product<UInt> for UInt[src]

impl Rem<UInt> for UInt[src]

type Output = Self

The resulting type after applying the % operator.

impl RemAssign<UInt> for UInt[src]

impl StructuralEq for UInt[src]

impl StructuralPartialEq for UInt[src]

impl Sub<UInt> for UInt[src]

type Output = Self

The resulting type after applying the - operator.

impl SubAssign<UInt> for UInt[src]

impl<'a> Sum<&'a UInt> for UInt[src]

impl Sum<UInt> for UInt[src]

impl TryFrom<UInt> for u8[src]

type Error = StdTryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<UInt> for u16[src]

type Error = StdTryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<UInt> for u32[src]

type Error = StdTryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<i128> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<i16> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<i32> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<i64> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<i8> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<u128> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

impl TryFrom<u64> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

Auto Trait Implementations

impl RefUnwindSafe for UInt

impl Send for UInt

impl Sync for UInt

impl Unpin for UInt

impl UnwindSafe for UInt

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.