[][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]

#[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_value()));
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_value());
assert_eq!(UInt::new_wrapping(js_int::MAX_SAFE_UINT + 1), UInt::from(0u32));

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

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]

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_value().checked_next_power_of_two(), None);

#[must_use] 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_value() - UInt::from(2u32)).checked_add(UInt::from(1u32)),
    Some(UInt::max_value() - UInt::from(1u32))
);
assert_eq!(
    (UInt::max_value() - 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_value().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_value().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_value().saturating_add(UInt::from(1u32)), UInt::max_value());

#[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_value().saturating_mul(UInt::from(2u32)), UInt::max_value());
assert_eq!(UInt::max_value().saturating_mul(UInt::max_value()), UInt::max_value());

#[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:

use js_int::UInt;
assert_eq!(UInt::from(5u32).saturating_pow(2), UInt::from(25u32));
assert_eq!(UInt::max_value().saturating_pow(2), UInt::max_value());

Trait Implementations

impl PartialEq<UInt> for UInt[src]

impl Copy for UInt[src]

impl Eq for UInt[src]

impl Default for UInt[src]

impl From<u8> for UInt[src]

impl From<u16> for UInt[src]

impl From<u32> for UInt[src]

impl From<UInt> for u64[src]

impl From<UInt> for f64[src]

impl Clone for UInt[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Hash for UInt[src]

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

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

impl Add<UInt> for UInt[src]

type Output = Self

The resulting type after applying the + operator.

impl Sub<UInt> for UInt[src]

type Output = Self

The resulting type after applying the - operator.

impl Mul<UInt> for UInt[src]

type Output = Self

The resulting type after applying the * operator.

impl Div<UInt> for UInt[src]

type Output = Self

The resulting type after applying the / operator.

impl Rem<UInt> for UInt[src]

type Output = Self

The resulting type after applying the % operator.

impl AddAssign<UInt> for UInt[src]

impl SubAssign<UInt> for UInt[src]

impl MulAssign<UInt> for UInt[src]

impl DivAssign<UInt> for UInt[src]

impl RemAssign<UInt> for UInt[src]

impl Debug for UInt[src]

impl Display for UInt[src]

impl TryFrom<u64> for UInt[src]

type Error = TryFromIntError

The type returned in the event of a conversion error.

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<i8> 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 FromStr for UInt[src]

type Err = ParseIntError

The associated error which can be returned from parsing.

Auto Trait Implementations

impl Send for UInt

impl Unpin for UInt

impl Sync for UInt

impl UnwindSafe for UInt

impl RefUnwindSafe for UInt

Blanket Implementations

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

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

type Owned = T

The resulting type after obtaining ownership.

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[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.

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

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

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