Struct Bits

Source
pub struct Bits<const N: usize>(/* private fields */);
Expand description

The Bits type is a fixed-sized bit vector. It is meant to imitate the behavior of bit vectors in hardware. Due to the design of the Bits type, you can only create a Bits type of up to 128 bits in length for now. However, you can easily express larger constructs in hardware using arrays, tuples and structs. The only real limitation of the Bits type being 128 bits is that you cannot perform arbitrary arithmetic on longer bit values in your hardware designs. I don’t think this is a significant issue, but the Bits design of the rust-hdl crate was much slower and harder to maintain and use. I think this is a good trade-off.

Note that the Bits type implements 2’s complement arithmetic. See https://en.wikipedia.org/wiki/Two%27s_complement for more information.

Note also that the Bits kind is treated as an unsigned value for the purposes of comparisons. If you need signed comparisons, you will need the SignedBits type.

Implementations§

Source§

impl<const N: usize> Bits<N>

Source

pub const MASK: Self

Defines a constant Bits value with all bits set to 1.

Source

pub const fn mask() -> Self

Return a Bits value with all bits set to 1.

let bits = Bits::<8>::mask();
assert_eq!(bits, 0xFF);
Source

pub fn set_bit(&mut self, bit: usize, value: bool)

Set a specific bit of a Bits value to 1 or 0. Panics if the index of the bit is outside the range of the Bits value.

let mut bits = Bits::<8>::mask();
bits.set_bit(0, false);
assert_eq!(bits, 0xFE);
bits.set_bit(0, true);
assert_eq!(bits, 0xFF);
Source

pub fn get_bit(&self, bit: usize) -> bool

Get the value of a specific bit of a Bits value. Panics if the index of the bit is outside the range of the Bits value.

let bits : Bits<8> = 0b1101_1010.into();
assert!(!bits.get_bit(0));
assert!(bits.get_bit(7));
assert!(bits.get_bit(6));
Source

pub fn any(self) -> bool

Returns true if any of the bits are set to 1.

let bits : Bits<8> = 0b1101_1010.into();
assert!(bits.any());
let bits : Bits<8> = 0.into();
assert!(!bits.any());
Source

pub fn all(self) -> bool

Returns true if all of the bits are set to 1.

let bits : Bits<8> = 0b1101_1010.into();
assert!(!bits.all());
let bits : Bits<8> = Bits::mask();
assert!(bits.all());
Source

pub fn xor(self) -> bool

Computes the xor of all of the bits in the value.

Source

pub fn slice<const M: usize>(&self, start: usize) -> Bits<M>

Extracts a range of bits from the Bits value.

Source

pub fn as_signed(self) -> SignedBits<N>

Reinterpret the Bits value as a SignedBits value.

Trait Implementations§

Source§

impl<const N: usize> Add<Bits<N>> for u128

Source§

type Output = Bits<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Bits<N>) -> Self::Output

Performs the + operation. Read more
Source§

impl<const N: usize> Add<u128> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const N: usize> Add for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const N: usize> AddAssign<u128> for Bits<N>

Source§

fn add_assign(&mut self, rhs: u128)

Performs the += operation. Read more
Source§

impl<const N: usize> AddAssign for Bits<N>

Source§

fn add_assign(&mut self, rhs: Bits<N>)

Performs the += operation. Read more
Source§

impl<const N: usize> Binary for Bits<N>

Source§

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

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

impl<const N: usize> BitAnd<Bits<N>> for u128

Source§

type Output = Bits<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Bits<N>) -> Self::Output

Performs the & operation. Read more
Source§

impl<const N: usize> BitAnd<u128> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: u128) -> Self::Output

Performs the & operation. Read more
Source§

impl<const N: usize> BitAnd for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Bits<N>) -> Bits<N>

Performs the & operation. Read more
Source§

impl<const N: usize> BitAndAssign<u128> for Bits<N>

Source§

fn bitand_assign(&mut self, rhs: u128)

Performs the &= operation. Read more
Source§

impl<const N: usize> BitAndAssign for Bits<N>

Source§

fn bitand_assign(&mut self, rhs: Bits<N>)

Performs the &= operation. Read more
Source§

impl<const N: usize> BitOr<Bits<N>> for u128

Source§

type Output = Bits<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Bits<N>) -> Self::Output

Performs the | operation. Read more
Source§

impl<const N: usize> BitOr<u128> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: u128) -> Self::Output

Performs the | operation. Read more
Source§

impl<const N: usize> BitOr for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Bits<N>) -> Bits<N>

Performs the | operation. Read more
Source§

impl<const N: usize> BitOrAssign<u128> for Bits<N>

Source§

fn bitor_assign(&mut self, rhs: u128)

Performs the |= operation. Read more
Source§

impl<const N: usize> BitOrAssign for Bits<N>

Source§

fn bitor_assign(&mut self, rhs: Bits<N>)

Performs the |= operation. Read more
Source§

impl<const N: usize> BitXor<Bits<N>> for u128

Source§

type Output = Bits<N>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Bits<N>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<const N: usize> BitXor<u128> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: u128) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<const N: usize> BitXor for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Bits<N>) -> Bits<N>

Performs the ^ operation. Read more
Source§

impl<const N: usize> BitXorAssign<u128> for Bits<N>

Source§

fn bitxor_assign(&mut self, rhs: u128)

Performs the ^= operation. Read more
Source§

impl<const N: usize> BitXorAssign for Bits<N>

Source§

fn bitxor_assign(&mut self, rhs: Bits<N>)

Performs the ^= operation. Read more
Source§

impl<const N: usize> Clone for Bits<N>

Source§

fn clone(&self) -> Bits<N>

Returns a duplicate 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<const N: usize> Debug for Bits<N>

Source§

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

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

impl<const N: usize> Default for Bits<N>

The default value for a Bits value is 0.

Source§

fn default() -> Self

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

impl<const N: usize> Display for Bits<N>

Source§

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

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

impl<const N: usize> From<u128> for Bits<N>

Provide conversion from a u128 to a Bits value. This will panic if you try to convert a value that is larger than the Bits value can hold.

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> Hash for Bits<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<const N: usize> LowerHex for Bits<N>

Source§

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

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

impl<const N: usize> Not for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<const N: usize> Ord for Bits<N>

Source§

fn cmp(&self, other: &Bits<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<const N: usize> PartialEq<u128> for Bits<N>

Source§

fn eq(&self, other: &u128) -> 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<const N: usize> PartialEq for Bits<N>

Source§

fn eq(&self, other: &Bits<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<const N: usize> PartialOrd for Bits<N>

Source§

fn partial_cmp(&self, other: &Bits<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<const M: usize, const N: usize> Shl<Bits<M>> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Bits<M>) -> Self::Output

Performs the << operation. Read more
Source§

impl<const M: usize, const N: usize> Shl<Bits<M>> for SignedBits<N>

Source§

type Output = SignedBits<N>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Bits<M>) -> Self::Output

Performs the << operation. Read more
Source§

impl<const N: usize> Shl<Bits<N>> for i128

Source§

type Output = SignedBits<N>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Bits<N>) -> Self::Output

Performs the << operation. Read more
Source§

impl<const N: usize> Shl<Bits<N>> for u128

Source§

type Output = Bits<N>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Bits<N>) -> Self::Output

Performs the << operation. Read more
Source§

impl<const N: usize> Shl<u128> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: u128) -> Self::Output

Performs the << operation. Read more
Source§

impl<const M: usize, const N: usize> ShlAssign<Bits<M>> for Bits<N>

Source§

fn shl_assign(&mut self, rhs: Bits<M>)

Performs the <<= operation. Read more
Source§

impl<const M: usize, const N: usize> ShlAssign<Bits<M>> for SignedBits<N>

Source§

fn shl_assign(&mut self, rhs: Bits<M>)

Performs the <<= operation. Read more
Source§

impl<const N: usize> ShlAssign<u128> for Bits<N>

Source§

fn shl_assign(&mut self, rhs: u128)

Performs the <<= operation. Read more
Source§

impl<const M: usize, const N: usize> Shr<Bits<M>> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Bits<M>) -> Self::Output

Performs the >> operation. Read more
Source§

impl<const M: usize, const N: usize> Shr<Bits<M>> for SignedBits<N>

Source§

type Output = SignedBits<N>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Bits<M>) -> Self::Output

Performs the >> operation. Read more
Source§

impl<const N: usize> Shr<Bits<N>> for u128

Source§

type Output = Bits<N>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Bits<N>) -> Self::Output

Performs the >> operation. Read more
Source§

impl<const N: usize> Shr<u128> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: u128) -> Self::Output

Performs the >> operation. Read more
Source§

impl<const M: usize, const N: usize> ShrAssign<Bits<M>> for Bits<N>

Source§

fn shr_assign(&mut self, rhs: Bits<M>)

Performs the >>= operation. Read more
Source§

impl<const M: usize, const N: usize> ShrAssign<Bits<M>> for SignedBits<N>

Source§

fn shr_assign(&mut self, rhs: Bits<M>)

Performs the >>= operation. Read more
Source§

impl<const N: usize> ShrAssign<u128> for Bits<N>

Source§

fn shr_assign(&mut self, rhs: u128)

Performs the >>= operation. Read more
Source§

impl<const N: usize> Sub<Bits<N>> for u128

Source§

type Output = Bits<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Bits<N>) -> Self::Output

Performs the - operation. Read more
Source§

impl<const N: usize> Sub<u128> for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const N: usize> Sub for Bits<N>

Source§

type Output = Bits<N>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const N: usize> SubAssign<u128> for Bits<N>

Source§

fn sub_assign(&mut self, rhs: u128)

Performs the -= operation. Read more
Source§

impl<const N: usize> SubAssign for Bits<N>

Source§

fn sub_assign(&mut self, rhs: Bits<N>)

Performs the -= operation. Read more
Source§

impl<const N: usize> UpperHex for Bits<N>

Source§

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

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

impl<const N: usize> Copy for Bits<N>

Source§

impl<const N: usize> Eq for Bits<N>

Source§

impl<const N: usize> StructuralPartialEq for Bits<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Bits<N>

§

impl<const N: usize> RefUnwindSafe for Bits<N>

§

impl<const N: usize> Send for Bits<N>

§

impl<const N: usize> Sync for Bits<N>

§

impl<const N: usize> Unpin for Bits<N>

§

impl<const N: usize> UnwindSafe for Bits<N>

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, dest: *mut u8)

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

Source§

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§

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

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.