Refactor SSZ types bitfield
Removes superfulous `Bitfield` struct
This commit is contained in:
parent
5943e176cf
commit
2b7d5560ad
@ -12,7 +12,7 @@ tree_hash = { path = "../tree_hash" }
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_hex = { path = "../serde_hex" }
|
||||
ssz = { path = "../ssz" }
|
||||
eth2_ssz = { path = "../ssz" }
|
||||
typenum = "1.10"
|
||||
|
||||
[dev-dependencies]
|
||||
|
@ -1,229 +0,0 @@
|
||||
use crate::bitfield::{Bitfield, Error};
|
||||
use crate::{FixedSizedError, VariableSizedError};
|
||||
use std::marker::PhantomData;
|
||||
use typenum::Unsigned;
|
||||
|
||||
/// Provides a common `impl` for structs that wrap a `Bitfield`.
|
||||
macro_rules! common_impl {
|
||||
($name: ident, $error: ident) => {
|
||||
impl<N: Unsigned> $name<N> {
|
||||
/// Create a new bitfield with the given length `initial_len` and all values set to `bit`.
|
||||
///
|
||||
/// Note: if `initial_len` is not a multiple of 8, the remaining bits will be set to `false`
|
||||
/// regardless of `bit`.
|
||||
pub fn from_elem(initial_len: usize, bit: bool) -> Result<Self, $error> {
|
||||
let bitfield = Bitfield::from_elem(initial_len, bit);
|
||||
Self::from_bitfield(bitfield)
|
||||
}
|
||||
|
||||
/// Create a new BitList using the supplied `bytes` as input
|
||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, $error> {
|
||||
let bitfield = Bitfield::from_bytes(bytes);
|
||||
Self::from_bitfield(bitfield)
|
||||
}
|
||||
|
||||
/// Returns a vector of bytes representing the bitfield
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
self.bitfield.to_bytes()
|
||||
}
|
||||
|
||||
/// Read the value of a bit.
|
||||
///
|
||||
/// If the index is in bounds, then result is Ok(value) where value is `true` if the bit is 1 and `false` if the bit is 0.
|
||||
/// If the index is out of bounds, we return an error to that extent.
|
||||
pub fn get(&self, i: usize) -> Result<bool, Error> {
|
||||
self.bitfield.get(i)
|
||||
}
|
||||
|
||||
fn capacity() -> usize {
|
||||
N::to_usize()
|
||||
}
|
||||
|
||||
/// Set the value of a bit.
|
||||
///
|
||||
/// Returns an `Err` if `i` is outside of the maximum permitted length.
|
||||
pub fn set(&mut self, i: usize, value: bool) -> Result<(), VariableSizedError> {
|
||||
if i < Self::capacity() {
|
||||
self.bitfield.set(i, value);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(VariableSizedError::ExceedsMaxLength {
|
||||
len: Self::capacity() + 1,
|
||||
max_len: Self::capacity(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of bits in this bitfield.
|
||||
pub fn len(&self) -> usize {
|
||||
self.bitfield.len()
|
||||
}
|
||||
|
||||
/// Returns true if `self.len() == 0`
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.bitfield.is_empty()
|
||||
}
|
||||
|
||||
/// Returns true if all bits are set to 0.
|
||||
pub fn is_zero(&self) -> bool {
|
||||
self.bitfield.is_zero()
|
||||
}
|
||||
|
||||
/// Returns the number of bytes required to represent this bitfield.
|
||||
pub fn num_bytes(&self) -> usize {
|
||||
self.bitfield.num_bytes()
|
||||
}
|
||||
|
||||
/// Returns the number of `1` bits in the bitfield
|
||||
pub fn num_set_bits(&self) -> usize {
|
||||
self.bitfield.num_set_bits()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Emulates a SSZ `Bitvector`.
|
||||
///
|
||||
/// An ordered, heap-allocated, fixed-length, collection of `bool` values, with `N` values.
|
||||
pub struct BitVector<N> {
|
||||
bitfield: Bitfield,
|
||||
_phantom: PhantomData<N>,
|
||||
}
|
||||
|
||||
common_impl!(BitVector, FixedSizedError);
|
||||
|
||||
impl<N: Unsigned> BitVector<N> {
|
||||
/// Create a new bitfield.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
bitfield: Bitfield::with_capacity(N::to_usize()),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_bitfield(bitfield: Bitfield) -> Result<Self, FixedSizedError> {
|
||||
if bitfield.len() != Self::capacity() {
|
||||
Err(FixedSizedError::InvalidLength {
|
||||
len: bitfield.len(),
|
||||
fixed_len: Self::capacity(),
|
||||
})
|
||||
} else {
|
||||
Ok(Self {
|
||||
bitfield,
|
||||
_phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Emulates a SSZ `Bitlist`.
|
||||
///
|
||||
/// An ordered, heap-allocated, variable-length, collection of `bool` values, limited to `N`
|
||||
/// values.
|
||||
pub struct BitList<N> {
|
||||
bitfield: Bitfield,
|
||||
_phantom: PhantomData<N>,
|
||||
}
|
||||
|
||||
common_impl!(BitList, VariableSizedError);
|
||||
|
||||
impl<N: Unsigned> BitList<N> {
|
||||
/// Create a new, empty BitList.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
bitfield: Bitfield::default(),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new BitList list with `initial_len` bits all set to `false`.
|
||||
pub fn with_capacity(initial_len: usize) -> Result<Self, VariableSizedError> {
|
||||
Self::from_elem(initial_len, false)
|
||||
}
|
||||
|
||||
/// The maximum possible number of bits.
|
||||
pub fn max_len() -> usize {
|
||||
N::to_usize()
|
||||
}
|
||||
|
||||
fn from_bitfield(bitfield: Bitfield) -> Result<Self, VariableSizedError> {
|
||||
if bitfield.len() > Self::max_len() {
|
||||
Err(VariableSizedError::ExceedsMaxLength {
|
||||
len: bitfield.len(),
|
||||
max_len: Self::max_len(),
|
||||
})
|
||||
} else {
|
||||
Ok(Self {
|
||||
bitfield,
|
||||
_phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the intersection (binary-and) of this bitfield with another
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn intersection(&self, other: &Self) -> Self {
|
||||
assert_eq!(self.len(), other.len());
|
||||
let bitfield = self.bitfield.intersection(&other.bitfield);
|
||||
Self::from_bitfield(bitfield).expect(
|
||||
"An intersection of two same-sized sets cannot be larger than one of the initial sets",
|
||||
)
|
||||
}
|
||||
|
||||
/// Like `intersection` but in-place (updates `self`).
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn intersection_inplace(&mut self, other: &Self) {
|
||||
self.bitfield.intersection_inplace(&other.bitfield);
|
||||
}
|
||||
|
||||
/// Compute the union (binary-or) of this bitfield with another. Lengths must match.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn union(&self, other: &Self) -> Self {
|
||||
assert_eq!(self.len(), other.len());
|
||||
let bitfield = self.bitfield.union(&other.bitfield);
|
||||
Self::from_bitfield(bitfield)
|
||||
.expect("A union of two same-sized sets cannot be larger than one of the initial sets")
|
||||
}
|
||||
|
||||
/// Like `union` but in-place (updates `self`).
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn union_inplace(&mut self, other: &Self) {
|
||||
self.bitfield.union_inplace(&other.bitfield)
|
||||
}
|
||||
|
||||
/// Compute the difference (binary-minus) of this bitfield with another. Lengths must match.
|
||||
///
|
||||
/// Computes `self - other`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn difference(&self, other: &Self) -> Self {
|
||||
assert_eq!(self.len(), other.len());
|
||||
let bitfield = self.bitfield.difference(&other.bitfield);
|
||||
Self::from_bitfield(bitfield).expect(
|
||||
"A difference of two same-sized sets cannot be larger than one of the initial sets",
|
||||
)
|
||||
}
|
||||
|
||||
/// Like `difference` but in-place (updates `self`).
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn difference_inplace(&mut self, other: &Self) {
|
||||
self.bitfield.difference_inplace(&other.bitfield)
|
||||
}
|
||||
}
|
@ -1,27 +1,21 @@
|
||||
use crate::FixedSizedError;
|
||||
use bit_reverse::LookupReverse;
|
||||
use bit_vec::BitVec;
|
||||
use cached_tree_hash::cached_tree_hash_bytes_as_list;
|
||||
use bit_vec::BitVec as Bitfield;
|
||||
use serde::de::{Deserialize, Deserializer};
|
||||
use serde::ser::{Serialize, Serializer};
|
||||
use serde_hex::{encode, PrefixedHexVisitor};
|
||||
use ssz::{Decode, Encode};
|
||||
use std::cmp;
|
||||
use std::default;
|
||||
use std::marker::PhantomData;
|
||||
use typenum::Unsigned;
|
||||
|
||||
/// A Bitfield represents a set of booleans compactly stored as a vector of bits.
|
||||
/// The Bitfield is given a fixed size during construction. Reads outside of the current size return an out-of-bounds error. Writes outside of the current size expand the size of the set.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bitfield(BitVec);
|
||||
|
||||
/// Error represents some reason a request against a bitfield was not satisfied
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
/// OutOfBounds refers to indexing into a bitfield where no bits exist; returns the illegal index and the current size of the bitfield, respectively
|
||||
OutOfBounds(usize, usize),
|
||||
}
|
||||
|
||||
impl Bitfield {
|
||||
pub fn with_capacity(initial_len: usize) -> Self {
|
||||
/// Provides a common `impl` for structs that wrap a `$name`.
|
||||
macro_rules! common_impl {
|
||||
($name: ident, $error: ident) => {
|
||||
impl<N: Unsigned> $name<N> {
|
||||
/// Create a new BitList list with `initial_len` bits all set to `false`.
|
||||
pub fn with_capacity(initial_len: usize) -> Result<Self, $error> {
|
||||
Self::from_elem(initial_len, false)
|
||||
}
|
||||
|
||||
@ -29,10 +23,13 @@ impl Bitfield {
|
||||
///
|
||||
/// Note: if `initial_len` is not a multiple of 8, the remaining bits will be set to `false`
|
||||
/// regardless of `bit`.
|
||||
pub fn from_elem(initial_len: usize, bit: bool) -> Self {
|
||||
pub fn from_elem(initial_len: usize, bit: bool) -> Result<Self, $error> {
|
||||
// BitVec can panic if we don't set the len to be a multiple of 8.
|
||||
let full_len = ((initial_len + 7) / 8) * 8;
|
||||
let mut bitfield = BitVec::from_elem(full_len, false);
|
||||
|
||||
Self::validate_length(full_len)?;
|
||||
|
||||
let mut bitfield = Bitfield::from_elem(full_len, false);
|
||||
|
||||
if bit {
|
||||
for i in 0..initial_len {
|
||||
@ -40,52 +37,71 @@ impl Bitfield {
|
||||
}
|
||||
}
|
||||
|
||||
Self { 0: bitfield }
|
||||
Ok(Self {
|
||||
bitfield,
|
||||
_phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new bitfield using the supplied `bytes` as input
|
||||
pub fn from_bytes(bytes: &[u8]) -> Self {
|
||||
Self {
|
||||
0: BitVec::from_bytes(&reverse_bit_order(bytes.to_vec())),
|
||||
}
|
||||
}
|
||||
pub fn from_bytes(bytes: &[u8]) -> Result<Self, $error> {
|
||||
Self::validate_length(bytes.len().saturating_mul(8))?;
|
||||
|
||||
Ok(Self {
|
||||
bitfield: Bitfield::from_bytes(&reverse_bit_order(bytes.to_vec())),
|
||||
_phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
/// Returns a vector of bytes representing the bitfield
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
reverse_bit_order(self.0.to_bytes().to_vec())
|
||||
reverse_bit_order(self.bitfield.to_bytes().to_vec())
|
||||
}
|
||||
|
||||
/// Read the value of a bit.
|
||||
///
|
||||
/// If the index is in bounds, then result is Ok(value) where value is `true` if the bit is 1 and `false` if the bit is 0.
|
||||
/// If the index is out of bounds, we return an error to that extent.
|
||||
pub fn get(&self, i: usize) -> Result<bool, Error> {
|
||||
match self.0.get(i) {
|
||||
/// If the index is in bounds, then result is Ok(value) where value is `true` if the
|
||||
/// bit is 1 and `false` if the bit is 0. If the index is out of bounds, we return an
|
||||
/// error to that extent.
|
||||
pub fn get(&self, i: usize) -> Result<bool, $error> {
|
||||
if i < N::to_usize() {
|
||||
match self.bitfield.get(i) {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(Error::OutOfBounds(i, self.0.len())),
|
||||
None => Err($error::OutOfBounds {
|
||||
i,
|
||||
len: self.bitfield.len(),
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
Err($error::InvalidLength {
|
||||
i,
|
||||
len: N::to_usize(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the value of a bit.
|
||||
///
|
||||
/// If the index is out of bounds, we expand the size of the underlying set to include the new index.
|
||||
/// Returns the previous value if there was one.
|
||||
pub fn set(&mut self, i: usize, value: bool) -> Option<bool> {
|
||||
let previous = match self.get(i) {
|
||||
/// If the index is out of bounds, we expand the size of the underlying set to include
|
||||
/// the new index. Returns the previous value if there was one.
|
||||
pub fn set(&mut self, i: usize, value: bool) -> Result<(), $error> {
|
||||
match self.get(i) {
|
||||
Ok(previous) => Some(previous),
|
||||
Err(Error::OutOfBounds(_, len)) => {
|
||||
Err($error::OutOfBounds { len, .. }) => {
|
||||
let new_len = i - len + 1;
|
||||
self.0.grow(new_len, false);
|
||||
self.bitfield.grow(new_len, false);
|
||||
None
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
self.0.set(i, value);
|
||||
previous
|
||||
|
||||
self.bitfield.set(i, value);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the number of bits in this bitfield.
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
self.bitfield.len()
|
||||
}
|
||||
|
||||
/// Returns true if `self.len() == 0`
|
||||
@ -95,7 +111,7 @@ impl Bitfield {
|
||||
|
||||
/// Returns true if all bits are set to 0.
|
||||
pub fn is_zero(&self) -> bool {
|
||||
self.0.none()
|
||||
self.bitfield.none()
|
||||
}
|
||||
|
||||
/// Returns the number of bytes required to represent this bitfield.
|
||||
@ -105,69 +121,24 @@ impl Bitfield {
|
||||
|
||||
/// Returns the number of `1` bits in the bitfield
|
||||
pub fn num_set_bits(&self) -> usize {
|
||||
self.0.iter().filter(|&bit| bit).count()
|
||||
self.bitfield.iter().filter(|&bit| bit).count()
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute the intersection (binary-and) of this bitfield with another. Lengths must match.
|
||||
pub fn intersection(&self, other: &Self) -> Self {
|
||||
let mut res = self.clone();
|
||||
res.intersection_inplace(other);
|
||||
res
|
||||
}
|
||||
|
||||
/// Like `intersection` but in-place (updates `self`).
|
||||
pub fn intersection_inplace(&mut self, other: &Self) {
|
||||
self.0.intersect(&other.0);
|
||||
}
|
||||
|
||||
/// Compute the union (binary-or) of this bitfield with another. Lengths must match.
|
||||
pub fn union(&self, other: &Self) -> Self {
|
||||
let mut res = self.clone();
|
||||
res.union_inplace(other);
|
||||
res
|
||||
}
|
||||
|
||||
/// Like `union` but in-place (updates `self`).
|
||||
pub fn union_inplace(&mut self, other: &Self) {
|
||||
self.0.union(&other.0);
|
||||
}
|
||||
|
||||
/// Compute the difference (binary-minus) of this bitfield with another. Lengths must match.
|
||||
///
|
||||
/// Computes `self - other`.
|
||||
pub fn difference(&self, other: &Self) -> Self {
|
||||
let mut res = self.clone();
|
||||
res.difference_inplace(other);
|
||||
res
|
||||
}
|
||||
|
||||
/// Like `difference` but in-place (updates `self`).
|
||||
pub fn difference_inplace(&mut self, other: &Self) {
|
||||
self.0.difference(&other.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl default::Default for Bitfield {
|
||||
/// default provides the "empty" bitfield
|
||||
/// Note: the empty bitfield is set to the `0` byte.
|
||||
fn default() -> Self {
|
||||
Self::from_elem(8, false)
|
||||
}
|
||||
}
|
||||
|
||||
impl cmp::PartialEq for Bitfield {
|
||||
/// Determines equality by comparing the `ssz` encoding of the two candidates.
|
||||
/// This method ensures that the presence of high-order (empty) bits in the highest byte do not exclude equality when they are in fact representing the same information.
|
||||
impl<N: Unsigned> cmp::PartialEq for $name<N> {
|
||||
/// Determines equality by comparing the `ssz` encoding of the two candidates. This
|
||||
/// method ensures that the presence of high-order (empty) bits in the highest byte do
|
||||
/// not exclude equality when they are in fact representing the same information.
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
ssz::ssz_encode(self) == ssz::ssz_encode(other)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new bitfield that is a union of two other bitfields.
|
||||
///
|
||||
/// For example `union(0101, 1000) == 1101`
|
||||
// TODO: length-independent intersection for BitAnd
|
||||
impl std::ops::BitOr for Bitfield {
|
||||
/// Create a new bitfield that is a union of two other bitfields.
|
||||
///
|
||||
/// For example `union(0101, 1000) == 1101`
|
||||
// TODO: length-independent intersection for BitAnd
|
||||
impl<N: Unsigned + Clone> std::ops::BitOr for $name<N> {
|
||||
type Output = Self;
|
||||
|
||||
fn bitor(self, other: Self) -> Self {
|
||||
@ -176,17 +147,18 @@ impl std::ops::BitOr for Bitfield {
|
||||
} else {
|
||||
(&other, &self)
|
||||
};
|
||||
let mut new = biggest.clone();
|
||||
let mut new = (*biggest).clone();
|
||||
for i in 0..smallest.len() {
|
||||
if let Ok(true) = smallest.get(i) {
|
||||
new.set(i, true);
|
||||
new.set(i, true)
|
||||
.expect("Cannot produce bitfield larger than smallest of two given");
|
||||
}
|
||||
}
|
||||
new
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for Bitfield {
|
||||
impl<N: Unsigned> Encode for $name<N> {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
false
|
||||
}
|
||||
@ -194,28 +166,20 @@ impl Encode for Bitfield {
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
buf.append(&mut self.to_bytes())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for Bitfield {
|
||||
impl<N: Unsigned> Decode for $name<N> {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
|
||||
Ok(Bitfield::from_bytes(bytes))
|
||||
$name::from_bytes(bytes)
|
||||
.map_err(|e| ssz::DecodeError::BytesInvalid(format!("Bitlist {:?}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse the bit order of a whole byte vec, so that the ith bit
|
||||
// of the input vec is placed in the (N - i)th bit of the output vec.
|
||||
// This function is necessary for converting bitfields to and from YAML,
|
||||
// as the BitVec library and the hex-parser use opposing bit orders.
|
||||
fn reverse_bit_order(mut bytes: Vec<u8>) -> Vec<u8> {
|
||||
bytes.reverse();
|
||||
bytes.into_iter().map(LookupReverse::swap_bits).collect()
|
||||
}
|
||||
|
||||
impl Serialize for Bitfield {
|
||||
impl<N: Unsigned> Serialize for $name<N> {
|
||||
/// Serde serialization is compliant with the Ethereum YAML test format.
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -223,9 +187,9 @@ impl Serialize for Bitfield {
|
||||
{
|
||||
serializer.serialize_str(&encode(self.to_bytes()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Bitfield {
|
||||
impl<'de, N: Unsigned> Deserialize<'de> for $name<N> {
|
||||
/// Serde serialization is compliant with the Ethereum YAML test format.
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
@ -235,11 +199,12 @@ impl<'de> Deserialize<'de> for Bitfield {
|
||||
// bit from the end of the hex string, e.g.
|
||||
// "0xef01" => [0xef, 0x01] => [0b1000_0000, 0b1111_1110]
|
||||
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
|
||||
Ok(Bitfield::from_bytes(&bytes))
|
||||
$name::from_bytes(&bytes)
|
||||
.map_err(|e| serde::de::Error::custom(format!("Bitlist {:?}", e)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl tree_hash::TreeHash for Bitfield {
|
||||
impl<N: Unsigned> tree_hash::TreeHash for $name<N> {
|
||||
fn tree_hash_type() -> tree_hash::TreeHashType {
|
||||
tree_hash::TreeHashType::List
|
||||
}
|
||||
@ -255,10 +220,221 @@ impl tree_hash::TreeHash for Bitfield {
|
||||
fn tree_hash_root(&self) -> Vec<u8> {
|
||||
self.to_bytes().tree_hash_root()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
cached_tree_hash_bytes_as_list!(Bitfield);
|
||||
// Reverse the bit order of a whole byte vec, so that the ith bit
|
||||
// of the input vec is placed in the (N - i)th bit of the output vec.
|
||||
// This function is necessary for converting bitfields to and from YAML,
|
||||
// as the BitVec library and the hex-parser use opposing bit orders.
|
||||
fn reverse_bit_order(mut bytes: Vec<u8>) -> Vec<u8> {
|
||||
bytes.reverse();
|
||||
bytes.into_iter().map(LookupReverse::swap_bits).collect()
|
||||
}
|
||||
|
||||
/// Emulates a SSZ `Bitvector`.
|
||||
///
|
||||
/// An ordered, heap-allocated, fixed-length, collection of `bool` values, with `N` values.
|
||||
///
|
||||
/// ## Notes
|
||||
///
|
||||
/// Considering this struct is backed by bytes, errors may be raised when attempting to decode
|
||||
/// bytes into a `BitVector<N>` where `N` is not a multiple of 8. It is advised to always set `N` to
|
||||
/// a multiple of 8.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```
|
||||
/// use ssz_types::{BitVector, typenum};
|
||||
///
|
||||
/// let mut bitvec: BitVector<typenum::U8> = BitVector::new();
|
||||
///
|
||||
/// assert_eq!(bitvec.len(), 8);
|
||||
///
|
||||
/// for i in 0..8 {
|
||||
/// assert_eq!(bitvec.get(i).unwrap(), false); // Defaults to false.
|
||||
/// }
|
||||
///
|
||||
/// assert!(bitvec.get(8).is_err()); // Cannot get out-of-bounds.
|
||||
///
|
||||
/// assert!(bitvec.set(7, true).is_ok());
|
||||
/// assert!(bitvec.set(8, true).is_err()); // Cannot set out-of-bounds.
|
||||
/// ```
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BitVector<N> {
|
||||
bitfield: Bitfield,
|
||||
_phantom: PhantomData<N>,
|
||||
}
|
||||
|
||||
common_impl!(BitVector, FixedSizedError);
|
||||
|
||||
impl<N: Unsigned> BitVector<N> {
|
||||
/// Create a new bitfield.
|
||||
pub fn new() -> Self {
|
||||
Self::with_capacity(Self::capacity()).expect("Capacity must be correct")
|
||||
}
|
||||
|
||||
fn capacity() -> usize {
|
||||
N::to_usize()
|
||||
}
|
||||
|
||||
fn validate_length(len: usize) -> Result<(), FixedSizedError> {
|
||||
let fixed_len = N::to_usize();
|
||||
|
||||
if len > fixed_len {
|
||||
Err(FixedSizedError::InvalidLength {
|
||||
i: len,
|
||||
len: fixed_len,
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Emulates a SSZ `Bitlist`.
|
||||
///
|
||||
/// An ordered, heap-allocated, variable-length, collection of `bool` values, limited to `N`
|
||||
/// values.
|
||||
///
|
||||
/// ## Notes
|
||||
///
|
||||
/// Considering this struct is backed by bytes, errors may be raised when attempting to decode
|
||||
/// bytes into a `BitList<N>` where `N` is not a multiple of 8. It is advised to always set `N` to
|
||||
/// a multiple of 8.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```
|
||||
/// use ssz_types::{BitList, typenum};
|
||||
///
|
||||
/// let mut bitlist: BitList<typenum::U8> = BitList::new();
|
||||
///
|
||||
/// assert_eq!(bitlist.len(), 0);
|
||||
///
|
||||
/// assert!(bitlist.get(0).is_err()); // Cannot get at or below the length.
|
||||
///
|
||||
/// for i in 0..8 {
|
||||
/// assert!(bitlist.set(i, true).is_ok());
|
||||
/// }
|
||||
///
|
||||
/// assert!(bitlist.set(8, true).is_err()); // Cannot set out-of-bounds.
|
||||
///
|
||||
/// // Cannot create with an excessive capacity.
|
||||
/// let result: Result<BitList<typenum::U8>, _> = BitList::with_capacity(9);
|
||||
/// assert!(result.is_err());
|
||||
/// ```
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BitList<N> {
|
||||
bitfield: Bitfield,
|
||||
_phantom: PhantomData<N>,
|
||||
}
|
||||
|
||||
common_impl!(BitList, FixedSizedError);
|
||||
|
||||
impl<N: Unsigned> BitList<N> {
|
||||
/// Create a new, empty BitList.
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
bitfield: Bitfield::default(),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_length(len: usize) -> Result<(), FixedSizedError> {
|
||||
let max_len = Self::max_len();
|
||||
|
||||
if len > max_len {
|
||||
Err(FixedSizedError::InvalidLength {
|
||||
i: len,
|
||||
len: max_len,
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The maximum possible number of bits.
|
||||
pub fn max_len() -> usize {
|
||||
N::to_usize()
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Unsigned + Clone> BitList<N> {
|
||||
/// Compute the intersection (binary-and) of this bitfield with another
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn intersection(&self, other: &Self) -> Self {
|
||||
assert_eq!(self.len(), other.len());
|
||||
let mut res: Self = self.to_owned();
|
||||
res.intersection_inplace(other);
|
||||
res
|
||||
}
|
||||
|
||||
/// Like `intersection` but in-place (updates `self`).
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn intersection_inplace(&mut self, other: &Self) {
|
||||
self.bitfield.intersect(&other.bitfield);
|
||||
}
|
||||
|
||||
/// Compute the union (binary-or) of this bitfield with another. Lengths must match.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn union(&self, other: &Self) -> Self {
|
||||
assert_eq!(self.len(), other.len());
|
||||
let mut res = self.clone();
|
||||
res.union_inplace(other);
|
||||
res
|
||||
}
|
||||
|
||||
/// Like `union` but in-place (updates `self`).
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn union_inplace(&mut self, other: &Self) {
|
||||
self.bitfield.union(&other.bitfield);
|
||||
}
|
||||
|
||||
/// Compute the difference (binary-minus) of this bitfield with another. Lengths must match.
|
||||
///
|
||||
/// Computes `self - other`.
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn difference(&self, other: &Self) -> Self {
|
||||
assert_eq!(self.len(), other.len());
|
||||
let mut res = self.clone();
|
||||
res.difference_inplace(other);
|
||||
res
|
||||
}
|
||||
|
||||
/// Like `difference` but in-place (updates `self`).
|
||||
///
|
||||
/// ## Panics
|
||||
///
|
||||
/// If `self` and `other` have different lengths.
|
||||
pub fn difference_inplace(&mut self, other: &Self) {
|
||||
self.bitfield.difference(&other.bitfield);
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Unsigned> default::Default for BitList<N> {
|
||||
/// Default provides the "empty" bitfield
|
||||
/// Note: the empty bitfield is set to the `0` byte.
|
||||
fn default() -> Self {
|
||||
Self::from_elem(0, false).expect("Zero cannot be larger than the maximum length")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -568,3 +744,4 @@ mod tests {
|
||||
assert!(a.difference(&a).is_zero());
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -60,8 +60,8 @@ impl<T, N: Unsigned> FixedVector<T, N> {
|
||||
})
|
||||
} else {
|
||||
Err(Error::InvalidLength {
|
||||
len: vec.len(),
|
||||
fixed_len: Self::capacity(),
|
||||
i: vec.len(),
|
||||
len: Self::capacity(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,8 @@
|
||||
mod bit_vector;
|
||||
mod bitfield;
|
||||
mod fixed_vector;
|
||||
mod variable_list;
|
||||
|
||||
pub use bit_vector::{BitList, BitVector};
|
||||
pub use bitfield::{BitList, BitVector};
|
||||
pub use fixed_vector::FixedVector;
|
||||
pub use typenum;
|
||||
pub use variable_list::VariableList;
|
||||
@ -12,12 +11,26 @@ pub use variable_list::VariableList;
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum VariableSizedError {
|
||||
/// The operation would cause the maximum length to be exceeded.
|
||||
ExceedsMaxLength { len: usize, max_len: usize },
|
||||
ExceedsMaxLength {
|
||||
len: usize,
|
||||
max_len: usize,
|
||||
},
|
||||
OutOfBounds {
|
||||
i: usize,
|
||||
len: usize,
|
||||
},
|
||||
}
|
||||
|
||||
/// Returned when a fixed-length item encounters an error.
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum FixedSizedError {
|
||||
/// The operation would create an item of an invalid size.
|
||||
InvalidLength { len: usize, fixed_len: usize },
|
||||
InvalidLength {
|
||||
i: usize,
|
||||
len: usize,
|
||||
},
|
||||
OutOfBounds {
|
||||
i: usize,
|
||||
len: usize,
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user