Expand description
The ring of numbers modulo $2^{\mathtt{BITS}}$.
Implementations
sourceimpl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
sourcepub fn to_base_le(&self, base: u64) -> impl Iterator<Item = u64>
pub fn to_base_le(&self, base: u64) -> impl Iterator<Item = u64>
Returns an iterator over the base base digits of the number in
little-endian order.
Pro tip: instead of setting base = 10, set it to the highest
power of 10 that still fits u64. This way much fewer iterations
are required to extract all the digits.
Panics
Panics if the base is less than 2.
sourcepub fn to_base_be(&self, base: u64) -> impl Iterator<Item = u64>
pub fn to_base_be(&self, base: u64) -> impl Iterator<Item = u64>
Returns an iterator over the base base digits of the number in
big-endian order.
Pro tip: instead of setting base = 10, set it to the highest
power of 10 that still fits u64. This way much fewer iterations
are required to extract all the digits.
Panics
Panics if the base is less than 2.
sourcepub fn from_base_le<I: IntoIterator<Item = u64>>(
base: u64,
digits: I
) -> Result<Self, BaseConvertError>
pub fn from_base_le<I: IntoIterator<Item = u64>>(
base: u64,
digits: I
) -> Result<Self, BaseConvertError>
Constructs the Uint from digits in the base base in little-endian.
Errors
BaseConvertError::InvalidBaseif the base is less than 2.BaseConvertError::InvalidDigitif a digit is out of range.BaseConvertError::Overflowif the number is too large to fit.
sourcepub fn from_base_be<I: IntoIterator<Item = u64>>(
base: u64,
digits: I
) -> Result<Self, BaseConvertError>
pub fn from_base_be<I: IntoIterator<Item = u64>>(
base: u64,
digits: I
) -> Result<Self, BaseConvertError>
Constructs the Uint from digits in the base base in big-endian.
Errors
BaseConvertError::InvalidBaseif the base is less than 2.BaseConvertError::InvalidDigitif a digit is out of range.BaseConvertError::Overflowif the number is too large to fit.
sourceimpl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
pub const fn bit(&self, index: usize) -> bool
pub fn set_bit(&mut self, index: usize, value: bool)
sourcepub fn reverse_bits(self) -> Self
pub fn reverse_bits(self) -> Self
Reverses the order of bits in the integer. The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
sourcepub fn leading_zeros(&self) -> usize
pub fn leading_zeros(&self) -> usize
Returns the number of leading zeros in the binary representation of
self.
sourcepub fn leading_ones(&self) -> usize
pub fn leading_ones(&self) -> usize
Returns the number of leading zeros in the binary representation of
self.
sourcepub fn trailing_zeros(&self) -> usize
pub fn trailing_zeros(&self) -> usize
Returns the number of trailing zeros in the binary representation of
self.
sourcepub fn trailing_ones(&self) -> usize
pub fn trailing_ones(&self) -> usize
Returns the number of trailing ones in the binary representation of
self.
sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Returns the number of ones in the binary representation of self.
sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Returns the number of zeros in the binary representation of self.
sourcepub fn checked_log2(&self) -> Option<usize>
pub fn checked_log2(&self) -> Option<usize>
Returns the base 2 logarithm of the number, rounded down.
This is equivalent to the index of the highest set bit.
Returns None if the number is zero.
sourcepub fn most_significant_bits(&self) -> (u64, usize)
pub fn most_significant_bits(&self) -> (u64, usize)
Returns the most significant 64 bits of the number and the exponent.
Given return value $(\mathtt{bits}, \mathtt{exponent})$, the self can
be approximated as
$$ \mathtt{self} ≈ \mathtt{bits} ⋅ 2^\mathtt{exponent} $$
If self is $<≥> 2^{63}$, then exponent will be zero and bits will
have leading zeros.
sourcepub fn checked_shl(self, rhs: usize) -> Option<Self>
pub fn checked_shl(self, rhs: usize) -> Option<Self>
Checked left shift by rhs bits.
Returns $\mathtt{self} ⋅ 2^{\mathtt{rhs}}$ or None if the result
would $≥ 2^{\mathtt{BITS}}$. That is, it returns None if the bits
shifted out would be non-zero.
Note: This differs from u64::checked_shl which returns None if the
shift is larger than BITS (which is IMHO not very useful).
sourcepub fn overflowing_shl(self, rhs: usize) -> (Self, bool)
pub fn overflowing_shl(self, rhs: usize) -> (Self, bool)
Left shift by rhs bits with overflow detection.
Returns $\mod{\mathtt{value} ⋅ 2^{\mathtt{rhs}}}_{2^{\mathtt{BITS}}}$.
If the product is $≥ 2^{\mathtt{BITS}}$ it returns true. That is, it
returns true if the bits shifted out are non-zero.
Note: This differs from u64::overflowing_shl which returns true if
the shift is larger than BITS (which is IMHO not very useful).
sourcepub fn wrapping_shl(self, rhs: usize) -> Self
pub fn wrapping_shl(self, rhs: usize) -> Self
Left shift by rhs bits.
Returns $\mod{\mathtt{value} ⋅ 2^{\mathtt{rhs}}}_{2^{\mathtt{BITS}}}$.
Note: This differs from u64::wrapping_shl which first reduces rhs
by BITS (which is IMHO not very useful).
sourcepub fn checked_shr(self, rhs: usize) -> Option<Self>
pub fn checked_shr(self, rhs: usize) -> Option<Self>
Checked right shift by rhs bits.
$$ \frac{\mathtt{self}}{2^{\mathtt{rhs}}} $$
Returns the above or None if the division is not exact. This is the
same as
Note: This differs from u64::checked_shr which returns None if the
shift is larger than BITS (which is IMHO not very useful).
sourcepub fn overflowing_shr(self, rhs: usize) -> (Self, bool)
pub fn overflowing_shr(self, rhs: usize) -> (Self, bool)
Right shift by rhs bits with underflow detection.
$$ \floor{\frac{\mathtt{self}}{2^{\mathtt{rhs}}}} $$
Returns the above and false if the division was exact, and true if
it was rounded down. This is the same as non-zero bits being shifted
out.
Note: This differs from u64::overflowing_shl which returns true if
the shift is larger than BITS (which is IMHO not very useful).
sourcepub fn wrapping_shr(self, rhs: usize) -> Self
pub fn wrapping_shr(self, rhs: usize) -> Self
Right shift by rhs bits.
$$ \mathtt{wrapping\_shr}(\mathtt{self}, \mathtt{rhs}) = \floor{\frac{\mathtt{self}}{2^{\mathtt{rhs}}}} $$
Note: This differs from u64::wrapping_shr which first reduces rhs
by BITS (which is IMHO not very useful).
sourcepub fn rotate_left(self, rhs: usize) -> Self
pub fn rotate_left(self, rhs: usize) -> Self
Shifts the bits to the left by a specified amount, rhs, wrapping the
truncated bits to the end of the resulting integer.
pub fn rotate_right(self, rhs: usize) -> Self
sourceimpl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
sourcepub const BYTES: usize = (BITS + 7) / 8
pub const BYTES: usize = (BITS + 7) / 8
The size of this integer type in bytes. Note that some bits may be forced zero if BITS is not cleanly divisible by eight.
sourcepub fn as_le_slice(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_le_slice(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Access the underlying store as a little-endian slice of bytes.
Only available on litte-endian targets.
If BITS does not evenly divide 8, it is padded with zero bits in the
most significant position.
sourcepub unsafe fn as_le_slice_mut(&mut self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub unsafe fn as_le_slice_mut(&mut self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
sourcepub fn as_le_bytes(&self) -> Cow<'_, [u8]>
pub fn as_le_bytes(&self) -> Cow<'_, [u8]>
Access the underlying store as a little-endian bytes.
Uses an optimized implementation on little-endian targets.
sourcepub fn as_le_bytes_trimmed(&self) -> Cow<'_, [u8]>
pub fn as_le_bytes_trimmed(&self) -> Cow<'_, [u8]>
Access the underlying store as a little-endian bytes with trailing zeros removed.
Uses an optimized implementation on little-endian targets.
sourcepub fn to_le_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
pub fn to_le_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
Converts the Uint to a little-endian byte array of size exactly
Self::BYTES.
Panics
Panics if the generic parameter BYTES is not exactly Self::BYTES.
Ideally this would be a compile time error, but this is blocked by
Rust issue #60551.
sourcepub fn to_le_bytes_vec(&self) -> Vec<u8>
pub fn to_le_bytes_vec(&self) -> Vec<u8>
Converts the Uint to a little-endian byte vector of size exactly
Self::BYTES.
This method is useful when Self::to_le_bytes can not be used because
byte size is not known compile time.
sourcepub fn to_le_bytes_trimmed_vec(&self) -> Vec<u8>
pub fn to_le_bytes_trimmed_vec(&self) -> Vec<u8>
Converts the Uint to a little-endian byte vector with trailing zeros
bytes removed.
sourcepub fn to_be_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
pub fn to_be_bytes<const BYTES: usize>(&self) -> [u8; BYTES]
Converts the Uint to a big-endian byte array of size exactly
Self::BYTES.
Panics
Panics if the generic parameter BYTES is not exactly Self::BYTES.
Ideally this would be a compile time error, but this is blocked by
Rust issue #60551.
sourcepub fn to_be_bytes_vec(&self) -> Vec<u8>
pub fn to_be_bytes_vec(&self) -> Vec<u8>
Converts the Uint to a big-endian byte vector of size exactly
Self::BYTES.
This method is useful when Self::to_be_bytes can not be used because
byte size is not known compile time.
sourcepub fn to_be_bytes_trimmed_vec(&self) -> Vec<u8>
pub fn to_be_bytes_trimmed_vec(&self) -> Vec<u8>
Converts the Uint to a big-endian byte vector with leading zeros
bytes removed.
sourcepub fn from_be_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
pub fn from_be_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
Converts a big-endian byte array of size exactly
Self::BYTES to Uint.
Panics
Panics if the generic parameter BYTES is not exactly Self::BYTES.
Ideally this would be a compile time error, but this is blocked by
Rust issue #60551.
Panics if the value is too large for the bit-size of the Uint.
sourcepub fn from_le_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
pub fn from_le_bytes<const BYTES: usize>(bytes: [u8; BYTES]) -> Self
Converts a little-endian byte array of size exactly
Self::BYTES to Uint.
Panics
Panics if the generic parameter BYTES is not exactly Self::BYTES.
Ideally this would be a compile time error, but this is blocked by
Rust issue #60551.
Panics if the value is too large for the bit-size of the Uint.
sourceimpl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
sourcepub fn from_str_radix(src: &str, radix: u64) -> Result<Self, ParseError>
pub fn from_str_radix(src: &str, radix: u64) -> Result<Self, ParseError>
Parse a string into a Uint.
For bases 2 to 36, the case-agnostic alphabet 0—1, a—b is used and _
are ignored. For bases 37 to 64, the case-sensitive alphabet a—z, A—Z,
0—9, {+-}, {/,_} is used. That is, for base 64 it is compatible with
all the common base64 variants.
Errors
ParseError::InvalidDigitif the string contains a non-digit.ParseError::InvalidRadixif the radix is larger than 64.ParseError::BaseConvertErrorifUint::from_base_befails.
sourceimpl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS>
sourcepub const MIN: Self = Self::ZERO
pub const MIN: Self = Self::ZERO
The smallest value that can be represented by this integer type.
Synonym for Self::ZERO.
sourcepub const ZERO: Self = Self { limbs: [0; LIMBS] }
pub const ZERO: Self = Self { limbs: [0; LIMBS] }
The value zero. This is the only value that exists in all Uint
types.
sourcepub const MAX: Self = {
let mut limbs = [u64::MAX; LIMBS];
if BITS > 0 {
limbs[LIMBS - 1] &= Self::MASK;
}
Self { limbs }
}
pub const MAX: Self = { let mut limbs = [u64::MAX; LIMBS]; if BITS > 0 { limbs[LIMBS - 1] &= Self::MASK; } Self { limbs } }
The largest value that can be represented by this integer type, $2^{\mathtt{BITS}} − 1$.
sourcepub fn as_limbs_mut(&mut self) -> &mut [u64; LIMBS]
pub fn as_limbs_mut(&mut self) -> &mut [u64; LIMBS]
Access the array of limbs.
sourcepub const fn into_limbs(self) -> [u64; LIMBS]
pub const fn into_limbs(self) -> [u64; LIMBS]
Convert to a array of limbs.
Limbs are least significant first.
sourcepub const fn from_limbs(limbs: [u64; LIMBS]) -> Self
pub const fn from_limbs(limbs: [u64; LIMBS]) -> Self
Panics
Panics it LIMBS is not equal to nlimbs(BITS).
Panics if the value is to large for the bit-size of the Uint.
pub fn from_limbs_slice(slice: &[u64]) -> Self
Trait Implementations
sourceimpl<const BITS: usize, const LIMBS: usize> BitAnd<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitAnd<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourceimpl<const BITS: usize, const LIMBS: usize> BitAnd<&'_ Uint<BITS, LIMBS>> for &Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitAnd<&'_ Uint<BITS, LIMBS>> for &Uint<BITS, LIMBS>
sourceimpl<const BITS: usize, const LIMBS: usize> BitAndAssign<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitAndAssign<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourcefn bitand_assign(&mut self, rhs: &Uint<BITS, LIMBS>)
fn bitand_assign(&mut self, rhs: &Uint<BITS, LIMBS>)
Performs the &= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> BitAndAssign<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitAndAssign<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourcefn bitand_assign(&mut self, rhs: Uint<BITS, LIMBS>)
fn bitand_assign(&mut self, rhs: Uint<BITS, LIMBS>)
Performs the &= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> BitOr<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitOr<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourceimpl<const BITS: usize, const LIMBS: usize> BitOr<&'_ Uint<BITS, LIMBS>> for &Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitOr<&'_ Uint<BITS, LIMBS>> for &Uint<BITS, LIMBS>
sourceimpl<const BITS: usize, const LIMBS: usize> BitOrAssign<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitOrAssign<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourcefn bitor_assign(&mut self, rhs: &Uint<BITS, LIMBS>)
fn bitor_assign(&mut self, rhs: &Uint<BITS, LIMBS>)
Performs the |= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> BitOrAssign<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitOrAssign<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourcefn bitor_assign(&mut self, rhs: Uint<BITS, LIMBS>)
fn bitor_assign(&mut self, rhs: Uint<BITS, LIMBS>)
Performs the |= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> BitXor<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitXor<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourceimpl<const BITS: usize, const LIMBS: usize> BitXor<&'_ Uint<BITS, LIMBS>> for &Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitXor<&'_ Uint<BITS, LIMBS>> for &Uint<BITS, LIMBS>
sourceimpl<const BITS: usize, const LIMBS: usize> BitXorAssign<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitXorAssign<&'_ Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourcefn bitxor_assign(&mut self, rhs: &Uint<BITS, LIMBS>)
fn bitxor_assign(&mut self, rhs: &Uint<BITS, LIMBS>)
Performs the ^= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> BitXorAssign<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> BitXorAssign<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourcefn bitxor_assign(&mut self, rhs: Uint<BITS, LIMBS>)
fn bitxor_assign(&mut self, rhs: Uint<BITS, LIMBS>)
Performs the ^= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> OverflowingAdd for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> OverflowingAdd for Uint<BITS, LIMBS>
fn overflowing_add(self, other: Self) -> (Self, bool)
sourceimpl<const BITS: usize, const LIMBS: usize> PartialEq<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> PartialEq<Uint<BITS, LIMBS>> for Uint<BITS, LIMBS>
sourceimpl<const BITS: usize, const LIMBS: usize> ShlAssign<&'_ usize> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<&'_ usize> for Uint<BITS, LIMBS>
sourcefn shl_assign(&mut self, rhs: &usize)
fn shl_assign(&mut self, rhs: &usize)
Performs the <<= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> ShlAssign<usize> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShlAssign<usize> for Uint<BITS, LIMBS>
sourcefn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
Performs the <<= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> ShrAssign<&'_ usize> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<&'_ usize> for Uint<BITS, LIMBS>
sourcefn shr_assign(&mut self, rhs: &usize)
fn shr_assign(&mut self, rhs: &usize)
Performs the >>= operation. Read more
sourceimpl<const BITS: usize, const LIMBS: usize> ShrAssign<usize> for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> ShrAssign<usize> for Uint<BITS, LIMBS>
sourcefn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
Performs the >>= operation. Read more
impl<const BITS: usize, const LIMBS: usize> Copy for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Eq for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> StructuralEq for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> StructuralPartialEq for Uint<BITS, LIMBS>
Auto Trait Implementations
impl<const BITS: usize, const LIMBS: usize> RefUnwindSafe for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Send for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Sync for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> Unpin for Uint<BITS, LIMBS>
impl<const BITS: usize, const LIMBS: usize> UnwindSafe for Uint<BITS, LIMBS>
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
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
Uses borrowed data to replace owned data, usually by cloning. Read more