pub struct UInt(_);
Expand description
An integer limited to the range of non-negative integers that can be represented exactly by an f64.
Implementations
sourceimpl UInt
impl UInt
sourcepub const MIN: Self = Self(0)
pub const MIN: Self = Self(0)
The smallest value that can be represented by this integer type.
Examples
Basic usage:
assert_eq!(UInt::MIN, uint!(0));
sourcepub const MAX: Self = Self(MAX_SAFE_UINT)
pub const MAX: Self = Self(MAX_SAFE_UINT)
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());
sourcepub fn new(val: u64) -> Option<Self>
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);
sourcepub fn new_wrapping(val: u64) -> Self
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));
sourcepub fn new_saturating(val: u64) -> Self
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);
sourcepub const fn min_value() -> Self
👎 Deprecated: Use UInt::MIN
instead.
pub const fn min_value() -> Self
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));
sourcepub const fn max_value() -> Self
👎 Deprecated: Use UInt::MAX
instead.
pub const fn max_value() -> Self
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());
sourcepub fn is_power_of_two(self) -> bool
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());
sourcepub fn checked_next_power_of_two(self) -> Option<Self>
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);
sourcepub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>
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)));
sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
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);
sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
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);
sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
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);
sourcepub fn checked_div(self, rhs: Self) -> Option<Self>
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);
sourcepub fn checked_rem(self, rhs: Self) -> Option<Self>
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);
sourcepub fn checked_neg(self) -> Option<Self>
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);
sourcepub fn checked_pow(self, exp: u32) -> Option<Self>
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);
sourcepub fn saturating_add(self, rhs: Self) -> Self
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);
sourcepub fn saturating_sub(self, rhs: Self) -> Self
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));
sourcepub fn saturating_mul(self, rhs: Self) -> Self
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);
sourcepub fn saturating_pow(self, exp: u32) -> Self
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
sourceimpl AddAssign<UInt> for UInt
impl AddAssign<UInt> for UInt
sourcefn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Performs the +=
operation. Read more
sourceimpl<'de> Deserialize<'de> for UInt
impl<'de> Deserialize<'de> for UInt
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl DivAssign<UInt> for UInt
impl DivAssign<UInt> for UInt
sourcefn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
Performs the /=
operation. Read more
sourceimpl MulAssign<UInt> for UInt
impl MulAssign<UInt> for UInt
sourcefn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
Performs the *=
operation. Read more
sourceimpl Ord for UInt
impl Ord for UInt
sourceimpl PartialOrd<UInt> for UInt
impl PartialOrd<UInt> for UInt
sourcefn partial_cmp(&self, other: &UInt) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl RemAssign<UInt> for UInt
impl RemAssign<UInt> for UInt
sourcefn rem_assign(&mut self, other: Self)
fn rem_assign(&mut self, other: Self)
Performs the %=
operation. Read more
sourceimpl SubAssign<UInt> for UInt
impl SubAssign<UInt> for UInt
sourcefn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Performs the -=
operation. Read more
sourceimpl TryFrom<UInt> for u8
impl TryFrom<UInt> for u8
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<UInt> for i8
impl TryFrom<UInt> for i8
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<UInt> for u16
impl TryFrom<UInt> for u16
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<UInt> for i16
impl TryFrom<UInt> for i16
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<UInt> for u32
impl TryFrom<UInt> for u32
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<UInt> for i32
impl TryFrom<UInt> for i32
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<UInt> for usize
impl TryFrom<UInt> for usize
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<UInt> for isize
impl TryFrom<UInt> for isize
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: UInt) -> Result<Self, TryFromIntError>
fn try_from(val: UInt) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<i128> for UInt
impl TryFrom<i128> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: i128) -> Result<Self, TryFromIntError>
fn try_from(val: i128) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<i16> for UInt
impl TryFrom<i16> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: i16) -> Result<Self, TryFromIntError>
fn try_from(val: i16) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<i32> for UInt
impl TryFrom<i32> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: i32) -> Result<Self, TryFromIntError>
fn try_from(val: i32) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<i64> for UInt
impl TryFrom<i64> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: i64) -> Result<Self, TryFromIntError>
fn try_from(val: i64) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<i8> for UInt
impl TryFrom<i8> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: i8) -> Result<Self, TryFromIntError>
fn try_from(val: i8) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<isize> for UInt
impl TryFrom<isize> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: isize) -> Result<Self, TryFromIntError>
fn try_from(val: isize) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<u128> for UInt
impl TryFrom<u128> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: u128) -> Result<Self, TryFromIntError>
fn try_from(val: u128) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<u64> for UInt
impl TryFrom<u64> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: u64) -> Result<Self, TryFromIntError>
fn try_from(val: u64) -> Result<Self, TryFromIntError>
Performs the conversion.
sourceimpl TryFrom<usize> for UInt
impl TryFrom<usize> for UInt
type Error = TryFromIntError
type Error = TryFromIntError
The type returned in the event of a conversion error.
sourcefn try_from(val: usize) -> Result<Self, TryFromIntError>
fn try_from(val: usize) -> Result<Self, TryFromIntError>
Performs the conversion.
impl Copy for UInt
impl Eq for UInt
impl StructuralEq for UInt
impl StructuralPartialEq for UInt
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more