Struct js_int::UInt

source ·
pub struct UInt(/* private fields */);
Expand description

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

Implementations§

source§

impl UInt

source

pub const MIN: Self = _

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

§Examples

Basic usage:

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

pub const MAX: Self = _

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

pub fn new(val: u64) -> Option<Self>

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

pub fn new_wrapping(val: u64) -> Self

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

pub fn new_saturating(val: u64) -> Self

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

pub const fn min_value() -> Self

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

pub const fn max_value() -> Self

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

pub fn is_power_of_two(self) -> bool

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

pub fn checked_next_power_of_two(self) -> Option<Self>

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

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

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

pub fn checked_add(self, rhs: Self) -> Option<Self>

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

pub fn checked_sub(self, rhs: Self) -> Option<Self>

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

pub fn checked_mul(self, rhs: Self) -> Option<Self>

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

pub fn checked_div(self, rhs: Self) -> Option<Self>

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

pub fn checked_rem(self, rhs: Self) -> Option<Self>

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

pub fn checked_neg(self) -> Option<Self>

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

pub fn checked_pow(self, exp: u32) -> Option<Self>

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

pub fn saturating_add(self, rhs: Self) -> Self

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

pub fn saturating_sub(self, rhs: Self) -> Self

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

pub fn saturating_mul(self, rhs: Self) -> Self

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

pub fn saturating_pow(self, exp: u32) -> Self

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§

source§

impl Add for UInt

§

type Output = UInt

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
source§

impl AddAssign for UInt

source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
source§

impl Clone for UInt

source§

fn clone(&self) -> UInt

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for UInt

source§

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

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

impl Default for UInt

source§

fn default() -> UInt

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

impl<'de> Deserialize<'de> for UInt

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for UInt

source§

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

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

impl Div for UInt

§

type Output = UInt

The resulting type after applying the / operator.
source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
source§

impl DivAssign for UInt

source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
source§

impl From<UInt> for Int

source§

fn from(val: UInt) -> Self

Converts to this type from the input type.
source§

impl From<UInt> for f64

source§

fn from(val: UInt) -> Self

Converts to this type from the input type.
source§

impl From<UInt> for i128

source§

fn from(val: UInt) -> Self

Converts to this type from the input type.
source§

impl From<UInt> for i64

source§

fn from(val: UInt) -> Self

Converts to this type from the input type.
source§

impl From<UInt> for u128

source§

fn from(val: UInt) -> Self

Converts to this type from the input type.
source§

impl From<UInt> for u64

source§

fn from(val: UInt) -> Self

Converts to this type from the input type.
source§

impl From<u16> for UInt

source§

fn from(val: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for UInt

source§

fn from(val: u32) -> Self

Converts to this type from the input type.
source§

impl From<u8> for UInt

source§

fn from(val: u8) -> Self

Converts to this type from the input type.
source§

impl FromStr for UInt

§

type Err = ParseIntError

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

fn from_str(src: &str) -> Result<Self, Self::Err>

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

impl Hash for UInt

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 Mul for UInt

§

type Output = UInt

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
source§

impl MulAssign for UInt

source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
source§

impl Ord for UInt

source§

fn cmp(&self, other: &UInt) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for UInt

source§

fn eq(&self, other: &UInt) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for UInt

source§

fn partial_cmp(&self, other: &UInt) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl<'a> Product<&'a UInt> for UInt

source§

fn product<I>(iter: I) -> Self
where I: Iterator<Item = &'a UInt>,

Method which takes an iterator and generates Self from the elements by multiplying the items.
source§

impl Product for UInt

source§

fn product<I>(iter: I) -> Self
where I: Iterator<Item = UInt>,

Method which takes an iterator and generates Self from the elements by multiplying the items.
source§

impl Rem for UInt

§

type Output = UInt

The resulting type after applying the % operator.
source§

fn rem(self, rhs: Self) -> Self

Performs the % operation. Read more
source§

impl RemAssign for UInt

source§

fn rem_assign(&mut self, other: Self)

Performs the %= operation. Read more
source§

impl Serialize for UInt

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Sub for UInt

§

type Output = UInt

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
source§

impl SubAssign for UInt

source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
source§

impl<'a> Sum<&'a UInt> for UInt

source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = &'a UInt>,

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

impl Sum for UInt

source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = UInt>,

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

impl TryFrom<UInt> for i16

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<UInt> for i32

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<UInt> for i8

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<UInt> for isize

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<UInt> for u16

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<UInt> for u32

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<UInt> for u8

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<UInt> for usize

§

type Error = TryFromIntError

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

fn try_from(val: UInt) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<i128> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: i128) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<i16> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: i16) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<i32> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: i32) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<i64> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: i64) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<i8> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: i8) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<isize> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: isize) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<u128> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: u128) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<u64> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: u64) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl TryFrom<usize> for UInt

§

type Error = TryFromIntError

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

fn try_from(val: usize) -> Result<Self, TryFromIntError>

Performs the conversion.
source§

impl Copy for UInt

source§

impl Eq for UInt

source§

impl StructuralPartialEq for UInt

Auto Trait Implementations§

§

impl Freeze for UInt

§

impl RefUnwindSafe for UInt

§

impl Send for UInt

§

impl Sync for UInt

§

impl Unpin for UInt

§

impl UnwindSafe for UInt

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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,

§

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§

default 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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,