bit_int

Struct BitUint

Source
pub struct BitUint<T: Unsigned + PrimInt, const N: u32>(/* private fields */);
Expand description

BitUint is a type that represents a N-bit unsigned integer.

The largest size of N is equal to the size of the underlying type in bits.

§Examples

use bit_int::BitUint;

let n = BitUint::<u32, 33>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

Implementations§

Source§

impl<const N: u32> BitUint<u8, 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::<u8, 7>::MIN.get(), 0);
Source

pub const MAX: Self = _

The largest value that can be represented by this BitUint.

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

pub const BITS: u32 = N

The size of this BitUint in bits.

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

impl<const N: u32> BitUint<u16, 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::<u16, 7>::MIN.get(), 0);
Source

pub const MAX: Self = _

The largest value that can be represented by this BitUint.

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

pub const BITS: u32 = N

The size of this BitUint in bits.

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

impl<const N: u32> BitUint<u32, 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::<u32, 7>::MIN.get(), 0);
Source

pub const MAX: Self = _

The largest value that can be represented by this BitUint.

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

pub const BITS: u32 = N

The size of this BitUint in bits.

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

impl<const N: u32> BitUint<u64, 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::<u64, 7>::MIN.get(), 0);
Source

pub const MAX: Self = _

The largest value that can be represented by this BitUint.

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

pub const BITS: u32 = N

The size of this BitUint in bits.

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

impl<const N: u32> BitUint<u128, 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::<u128, 7>::MIN.get(), 0);
Source

pub const MAX: Self = _

The largest value that can be represented by this BitUint.

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

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<u128, 7>::BITS, 7);
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<u8, N>

Source

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

Computes self + rhs, returning None if overflow occurred.

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

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

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

Computes self - rhs, returning None if overflow occurred.

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

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

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

Computes self * rhs, returning None if overflow occurred.

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

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

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

Computes self / rhs, returning None if rhs == 0.

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

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

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

Source

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

Computes self + rhs, returning None if overflow occurred.

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

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

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

Computes self - rhs, returning None if overflow occurred.

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

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

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

Computes self * rhs, returning None if overflow occurred.

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

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

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

Computes self / rhs, returning None if rhs == 0.

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

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

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

Source

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

Computes self + rhs, returning None if overflow occurred.

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

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

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

Computes self - rhs, returning None if overflow occurred.

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

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

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

Computes self * rhs, returning None if overflow occurred.

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

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

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

Computes self / rhs, returning None if rhs == 0.

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

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

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

Source

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

Computes self + rhs, returning None if overflow occurred.

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

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

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

Computes self - rhs, returning None if overflow occurred.

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

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

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

Computes self * rhs, returning None if overflow occurred.

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

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

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

Computes self / rhs, returning None if rhs == 0.

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

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

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

Source

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

Computes self + rhs, returning None if overflow occurred.

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

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

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

Computes self - rhs, returning None if overflow occurred.

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

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

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

Computes self * rhs, returning None if overflow occurred.

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

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

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

Computes self / rhs, returning None if rhs == 0.

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

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

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

Source

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

Computes self + rhs, returning 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!(n.checked_add(22).is_none());
Source

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

Computes self - rhs, returning 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!(n.checked_sub(43).is_none());
Source

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

Computes self * rhs, returning 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!(n.checked_mul(4).is_none());
Source

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

Computes self / rhs, returning None if rhs == 0.

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

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

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

Source

pub const fn new(n: u8) -> 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::<u8, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u8, 5>::new(42);
assert!(m.is_none());
Source§

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

Source

pub const fn new(n: u16) -> 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::<u16, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u16, 5>::new(42);
assert!(m.is_none());
Source§

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

Source

pub const fn new(n: u32) -> 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::<u32, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u32, 5>::new(42);
assert!(m.is_none());
Source§

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

Source

pub const fn new(n: u64) -> 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::<u64, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u64, 5>::new(42);
assert!(m.is_none());
Source§

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

Source

pub const fn new(n: u128) -> 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::<u128, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u128, 5>::new(42);
assert!(m.is_none());
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!(m.is_none());
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.

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>

Auto Trait Implementations§

§

impl<T, const N: u32> Freeze for BitUint<T, N>
where T: Freeze,

§

impl<T, const N: u32> RefUnwindSafe for BitUint<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: u32> Send for BitUint<T, N>
where T: Send,

§

impl<T, const N: u32> Sync for BitUint<T, N>
where T: Sync,

§

impl<T, const N: u32> Unpin for BitUint<T, N>
where T: Unpin,

§

impl<T, const N: u32> UnwindSafe for BitUint<T, N>
where T: UnwindSafe,

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§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.