Enum rust_hdl::prelude::Bits

source ·
pub enum Bits<const N: usize> {
    // some variants omitted
}
Expand description

The Bits type holds a bit array of size [N].

Implementations§

source§

impl<const N: usize> Bits<N>

source

pub fn any(&self) -> bool

The [any] function returns true if any of the individual bits are true, and false otherwise. This reduction operation is equivalent to a logical OR of all the bits in the vector.

let x : Bits<14> = bits(0xDEA);
assert_eq!(x.any(), true);
let y : Bits<14> = Bits::default();
assert_eq!(y.any(), false);
source

pub fn all(&self) -> bool

The [all] function returns true if all of the individual bits are true, and false otherwise. This reduction operation is equivalent to a logical AND of all the bits in the vector.

let x : Bits<14> = bits(0xDEA);
assert_eq!(x.all(), false);
let y : Bits<14> = bits(0x3FFF);
assert_eq!(y.all(), true);
source

pub fn xor(&self) -> bool

The [xor] function computes the exclusive OR of all the bits in the vector. This is equivalent to counting the number of ones. If the number is odd, the XOR will be true. If even, it will be false.

let x: Bits<12> = bits(0b1100_0100_1100);
assert_eq!(x.xor(), true);
let y: Bits<12> = bits(0b1100_0110_1100);
assert_eq!(y.xor(), false);
source

pub fn index(&self) -> usize

The [index] function is used when a Bits is going to be used to index into an array or some other bit vector. This is typically a very specialized hardware operation, so there are limited cases in which it can be used. Also, there is an assumption that the Bits being used as an index is sufficiently small to fit in a natural word (assume 32 bits, here for safety). In practice, that means, that if you are indexing into a register using some other register/value, the length of the register is limited to a few billion bits.

let x: Bits<12> = bits(0b1100_0100_1100);
assert_eq!(x.index(), 0b1100_0100_1100_usize);
source

pub fn len(&self) -> usize

Return the number of bits in the current Bits. Because this is determined at compile time, it is of limited use as a runtime function, but is there nonetheless.

let x : Bits<14> = Bits::default();
assert_eq!(x.len(), 14);
source

pub fn is_empty(&self) -> bool

Return true if this Bits contains no actual bits (i.e., N = 0).

source

pub fn count() -> u128

Compute the number of possible values that a Bits can take. This is basically 2 raised to the Nth power. Because the result is returned as a usize, you must be careful, since this can easily overflow. A Bits<256> for example, cannot represent [count] on a normal 64 bit machine.

assert_eq!(Bits::<16>::count(), 1 << 16);
source

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

Extract the [index] bit from the given Bits. This will cause a runtime panic if the [index] bit is out of range of the width of the bitvector.

let x : Bits<14> = bits(0b10110);
assert_eq!(x.get_bit(0), false);
assert_eq!(x.get_bit(1), true);
assert_eq!(x.get_bit(2), true); // You get the idea
source

pub fn replace_bit(&self, index: usize, val: bool) -> Bits<N>

Replace the given bit of a Bits with a new bit value. This method leaves the original value alone, and returns a new Bits with all bits except the designated one left alone.

let x: Bits<16> = bits(0b1100_0000);
let x = x.replace_bit(0, true);
let x = x.replace_bit(7, false);
assert_eq!(x, bits(0b0100_0001));
source

pub fn get_bits<const M: usize>(&self, index: usize) -> Bits<M>

Return a subset of bits from a Bits value, with a given offset. To preserve the feasibility of representing this in hardware, the width of the result must be fixed (the argument [M]), and only the offset can be computed.

let x: Bits<40> = bits(0xDEAD_BEEF_CA);
let y = x.get_bits::<32>(8);
assert_eq!(y, bits(0xDEAD_BEEF))
source

pub fn set_bits<const M: usize>(&mut self, index: usize, rhs: Bits<M>)

Set a group of bits in a value. This operation modifies the value in place.

let mut x: Bits<40> = bits(0xDEAD_BEEF_CA);
x.set_bits::<16>(8, bits(0xCAFE));
assert_eq!(x, bits(0xDEAD_CAFE_CA));
source

pub fn mask() -> Bits<N>

Returns a Bits value that contains [N] ones.

let x = Bits::<40>::mask();
assert_eq!(x, bits(0xFF_FFFF_FFFF));
source

pub const fn width() -> usize

Returns the width in bits of the [BitVec]. Note that this is the number of bits allocated. It does not depend on the value at all.

assert_eq!(Bits::<40>::width(), 40);
source

pub fn to_u8(self) -> u8

Convert Bits to an u8.

let x : Bits<6> = 12.into();
let y = x.to_u8();
assert_eq!(y, 12_u8);

Note that this will panic if the width of the bitvector is larger than 8 bits.

let x: Bits<12> = 0xADE.into();
let y = x.to_u8(); // Panics - too many bits
source

pub fn to_u16(self) -> u16

Convert Bits to an u16.

let x : Bits<12> = 12.into();
let y = x.to_u16();
assert_eq!(y, 12_u16);

Note that this will panic if the width of the bitvector is larger than 16 bits.

let x: Bits<20> = 0xADE.into();
let y = x.to_u16(); // Panics - too many bits
source

pub fn to_u32(self) -> u32

Convert Bits to an u32.

let x : Bits<24> = 12.into();
let y = x.to_u32();
assert_eq!(y, 12_u32);

Note that this will panic if the width of the bitvector is larger than 32 bits.

let x: Bits<40> = 0xADE.into();
let y = x.to_u32(); // Panics - too many bits
source

pub fn to_u64(self) -> u64

Convert Bits to an u64.

let x : Bits<40> = 12.into();
let y = x.to_u64();
assert_eq!(y, 12_u64);

Note that this will panic if the width of the bitvector is larger than 64 bits.

let x: Bits<80> = 0xADE.into();
let y = x.to_u64(); // Panics - too many bits
source

pub fn to_u128(self) -> u128

Convert Bits to an u128.

let x : Bits<80> = 12.into();
let y = x.to_u128();
assert_eq!(y, 12_u128);

Note that this will panic if the width of the bitvector is larger than 128 bits.

let x: Bits<140> = 0xADE.into();
let y = x.to_u128(); // Panics - too many bits

Trait Implementations§

source§

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

§

type Output = Bits<N>

The resulting type after applying the + operator.
source§

fn add(self, rhs: u64) -> <Bits<N> as Add<u64>>::Output

Performs the + operation. Read more
source§

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

Allows you to format a Bits as a binary string

let y = Bits::<16>::from(0b1011_0100_0010_0000);
println!("y = {:b}", y); // Prints y = 1011010000100000
source§

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

Formats the value using the given formatter.
source§

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

§

type Output = Bits<N>

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: u64) -> <Bits<N> as BitAnd<u64>>::Output

Performs the & operation. Read more
source§

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

§

type Output = Bits<N>

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: u64) -> <Bits<N> as BitOr<u64>>::Output

Performs the | operation. Read more
source§

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

§

type Output = Bits<N>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: u64) -> <Bits<N> as BitXor<u64>>::Output

Performs the ^ operation. Read more
source§

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

source§

fn clone(&self) -> Bits<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<const N: usize> Debug for Bits<N>

source§

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

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

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

Construct a default Bits - i.e., a zero bit vector of length N.

let x : Bits<200> = Default::default();
assert_eq!(x, bits(0));
source§

fn default() -> Bits<N>

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

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

Convert from a BigUint to a Bits. Will panic if the number of bits needed to represent the value are greater than the width of the Bits.

let x = BigUint::parse_bytes(b"10111000101", 2).unwrap();
let y : Bits<16> = x.into();
println!("y = {:x}", y); // Prints y = 02c5

The following will panic, because the value cannot be represented in the given number of bits.

let x = BigUint::parse_bytes(b"10111000101", 2).unwrap();
let y : Bits<12> = x.into(); // Panics
source§

fn from(x: BigUint) -> Bits<N>

Converts to this type from the input type.
source§

impl<const N: usize> From<Wrapping<u64>> for Bits<N>

source§

fn from(x: Wrapping<u64>) -> Bits<N>

Converts to this type from the input type.
source§

impl From<bool> for Bits<1>

source§

fn from(x: bool) -> Bits<1>

Convenience method that allows you to convert a boolean into a single bit-width Bits.

let x : Bits<1> = true.into();
assert_eq!(x, bits(1))
source§

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

Convert from LiteralType to Bits. Because of some restrictions on how Rust’s type inference works, when you work with unsized literals (e.g., x = 3), there must either be a unique integer type that can fit the expression, or it will default to i32. Unfortunately, in general, Bits are used to represent unsigned types. The upshot of all this is that RustHDL selects one unsigned integer type to represent literals. Although not ideal, RustHDL uses a LiteralType (currently ‘u64’) to represent literals so as to make HDL expressions close to Verilog or VHDL. This choice should not affect any hardware implementation, as hardware registers need to be of Bits type.

let x: Bits<16> = 0xDEAD.into(); // This is interpreteed as a 128 bit constant by Rust

This example is the largest bit width literal you can express using current edition Rust:

let x: Bits<128> = 0xDEADBEEF_CAFEBABE_1234ABCD_00005EA1_u128.to_bits();

From a safety perspective, RustHDL will panic if the argument is too large to fit into the bit vector. Thus, this example will panic, since the literal cannot be fit into 16 bits without truncation:

let x: Bits<16> = 0xDEADBEEF.into(); // This will panic!
source§

fn from(x: u64) -> Bits<N>

Converts to this type from the input type.
source§

impl Into<Bits<rust_hdl_widgets::::sdram::cmd::{impl#8}::{constant#0}>> for SDRAMCommand

source§

fn into( self ) -> Bits<rust_hdl_widgets::::sdram::cmd::{impl#8}::into::{constant#0}>

Converts this type into the (usually inferred) input type.
source§

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

Allows you to format a Bits as a lowercase hex string

let y = Bits::<16>::from(0xcafe);
println!("y = {:x}", y); // Prints y = cafe
source§

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

Formats the value using the given formatter.
source§

impl Mul<Bits<16>> for Bits<16>

Multipliers are special, so we only implement multipliers that we think are synthesizable. In this case, we implement a 16 x 16 bit multiplier which yields a 32 bit result.

§

type Output = Bits<32>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Bits<16>) -> <Bits<16> as Mul<Bits<16>>>::Output

Performs the * operation. Read more
source§

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

Bitwise inversion of a Bits vector The ! operator will invert each bit in a Bits vector.

let x : Bits<16> = bits(0xAAAA);
let y = !x;
assert_eq!(y, bits(0x5555))
§

type Output = Bits<N>

The resulting type after applying the ! operator.
source§

fn not(self) -> <Bits<N> as Not>::Output

Performs the unary ! operation. Read more
source§

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

§

type Output = Bits<N>

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

fn shl(self, rhs: Bits<M>) -> <Bits<N> as Shl<Bits<M>>>::Output

Performs the << operation. Read more
source§

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

§

type Output = Bits<N>

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

fn shl(self, rhs: u64) -> <Bits<N> as Shl<u64>>::Output

Performs the << operation. Read more
source§

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

§

type Output = Bits<N>

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

fn shr(self, rhs: Bits<M>) -> <Bits<N> as Shr<Bits<M>>>::Output

Performs the >> operation. Read more
source§

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

§

type Output = Bits<N>

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

fn shr(self, rhs: u64) -> <Bits<N> as Shr<u64>>::Output

Performs the >> operation. Read more
source§

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

§

type Output = Bits<N>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: u64) -> <Bits<N> as Sub<u64>>::Output

Performs the - operation. Read more
source§

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

source§

const BITS: usize = N

source§

fn descriptor() -> TypeDescriptor

source§

fn vcd(self) -> VCDValue

source§

fn verilog(self) -> VerilogLiteral

source§

fn bits(self) -> usize

source§

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

Allows you to format a Bits as an uppercase hex string

let y = Bits::<16>::from(0xcafe);
println!("y = {:X}", y); // Prints y = CAFE
source§

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

Formats the value using the given formatter.
source§

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

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<M> Measure for Mwhere M: Debug + PartialOrd<M> + Add<M, Output = M> + Default + Clone,

source§

impl<N> NodeTrait for Nwhere N: Copy + Ord + Hash,