pub struct Bitfield<T> { /* private fields */ }Expand description
A heap-allocated, ordered, fixed-length, collection of bool values. Use of
BitList or BitVector type aliases is preferred
over direct use of this struct.
The T type parameter is used to define length behaviour with the Variable or Fixed marker
structs.
The length of the Bitfield is set at instantiation (i.e., runtime, not compile time). However,
use with a Variable sets a type-level (i.e., compile-time) maximum length and Fixed
provides a type-level fixed length.
Example
The example uses the following crate-level type aliases:
BitList<N>is an alias forBitfield<Variable<N>>BitVector<N>is an alias forBitfield<Fixed<N>>
use ssz_types::{BitVector, BitList, typenum};
// `BitList` has a type-level maximum length. The length of the list is specified at runtime
// and it must be less than or equal to `N`. After instantiation, `BitList` cannot grow or
// shrink.
type BitList8 = BitList<typenum::U8>;
// Creating a `BitList` with a larger-than-`N` capacity returns `None`.
assert!(BitList8::with_capacity(9).is_err());
let mut bitlist = BitList8::with_capacity(4).unwrap(); // `BitList` permits a capacity of less than the maximum.
assert!(bitlist.set(3, true).is_ok()); // Setting inside the instantiation capacity is permitted.
assert!(bitlist.set(5, true).is_err()); // Setting outside that capacity is not.
// `BitVector` has a type-level fixed length. Unlike `BitList`, it cannot be instantiated with a custom length
// or grow/shrink.
type BitVector8 = BitVector<typenum::U8>;
let mut bitvector = BitVector8::new();
assert_eq!(bitvector.len(), 8); // `BitVector` length is fixed at the type-level.
assert!(bitvector.set(7, true).is_ok()); // Setting inside the capacity is permitted.
assert!(bitvector.set(9, true).is_err()); // Setting outside the capacity is not.
Note
The internal representation of the bitfield is the same as that required by SSZ. The lowest
byte (by Vec index) stores the lowest bit-indices and the right-most bit stores the lowest
bit-index. E.g., smallvec![0b0000_0001, 0b0000_0010] has bits 0, 9 set.
Implementations§
source§impl<N: Unsigned + Clone> Bitfield<Variable<N>>
impl<N: Unsigned + Clone> Bitfield<Variable<N>>
sourcepub fn with_capacity(num_bits: usize) -> Result<Self, Error>
pub fn with_capacity(num_bits: usize) -> Result<Self, Error>
Instantiate with capacity for num_bits boolean values. The length cannot be grown or
shrunk after instantiation.
All bits are initialized to false.
Returns None if num_bits > N.
sourcepub fn into_bytes(self) -> SmallVec<[u8; 32]>
pub fn into_bytes(self) -> SmallVec<[u8; 32]>
Consumes self, returning a serialized representation.
The output is faithful to the SSZ encoding of self, such that a leading true bit is
used to indicate the length of the bitfield.
Example
use ssz_types::{BitList, typenum};
use smallvec::SmallVec;
type BitList8 = BitList<typenum::U8>;
let b = BitList8::with_capacity(4).unwrap();
assert_eq!(b.into_bytes(), SmallVec::from_buf([0b0001_0000]));sourcepub fn from_bytes(bytes: SmallVec<[u8; 32]>) -> Result<Self, Error>
pub fn from_bytes(bytes: SmallVec<[u8; 32]>) -> Result<Self, Error>
Instantiates a new instance from bytes. Consumes the same format that self.into_bytes()
produces (SSZ).
Returns None if bytes are not a valid encoding.
sourcepub fn intersection(&self, other: &Self) -> Self
pub fn intersection(&self, other: &Self) -> Self
Compute the intersection of two BitLists of potentially different lengths.
Return a new BitList with length equal to the shorter of the two inputs.
source§impl<N: Unsigned + Clone> Bitfield<Fixed<N>>
impl<N: Unsigned + Clone> Bitfield<Fixed<N>>
sourcepub fn new() -> Self
pub fn new() -> Self
Instantiate a new Bitfield with a fixed-length of N bits.
All bits are initialized to false.
sourcepub fn into_bytes(self) -> SmallVec<[u8; 32]>
pub fn into_bytes(self) -> SmallVec<[u8; 32]>
Consumes self, returning a serialized representation.
The output is faithful to the SSZ encoding of self.
Example
use ssz_types::{BitVector, typenum};
use smallvec::SmallVec;
type BitVector4 = BitVector<typenum::U4>;
assert_eq!(BitVector4::new().into_bytes(), SmallVec::from_buf([0b0000_0000]));sourcepub fn from_bytes(bytes: SmallVec<[u8; 32]>) -> Result<Self, Error>
pub fn from_bytes(bytes: SmallVec<[u8; 32]>) -> Result<Self, Error>
Instantiates a new instance from bytes. Consumes the same format that self.into_bytes()
produces (SSZ).
Returns None if bytes are not a valid encoding.
sourcepub fn intersection(&self, other: &Self) -> Self
pub fn intersection(&self, other: &Self) -> Self
Compute the intersection of two fixed-length Bitfields.
Return a new fixed-length Bitfield.
source§impl<T: BitfieldBehaviour> Bitfield<T>
impl<T: BitfieldBehaviour> Bitfield<T>
sourcepub fn set(&mut self, i: usize, value: bool) -> Result<(), Error>
pub fn set(&mut self, i: usize, value: bool) -> Result<(), Error>
Sets the i’th bit to value.
Returns None if i is out-of-bounds of self.
sourcepub fn get(&self, i: usize) -> Result<bool, Error>
pub fn get(&self, i: usize) -> Result<bool, Error>
Returns the value of the i’th bit.
Returns Error if i is out-of-bounds of self.
sourcepub fn into_raw_bytes(self) -> SmallVec<[u8; 32]>
pub fn into_raw_bytes(self) -> SmallVec<[u8; 32]>
Returns the underlying bytes representation of the bitfield.
sourcepub fn as_slice(&self) -> &[u8] ⓘ
pub fn as_slice(&self) -> &[u8] ⓘ
Returns a view into the underlying bytes representation of the bitfield.
sourcepub fn highest_set_bit(&self) -> Option<usize>
pub fn highest_set_bit(&self) -> Option<usize>
Returns the Some(i) where i is the highest index with a set bit. Returns None if
there are no set bits.
sourcepub fn iter(&self) -> BitIter<'_, T>
pub fn iter(&self) -> BitIter<'_, T>
Returns an iterator across bitfield bool values, starting at the lowest index.
sourcepub fn num_set_bits(&self) -> usize
pub fn num_set_bits(&self) -> usize
Returns the number of bits that are set to true.
sourcepub fn difference(&self, other: &Self) -> Self
pub fn difference(&self, other: &Self) -> Self
Compute the difference of this Bitfield and another of potentially different length.
sourcepub fn difference_inplace(&mut self, other: &Self)
pub fn difference_inplace(&mut self, other: &Self)
Compute the difference of this Bitfield and another of potentially different length.
Trait Implementations§
source§impl<N: Unsigned + Clone> Decode for Bitfield<Fixed<N>>
impl<N: Unsigned + Clone> Decode for Bitfield<Fixed<N>>
source§fn is_ssz_fixed_len() -> bool
fn is_ssz_fixed_len() -> bool
true if this object has a fixed-length. Read moresource§fn ssz_fixed_len() -> usize
fn ssz_fixed_len() -> usize
source§fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
source§impl<N: Unsigned + Clone> Decode for Bitfield<Variable<N>>
impl<N: Unsigned + Clone> Decode for Bitfield<Variable<N>>
source§fn is_ssz_fixed_len() -> bool
fn is_ssz_fixed_len() -> bool
true if this object has a fixed-length. Read moresource§fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError>
source§fn ssz_fixed_len() -> usize
fn ssz_fixed_len() -> usize
source§impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield<Fixed<N>>
impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield<Fixed<N>>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Serde serialization is compliant with the Ethereum YAML test format.
source§impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield<Variable<N>>
impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield<Variable<N>>
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Serde serialization is compliant with the Ethereum YAML test format.
source§impl<N: Unsigned + Clone> Encode for Bitfield<Fixed<N>>
impl<N: Unsigned + Clone> Encode for Bitfield<Fixed<N>>
source§fn is_ssz_fixed_len() -> bool
fn is_ssz_fixed_len() -> bool
true if this object has a fixed-length. Read moresource§fn ssz_bytes_len(&self) -> usize
fn ssz_bytes_len(&self) -> usize
self is serialized. Read moresource§fn ssz_fixed_len() -> usize
fn ssz_fixed_len() -> usize
source§impl<N: Unsigned + Clone> Encode for Bitfield<Variable<N>>
impl<N: Unsigned + Clone> Encode for Bitfield<Variable<N>>
source§fn is_ssz_fixed_len() -> bool
fn is_ssz_fixed_len() -> bool
true if this object has a fixed-length. Read moresource§fn ssz_bytes_len(&self) -> usize
fn ssz_bytes_len(&self) -> usize
self is serialized. Read more