Progress further on Bitfield struct

This commit is contained in:
Paul Hauner 2019-07-08 16:27:08 +10:00
parent bbcc58dca3
commit 93cd38da55
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
2 changed files with 5 additions and 36 deletions

View File

@ -46,25 +46,15 @@ impl_bitfield_fns!(BitVector);
impl<N: Unsigned> BitVector<N> { impl<N: Unsigned> BitVector<N> {
/// Create a new bitfield. /// Create a new bitfield.
pub fn new() -> Self { pub fn new() -> Self {
Self::with_capacity(Self::capacity()).expect("Capacity must be correct") Self {
bitfield: Bitfield::with_capacity(Self::capacity()),
_phantom: PhantomData,
}
} }
fn capacity() -> usize { fn capacity() -> usize {
N::to_usize() N::to_usize()
} }
fn validate_length(len: usize) -> Result<(), Error> {
let fixed_len = N::to_usize();
if len > fixed_len {
Err(Error::InvalidLength {
i: len,
len: fixed_len,
})
} else {
Ok(())
}
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -4,7 +4,7 @@
/// ///
/// The internal representation of the bitfield is the same as that required by SSZ - the highest /// The internal representation of the bitfield is the same as that required by SSZ - the highest
/// byte (by `Vec` index) stores the lowest bit-indices and the right-most bit stores the lowest /// byte (by `Vec` index) stores the lowest bit-indices and the right-most bit stores the lowest
/// bit-index. E.g., `vec![0b0000_0010, 0b0000_0001]` has bits `1, 9` set. /// bit-index. E.g., `vec![0b0000_0010, 0b0000_0001]` has bits `0, 9` set.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Bitfield { pub struct Bitfield {
bytes: Vec<u8>, bytes: Vec<u8>,
@ -69,21 +69,6 @@ impl Bitfield {
(bit_len + 7) / 8 (bit_len + 7) / 8
} }
/// Verify that the given `bytes` faithfully represent a bitfield of length `bit_len`.
///
/// The only valid `bytes` for `bit_len == 0` is `&[0]`.
fn verify_bitfield_bytes(bytes: &[u8], bit_len: usize) -> bool {
if bytes.len() == 1 && bit_len == 0 && bytes == &[0] {
true // A bitfield with `bit_len` 0 can only be represented by a single zero byte.
} else if bytes.len() != Bitfield::bytes_for_bit_len(bit_len) || bytes.is_empty() {
false // The number of bytes must be the minimum required to represent `bit_len`.
} else {
// Ensure there are no bits higher than `bit_len` that are set to true.
let (mask, _) = u8::max_value().overflowing_shr(bit_len as u32 % 8);
(bytes.last().expect("Bytes cannot be empty") & !mask) == 0
}
}
pub fn to_bytes(self) -> Vec<u8> { pub fn to_bytes(self) -> Vec<u8> {
self.bytes self.bytes
} }
@ -213,12 +198,6 @@ impl<'a> Iterator for BitIter<'a> {
macro_rules! impl_bitfield_fns { macro_rules! impl_bitfield_fns {
($name: ident) => { ($name: ident) => {
impl<N: Unsigned> $name<N> { impl<N: Unsigned> $name<N> {
pub fn with_capacity(initial_len: usize) -> Result<Self, Error> {
Self::validate_length(initial_len)?;
Self::with_capacity(initial_len)
}
pub fn get(&self, i: usize) -> Result<bool, Error> { pub fn get(&self, i: usize) -> Result<bool, Error> {
if i < N::to_usize() { if i < N::to_usize() {
match self.bitfield.get(i) { match self.bitfield.get(i) {