Type Alias BitUsize

Source
pub type BitUsize<const N: u32> = BitUint<usize, N>;
Expand description

A specialized BitUint type whose the underlying type is restricted to usize.

The largest size of N is equal to usize::BITS.

Aliased Type§

struct BitUsize<const N: u32>(/* private fields */);

Implementations

Source§

impl<T: Unsigned + PrimInt, const N: u32> BitUint<T, N>

Source

pub const unsafe fn new_unchecked(n: T) -> Self

Creates a new BitUint with the given unsigned integer value.

This method does not check whether the value is a valid N-bit unsigned integer. This results in undefined behaviour if the value is not a valid N-bit unsigned integer.

§Safety

The value must be a valid N-bit unsigned integer.

Source

pub const fn get(self) -> T

Returns the contained value as the underlying type.

Source§

impl<const N: u32> BitUint<usize, N>

Source

pub const fn new(n: usize) -> Option<Self>

Creates a new BitUint with the given unsigned integer value.

Returns None if the value is not a valid N-bit unsigned integer.

§Examples
let n = BitUint::<usize, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<usize, 5>::new(42);
assert_eq!(m, None);
Source§

impl<const N: u32> BitUint<usize, N>

Source

pub const MIN: Self

The smallest value that can be represented by this BitUint.

The value is always 0.

§Examples
assert_eq!(BitUint::<usize, 7>::MIN.get(), 0);
Source

pub const MAX: Self

The largest value that can be represented by this BitUint.

§Examples
assert_eq!(BitUint::<usize, 7>::MAX.get(), 127);
Source

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<usize, 7>::BITS, 7);
Source§

impl<const N: u32> BitUint<usize, N>

Source

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

Calculates the addition of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_add(21).map(BitUint::get), Some(63));
assert_eq!(n.checked_add(22), None);
Source

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

Calculates the subtraction of rhs from self.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_sub(42).map(BitUint::get), Some(0));
assert_eq!(n.checked_sub(43), None);
Source

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

Calculates the multiplication of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(21).unwrap();

assert_eq!(n.checked_mul(2).map(BitUint::get), Some(42));
assert_eq!(n.checked_mul(4), None);
Source

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

Calculates the divisor when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_div(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div(0), None);
Source

pub const fn checked_div_euclid(self, rhs: usize) -> Option<Self>

Calculates the quotient of Euclidean division of self by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_div_euclid(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div_euclid(0), None);
Source

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

Calculates the remainder when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem(0), None);
Source

pub const fn checked_rem_euclid(self, rhs: usize) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 3>::new(5).unwrap();

assert_eq!(n.checked_rem_euclid(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem_euclid(0), None);
Source

pub const fn checked_ilog(self, base: usize) -> Option<u32>

Returns the logarithm of the number with respect to an arbitrary base, rounded down.

Returns None if the number is zero, or if the base is not at least 2.

§Examples
let n = BitUint::<usize, 3>::new(5).unwrap();

assert_eq!(n.checked_ilog(5), Some(1));
Source

pub const fn checked_ilog2(self) -> Option<u32>

Returns the base 2 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<usize, 2>::new(2).unwrap();

assert_eq!(n.checked_ilog2(), Some(1));
Source

pub const fn checked_ilog10(self) -> Option<u32>

Returns the base 10 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<usize, 4>::new(10).unwrap();

assert_eq!(n.checked_ilog10(), Some(1));
Source

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

Negates self.

Returns None unless self is 0.

Note that negating any positive integer will overflow.

§Examples
assert_eq!(
    BitUint::<usize, 1>::MIN.checked_neg().map(BitUint::get),
    Some(0)
);
assert_eq!(BitUint::<usize, 1>::MAX.checked_neg(), None);
Source

pub const fn checked_shl(self, rhs: u32) -> Option<Self>

Shifts self left by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<usize, 5>::new(0x01).unwrap();
let m = BitUint::<usize, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(usize::BITS - 1).map(BitUint::get), Some(0x00));
Source

pub const fn checked_shr(self, rhs: u32) -> Option<Self>

Shifts self right by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<usize, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shr(4).map(BitUint::get), Some(0x01));
assert_eq!(n.checked_shr(129), None);
Source

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

Raises self to the power of exp, using exponentiation by squaring.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(2).unwrap();

assert_eq!(n.checked_pow(5).map(BitUint::get), Some(32));
assert_eq!(BitUint::<usize, 6>::MAX.checked_pow(2), None);

Trait Implementations

Source§

impl<T: Unsigned + PrimInt + Binary, const N: u32> Binary for BitUint<T, N>

Source§

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

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

impl<T: Clone + Unsigned + PrimInt, const N: u32> Clone for BitUint<T, N>

Source§

fn clone(&self) -> BitUint<T, N>

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<T: Debug + Unsigned + PrimInt, const N: u32> Debug for BitUint<T, N>

Source§

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

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

impl<T: Default + Unsigned + PrimInt, const N: u32> Default for BitUint<T, N>

Source§

fn default() -> BitUint<T, N>

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

impl<T: Unsigned + PrimInt + Display, const N: u32> Display for BitUint<T, N>

Source§

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

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

impl<T: Hash + Unsigned + PrimInt, const N: u32> Hash for BitUint<T, N>

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<T: Unsigned + PrimInt + LowerExp, const N: u32> LowerExp for BitUint<T, N>

Source§

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

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

impl<T: Unsigned + PrimInt + LowerHex, const N: u32> LowerHex for BitUint<T, N>

Source§

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

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

impl<T: Unsigned + PrimInt + Octal, const N: u32> Octal for BitUint<T, N>

Source§

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

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

impl<T: Ord + Unsigned + PrimInt, const N: u32> Ord for BitUint<T, N>

Source§

fn cmp(&self, other: &BitUint<T, N>) -> 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,

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

impl<T: PartialEq + Unsigned + PrimInt, const N: u32> PartialEq for BitUint<T, N>

Source§

fn eq(&self, other: &BitUint<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + Unsigned + PrimInt, const N: u32> PartialOrd for BitUint<T, N>

Source§

fn partial_cmp(&self, other: &BitUint<T, N>) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Unsigned + PrimInt + UpperExp, const N: u32> UpperExp for BitUint<T, N>

Source§

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

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

impl<T: Unsigned + PrimInt + UpperHex, const N: u32> UpperHex for BitUint<T, N>

Source§

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

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

impl<T: Copy + Unsigned + PrimInt, const N: u32> Copy for BitUint<T, N>

Source§

impl<T: Eq + Unsigned + PrimInt, const N: u32> Eq for BitUint<T, N>

Source§

impl<T: Unsigned + PrimInt, const N: u32> StructuralPartialEq for BitUint<T, N>