Refactor to use Bitfield struct with type variants

This commit is contained in:
Paul Hauner 2019-07-08 18:41:43 +10:00
parent 93cd38da55
commit 1484773cd1
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
4 changed files with 402 additions and 983 deletions

View File

@ -1,448 +0,0 @@
use super::*;
use crate::{bitfield::Bitfield, impl_bitfield_fns, Error};
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;
/// 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>,
}
impl_bitfield_fns!(BitList);
impl<N: Unsigned> BitList<N> {
/// Create a new, empty BitList.
pub fn new() -> Self {
Self {
bitfield: Bitfield::with_capacity(Self::max_len()),
_phantom: PhantomData,
}
}
fn validate_length(len: usize) -> Result<(), Error> {
let max_len = Self::max_len();
if len > max_len {
Err(Error::InvalidLength {
i: len,
len: max_len,
})
} else {
Ok(())
}
}
/// The maximum possible number of bits.
pub fn max_len() -> usize {
N::to_usize()
}
}
/*
fn encode_bitfield(bitfield: Bitfield) -> Vec<u8> {
// Set the next bit of the bitfield to true.
//
// SSZ spec:
//
// An additional leading 1 bit is added so that the length in bits will also be known.
bitfield.set(bitfield.len(), true);
let bytes = bitfield.to_bytes();
}
*/
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.intersection(&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);
}
}
/*
#[cfg(test)]
mod test {
use super::*;
use serde_yaml;
use ssz::ssz_encode;
// use tree_hash::TreeHash;
pub type BitList1024 = BitList<typenum::U1024>;
/*
#[test]
pub fn cached_tree_hash() {
let original = BitList1024::from_bytes(&vec![18; 12][..]);
let mut cache = cached_tree_hash::TreeHashCache::new(&original).unwrap();
assert_eq!(
cache.tree_hash_root().unwrap().to_vec(),
original.tree_hash_root()
);
let modified = BitList1024::from_bytes(&vec![2; 1][..]);
cache.update(&modified).unwrap();
assert_eq!(
cache.tree_hash_root().unwrap().to_vec(),
modified.tree_hash_root()
);
}
*/
#[test]
fn new_bitfield() {
let mut field = BitList1024::new();
let original_len = field.len();
for i in 0..100 {
if i < original_len {
assert!(!field.get(i).unwrap());
} else {
assert!(field.get(i).is_err());
}
field.set(i, true).unwrap();
}
}
#[test]
fn empty_bitfield() {
let mut field = BitList1024::from_elem(0, false).unwrap();
let original_len = field.len();
assert_eq!(original_len, 0);
for i in 0..100 {
if i < original_len {
assert!(!field.get(i).unwrap());
} else {
assert!(field.get(i).is_err());
}
field.set(i, true).unwrap();
}
assert_eq!(field.len(), 100);
assert_eq!(field.num_set_bits(), 100);
}
const INPUT: &[u8] = &[0b0100_0000, 0b0100_0000];
#[test]
fn get_from_bitfield() {
let field = BitList1024::from_bytes(INPUT).unwrap();
field.get(0).unwrap();
field.get(6).unwrap();
field.get(14).unwrap();
}
#[test]
fn set_for_bitfield() {
let mut field = BitList1024::from_bytes(INPUT).unwrap();
field.set(10, true).unwrap();
field.get(10).unwrap();
field.set(6, false).unwrap();
field.get(6).unwrap();
}
#[test]
fn len() {
let field = BitList1024::from_bytes(INPUT).unwrap();
assert_eq!(field.len(), 16);
let field = BitList1024::new();
assert_eq!(field.len(), 0);
}
#[test]
fn num_set_bits() {
let field = BitList1024::from_bytes(INPUT).unwrap();
assert_eq!(field.num_set_bits(), 2);
let field = BitList1024::new();
assert_eq!(field.num_set_bits(), 0);
}
#[test]
fn to_bytes() {
let field = BitList1024::from_bytes(INPUT).unwrap();
assert_eq!(field.to_bytes(), INPUT);
let field = BitList1024::new();
assert_eq!(field.to_bytes(), vec![0]);
}
#[test]
fn out_of_bounds() {
let mut field = BitList1024::from_bytes(INPUT).unwrap();
let out_of_bounds_index = field.len();
assert!(field.set(out_of_bounds_index, true).is_ok());
assert!(field.len() == out_of_bounds_index + 1);
assert!(field.get(out_of_bounds_index).unwrap());
for i in 0..100 {
if i <= out_of_bounds_index {
assert!(field.set(i, true).is_ok());
} else {
assert!(field.set(i, true).is_ok());
}
}
}
#[test]
fn grows_with_false() {
let input_all_set: &[u8] = &[0b1111_1111, 0b1111_1111];
let mut field = BitList1024::from_bytes(input_all_set).unwrap();
// Define `a` and `b`, where both are out of bounds and `b` is greater than `a`.
let a = field.len();
let b = a + 1;
// Ensure `a` is out-of-bounds for test integrity.
assert!(field.get(a).is_err());
// Set `b` to `true`..
assert!(field.set(b, true).is_ok());
// Ensure that `a` wasn't also set to `true` during the grow.
assert_eq!(field.get(a), Ok(false));
assert_eq!(field.get(b), Ok(true));
}
#[test]
fn num_bytes() {
let field = BitList1024::from_bytes(INPUT).unwrap();
assert_eq!(field.num_bytes(), 2);
let field = BitList1024::from_elem(2, true).unwrap();
assert_eq!(field.num_bytes(), 1);
let field = BitList1024::from_elem(13, true).unwrap();
assert_eq!(field.num_bytes(), 2);
}
#[test]
fn ssz_encoding() {
let field = create_bitfield();
assert_eq!(field.as_ssz_bytes(), vec![0b0000_0011, 0b1000_0111]);
let field = BitList1024::from_elem(18, true).unwrap();
assert_eq!(
field.as_ssz_bytes(),
vec![0b0000_0011, 0b1111_1111, 0b1111_1111]
);
let mut b = BitList1024::new();
b.set(1, true).unwrap();
assert_eq!(ssz_encode(&b), vec![0b0000_0010]);
}
fn create_bitfield() -> BitList1024 {
let count = 2 * 8;
let mut field = BitList1024::with_capacity(count).unwrap();
let indices = &[0, 1, 2, 7, 8, 9];
for &i in indices {
field.set(i, true).unwrap();
}
field
}
#[test]
fn ssz_decode() {
let encoded = vec![0b0000_0011, 0b1000_0111];
let field = BitList1024::from_ssz_bytes(&encoded).unwrap();
let expected = create_bitfield();
assert_eq!(field, expected);
let encoded = vec![255, 255, 3];
let field = BitList1024::from_ssz_bytes(&encoded).unwrap();
let expected = BitList1024::from_bytes(&[255, 255, 3]).unwrap();
assert_eq!(field, expected);
}
#[test]
fn serialize_deserialize() {
use serde_yaml::Value;
let data: &[(_, &[_])] = &[
("0x01", &[0b00000001]),
("0xf301", &[0b11110011, 0b00000001]),
];
for (hex_data, bytes) in data {
let bitfield = BitList1024::from_bytes(bytes).unwrap();
assert_eq!(
serde_yaml::from_str::<BitList1024>(hex_data).unwrap(),
bitfield
);
assert_eq!(
serde_yaml::to_value(&bitfield).unwrap(),
Value::String(hex_data.to_string())
);
}
}
#[test]
fn ssz_round_trip() {
let original = BitList1024::from_bytes(&vec![18; 12][..]).unwrap();
let ssz = ssz_encode(&original);
let decoded = BitList1024::from_ssz_bytes(&ssz).unwrap();
assert_eq!(original, decoded);
}
#[test]
fn bitor() {
let a = BitList1024::from_bytes(&vec![2, 8, 1][..]).unwrap();
let b = BitList1024::from_bytes(&vec![4, 8, 16][..]).unwrap();
let c = BitList1024::from_bytes(&vec![6, 8, 17][..]).unwrap();
assert_eq!(c, a | b);
}
#[test]
fn is_zero() {
let yes_data: &[&[u8]] = &[&[], &[0], &[0, 0], &[0, 0, 0]];
for bytes in yes_data {
assert!(BitList1024::from_bytes(bytes).unwrap().is_zero());
}
let no_data: &[&[u8]] = &[&[1], &[6], &[0, 1], &[0, 0, 1], &[0, 0, 255]];
for bytes in no_data {
assert!(!BitList1024::from_bytes(bytes).unwrap().is_zero());
}
}
#[test]
fn intersection() {
let a = BitList1024::from_bytes(&[0b1100, 0b0001]).unwrap();
let b = BitList1024::from_bytes(&[0b1011, 0b1001]).unwrap();
let c = BitList1024::from_bytes(&[0b1000, 0b0001]).unwrap();
assert_eq!(a.intersection(&b), c);
assert_eq!(b.intersection(&a), c);
assert_eq!(a.intersection(&c), c);
assert_eq!(b.intersection(&c), c);
assert_eq!(a.intersection(&a), a);
assert_eq!(b.intersection(&b), b);
assert_eq!(c.intersection(&c), c);
}
#[test]
fn union() {
let a = BitList1024::from_bytes(&[0b1100, 0b0001]).unwrap();
let b = BitList1024::from_bytes(&[0b1011, 0b1001]).unwrap();
let c = BitList1024::from_bytes(&[0b1111, 0b1001]).unwrap();
assert_eq!(a.union(&b), c);
assert_eq!(b.union(&a), c);
assert_eq!(a.union(&a), a);
assert_eq!(b.union(&b), b);
assert_eq!(c.union(&c), c);
}
#[test]
fn difference() {
let a = BitList1024::from_bytes(&[0b1100, 0b0001]).unwrap();
let b = BitList1024::from_bytes(&[0b1011, 0b1001]).unwrap();
let a_b = BitList1024::from_bytes(&[0b0100, 0b0000]).unwrap();
let b_a = BitList1024::from_bytes(&[0b0011, 0b1000]).unwrap();
assert_eq!(a.difference(&b), a_b);
assert_eq!(b.difference(&a), b_a);
assert!(a.difference(&a).is_zero());
}
}
*/

View File

@ -1,321 +0,0 @@
use super::*;
use crate::{bitfield::Bitfield, impl_bitfield_fns, Error};
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_hex::{encode, PrefixedHexVisitor};
use ssz::{Decode, Encode};
use std::cmp;
use std::marker::PhantomData;
use typenum::Unsigned;
/// 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>,
}
impl_bitfield_fns!(BitVector);
impl<N: Unsigned> BitVector<N> {
/// Create a new bitfield.
pub fn new() -> Self {
Self {
bitfield: Bitfield::with_capacity(Self::capacity()),
_phantom: PhantomData,
}
}
fn capacity() -> usize {
N::to_usize()
}
}
#[cfg(test)]
mod test {
use super::*;
use serde_yaml;
use ssz::ssz_encode;
// use tree_hash::TreeHash;
pub type BitVector4 = BitVector<typenum::U4>;
pub type BitVector1024 = BitVector<typenum::U1024>;
/*
#[test]
pub fn cached_tree_hash() {
let original = BitVector1024::from_bytes(&vec![18; 12][..]);
let mut cache = cached_tree_hash::TreeHashCache::new(&original).unwrap();
assert_eq!(
cache.tree_hash_root().unwrap().to_vec(),
original.tree_hash_root()
);
let modified = BitVector1024::from_bytes(&vec![2; 1][..]);
cache.update(&modified).unwrap();
assert_eq!(
cache.tree_hash_root().unwrap().to_vec(),
modified.tree_hash_root()
);
}
*/
/*
#[test]
fn new_bitfield() {
let mut field = BitVector1024::new();
let original_len = field.len();
assert_eq!(original_len, 1024);
for i in 0..1028 {
if i < original_len {
assert!(!field.get(i).unwrap());
assert!(field.set(i, true).is_ok());
} else {
assert!(field.get(i).is_err());
assert!(field.set(i, true).is_err());
}
}
}
#[test]
fn from_bytes_bitvec4() {
let bytes = &[3];
let bitvec = BitVector4::from_bytes(bytes).unwrap();
assert_eq!(bitvec.get(0), Ok(true));
assert_eq!(bitvec.get(1), Ok(true));
assert_eq!(bitvec.get(2), Ok(false));
assert_eq!(bitvec.get(3), Ok(false));
assert!(bitvec.get(4).is_err());
}
#[test]
fn from_bytes_bytes_too_long() {
let bytes = &[0, 0];
assert_eq!(
BitVector4::from_bytes(bytes),
Err(Error::InvalidLength { i: 16, len: 4 })
);
}
const INPUT: &[u8] = &[0b0100_0000, 0b0100_0000];
#[test]
fn get_from_bitfield() {
let field = BitVector1024::from_bytes(INPUT).unwrap();
field.get(0).unwrap();
field.get(6).unwrap();
field.get(14).unwrap();
}
#[test]
fn set_for_bitfield() {
let mut field = BitVector1024::from_bytes(INPUT).unwrap();
field.set(10, true).unwrap();
field.get(10).unwrap();
field.set(6, false).unwrap();
field.get(6).unwrap();
}
#[test]
fn len() {
let field = BitVector1024::from_bytes(INPUT).unwrap();
assert_eq!(field.len(), 16);
let field = BitVector1024::new();
assert_eq!(field.len(), 0);
}
#[test]
fn num_set_bits() {
let field = BitVector1024::from_bytes(INPUT).unwrap();
assert_eq!(field.num_set_bits(), 2);
let field = BitVector1024::new();
assert_eq!(field.num_set_bits(), 0);
}
#[test]
fn to_bytes() {
let field = BitVector1024::from_bytes(INPUT).unwrap();
assert_eq!(field.to_bytes(), INPUT);
let field = BitVector1024::new();
assert_eq!(field.to_bytes(), vec![0]);
}
#[test]
fn out_of_bounds() {
let mut field = BitVector1024::from_bytes(INPUT).unwrap();
let out_of_bounds_index = field.len();
assert!(field.set(out_of_bounds_index, true).is_ok());
assert!(field.len() == out_of_bounds_index + 1);
assert!(field.get(out_of_bounds_index).unwrap());
for i in 0..100 {
if i <= out_of_bounds_index {
assert!(field.set(i, true).is_ok());
} else {
assert!(field.set(i, true).is_ok());
}
}
}
#[test]
fn grows_with_false() {
let input_all_set: &[u8] = &[0b1111_1111, 0b1111_1111];
let mut field = BitVector1024::from_bytes(input_all_set).unwrap();
// Define `a` and `b`, where both are out of bounds and `b` is greater than `a`.
let a = field.len();
let b = a + 1;
// Ensure `a` is out-of-bounds for test integrity.
assert!(field.get(a).is_err());
// Set `b` to `true`..
assert!(field.set(b, true).is_ok());
// Ensure that `a` wasn't also set to `true` during the grow.
assert_eq!(field.get(a), Ok(false));
assert_eq!(field.get(b), Ok(true));
}
#[test]
fn num_bytes() {
let field = BitVector1024::from_bytes(INPUT).unwrap();
assert_eq!(field.num_bytes(), 2);
let field = BitVector1024::from_elem(2, true).unwrap();
assert_eq!(field.num_bytes(), 1);
let field = BitVector1024::from_elem(13, true).unwrap();
assert_eq!(field.num_bytes(), 2);
}
#[test]
fn ssz_encoding() {
let field = create_bitfield();
assert_eq!(field.as_ssz_bytes(), vec![0b0000_0011, 0b1000_0111]);
let field = BitVector1024::from_elem(18, true).unwrap();
assert_eq!(
field.as_ssz_bytes(),
vec![0b0000_0011, 0b1111_1111, 0b1111_1111]
);
let mut b = BitVector1024::new();
b.set(1, true).unwrap();
assert_eq!(ssz_encode(&b), vec![0b0000_0010]);
}
fn create_bitfield() -> BitVector1024 {
let count = 2 * 8;
let mut field = BitVector1024::with_capacity(count).unwrap();
let indices = &[0, 1, 2, 7, 8, 9];
for &i in indices {
field.set(i, true).unwrap();
}
field
}
#[test]
fn ssz_decode() {
let encoded = vec![0b0000_0011, 0b1000_0111];
let field = BitVector1024::from_ssz_bytes(&encoded).unwrap();
let expected = create_bitfield();
assert_eq!(field, expected);
let encoded = vec![255, 255, 3];
let field = BitVector1024::from_ssz_bytes(&encoded).unwrap();
let expected = BitVector1024::from_bytes(&[255, 255, 3]).unwrap();
assert_eq!(field, expected);
}
#[test]
fn serialize_deserialize() {
use serde_yaml::Value;
let data: &[(_, &[_])] = &[
("0x01", &[0b00000001]),
("0xf301", &[0b11110011, 0b00000001]),
];
for (hex_data, bytes) in data {
let bitfield = BitVector1024::from_bytes(bytes).unwrap();
assert_eq!(
serde_yaml::from_str::<BitVector1024>(hex_data).unwrap(),
bitfield
);
assert_eq!(
serde_yaml::to_value(&bitfield).unwrap(),
Value::String(hex_data.to_string())
);
}
}
#[test]
fn ssz_round_trip() {
let original = BitVector1024::from_bytes(&vec![18; 12][..]).unwrap();
let ssz = ssz_encode(&original);
let decoded = BitVector1024::from_ssz_bytes(&ssz).unwrap();
assert_eq!(original, decoded);
}
#[test]
fn bitor() {
let a = BitVector1024::from_bytes(&vec![2, 8, 1][..]).unwrap();
let b = BitVector1024::from_bytes(&vec![4, 8, 16][..]).unwrap();
let c = BitVector1024::from_bytes(&vec![6, 8, 17][..]).unwrap();
assert_eq!(c, a | b);
}
#[test]
fn is_zero() {
let yes_data: &[&[u8]] = &[&[], &[0], &[0, 0], &[0, 0, 0]];
for bytes in yes_data {
assert!(BitVector1024::from_bytes(bytes).unwrap().is_zero());
}
let no_data: &[&[u8]] = &[&[1], &[6], &[0, 1], &[0, 0, 1], &[0, 0, 255]];
for bytes in no_data {
assert!(!BitVector1024::from_bytes(bytes).unwrap().is_zero());
}
}
*/
}

View File

@ -1,24 +1,127 @@
/// A heap-allocated, ordered, fixed-length, collection of `bool` values.
use core::marker::PhantomData;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_hex::{encode as hex_encode, PrefixedHexVisitor};
use ssz::{Decode, Encode};
use typenum::Unsigned;
pub trait BitfieldBehaviour: Clone {}
/// A marker struct used to define SSZ `BitList` functionality on a `Bitfield`.
#[derive(Clone, PartialEq, Debug)]
pub struct BitList<N> {
_phantom: PhantomData<N>,
}
/// A marker struct used to define SSZ `BitVector` functionality on a `Bitfield`.
#[derive(Clone, PartialEq, Debug)]
pub struct BitVector<N> {
_phantom: PhantomData<N>,
}
impl<N: Unsigned + Clone> BitfieldBehaviour for BitList<N> {}
impl<N: Unsigned + Clone> BitfieldBehaviour for BitVector<N> {}
/// A heap-allocated, ordered, fixed-length, collection of `bool` values. Must be used with the `BitList` or
/// `BitVector` marker structs.
///
/// The length of the Bitfield is set at instantiation (i.e., runtime, not compile time).
/// The length of the Bitfield is set at instantiation (i.e., runtime, not compile time). However,
/// use with a `BitList` sets a type-level (i.e., compile-time) maximum length and `BitVector`
/// provides a type-level fixed length.
///
/// ## Note
///
/// 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
/// bit-index. E.g., `vec![0b0000_0010, 0b0000_0001]` has bits `0, 9` set.
#[derive(Clone, Debug, PartialEq)]
pub struct Bitfield {
pub struct Bitfield<T> {
bytes: Vec<u8>,
len: usize,
_phantom: PhantomData<T>,
}
impl Bitfield {
pub fn with_capacity(num_bits: usize) -> Self {
Self {
bytes: vec![0; Self::bytes_for_bit_len(num_bits)],
len: num_bits,
impl<N: Unsigned + Clone> Bitfield<BitList<N>> {
pub fn with_capacity(num_bits: usize) -> Option<Self> {
if num_bits <= N::to_usize() {
Some(Self {
bytes: vec![0; bytes_for_bit_len(num_bits)],
len: num_bits,
_phantom: PhantomData,
})
} else {
None
}
}
pub fn capacity() -> usize {
N::to_usize()
}
pub fn to_bytes(&self) -> Vec<u8> {
let len = self.len();
let mut bytes = self.as_slice().to_vec();
if bytes_for_bit_len(len + 1) == bytes.len() + 1 {
bytes.insert(0, 0);
}
let mut bitfield: Bitfield<BitList<N>> = Bitfield::from_raw_bytes(bytes, len + 1)
.expect("Bitfield capacity has been confirmed earlier.");
bitfield
.set(len, true)
.expect("Bitfield capacity has been confirmed earlier.");
bitfield.bytes
}
pub fn from_bytes(bytes: Vec<u8>) -> Option<Self> {
let mut initial_bitfield: Bitfield<BitList<N>> = {
let num_bits = bytes.len() * 8;
Bitfield::from_raw_bytes(bytes, num_bits)
.expect("Must have adequate bytes for bit count.")
};
let len = initial_bitfield.highest_set_bit()?;
initial_bitfield
.set(len, false)
.expect("Bit has been confirmed to exist");
let mut bytes = initial_bitfield.to_raw_bytes();
if bytes_for_bit_len(len) < bytes.len() {
bytes.remove(0);
}
Self::from_raw_bytes(bytes, len)
}
}
impl<N: Unsigned + Clone> Bitfield<BitVector<N>> {
pub fn new() -> Self {
let num_bits = N::to_usize();
Self {
bytes: vec![0; num_bits],
len: num_bits,
_phantom: PhantomData,
}
}
pub fn capacity() -> usize {
N::to_usize()
}
pub fn to_bytes(self) -> Vec<u8> {
self.to_raw_bytes()
}
pub fn from_bytes(bytes: Vec<u8>) -> Option<Self> {
Self::from_raw_bytes(bytes, Self::capacity())
}
}
impl<T: BitfieldBehaviour> Bitfield<T> {
pub fn set(&mut self, i: usize, value: bool) -> Option<()> {
if i < self.len {
let byte = {
@ -65,11 +168,7 @@ impl Bitfield {
self.len == 0
}
fn bytes_for_bit_len(bit_len: usize) -> usize {
(bit_len + 7) / 8
}
pub fn to_bytes(self) -> Vec<u8> {
pub fn to_raw_bytes(self) -> Vec<u8> {
self.bytes
}
@ -77,11 +176,15 @@ impl Bitfield {
&self.bytes
}
pub fn from_bytes(bytes: Vec<u8>, bit_len: usize) -> Option<Self> {
pub fn from_raw_bytes(bytes: Vec<u8>, bit_len: usize) -> Option<Self> {
if bytes.len() == 1 && bit_len == 0 && bytes == &[0] {
// A bitfield with `bit_len` 0 can only be represented by a single zero byte.
Some(Self { bytes, len: 0 })
} else if bytes.len() != Bitfield::bytes_for_bit_len(bit_len) || bytes.is_empty() {
Some(Self {
bytes,
len: 0,
_phantom: PhantomData,
})
} else if bytes.len() != bytes_for_bit_len(bit_len) || bytes.is_empty() {
// The number of bytes must be the minimum required to represent `bit_len`.
None
} else {
@ -92,6 +195,7 @@ impl Bitfield {
Some(Self {
bytes,
len: bit_len,
_phantom: PhantomData,
})
} else {
None
@ -99,7 +203,14 @@ impl Bitfield {
}
}
pub fn iter(&self) -> BitIter<'_> {
pub fn highest_set_bit(&self) -> Option<usize> {
let byte_i = self.bytes.iter().position(|byte| *byte > 0)?;
let bit_i = 7 - self.bytes[byte_i].leading_zeros() as usize;
Some((self.bytes.len().saturating_sub(1) - byte_i) * 8 + bit_i)
}
pub fn iter(&self) -> BitIter<'_, T> {
BitIter {
bitfield: self,
i: 0,
@ -178,12 +289,16 @@ impl Bitfield {
}
}
pub struct BitIter<'a> {
bitfield: &'a Bitfield,
fn bytes_for_bit_len(bit_len: usize) -> usize {
(bit_len + 7) / 8
}
pub struct BitIter<'a, T> {
bitfield: &'a Bitfield<T>,
i: usize,
}
impl<'a> Iterator for BitIter<'a> {
impl<'a, T: BitfieldBehaviour> Iterator for BitIter<'a, T> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
@ -193,188 +308,200 @@ impl<'a> Iterator for BitIter<'a> {
}
}
/// Provides a common `impl` for structs that wrap a `$name`.
#[macro_export]
macro_rules! impl_bitfield_fns {
($name: ident) => {
impl<N: Unsigned> $name<N> {
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,
len: self.bitfield.len(),
}),
}
} else {
Err(Error::InvalidLength {
i,
len: N::to_usize(),
})
}
}
impl<N: Unsigned + Clone> Encode for Bitfield<BitList<N>> {
fn is_ssz_fixed_len() -> bool {
false
}
pub fn set(&mut self, i: usize, value: bool) -> Option<()> {
self.bitfield.set(i, value)
}
/// 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 presently used to store the bitfield.
pub fn num_bytes(&self) -> usize {
self.bitfield.as_slice().len()
}
/// Returns the number of `1` bits in the bitfield
pub fn num_set_bits(&self) -> usize {
self.bitfield.iter().filter(|&bit| bit).count()
}
}
/*
impl<N: Unsigned> Encode for $name<N> {
fn is_ssz_fixed_len() -> bool {
false
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.append(&mut self.bitfield.to_bytes())
}
}
impl<N: Unsigned> Decode for $name<N> {
fn is_ssz_fixed_len() -> bool {
false
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
let bitfield =
Bitfield::from_bytes(bytes.to_vec(), bytes.len() * 8).expect("Cannot fail");
Ok(Self {
bitfield,
_phantom: PhantomData,
})
/*
$name::from_bytes(bytes)
.map_err(|e| ssz::DecodeError::BytesInvalid(format!("Bitfield {:?}", e)))
*/
}
}
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
S: Serializer,
{
serializer.serialize_str(&encode(self.bitfield.to_bytes()))
}
}
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
D: Deserializer<'de>,
{
// We reverse the bit-order so that the BitVec library can read its 0th
// bit from the end of the hex string, e.g.
// "0xef01" => [0xef, 0x01] => [0b1000_0000, 0b1111_1110]
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
$name::from_bytes(&bytes)
.map_err(|e| serde::de::Error::custom(format!("Bitfield {:?}", e)))
}
}
impl<N: Unsigned> tree_hash::TreeHash for $name<N> {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
unreachable!("List should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
unreachable!("List should never be packed.")
}
fn tree_hash_root(&self) -> Vec<u8> {
self.to_bytes().tree_hash_root()
}
}
*/
};
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.append(&mut self.clone().to_bytes())
}
}
impl<N: Unsigned + Clone> Decode for Bitfield<BitList<N>> {
fn is_ssz_fixed_len() -> bool {
false
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
Self::from_bytes(bytes.to_vec())
.ok_or_else(|| ssz::DecodeError::BytesInvalid("BitList failed to decode".to_string()))
}
}
impl<N: Unsigned + Clone> Encode for Bitfield<BitVector<N>> {
fn is_ssz_fixed_len() -> bool {
true
}
fn ssz_fixed_len() -> usize {
bytes_for_bit_len(N::to_usize())
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.append(&mut self.clone().to_bytes())
}
}
impl<N: Unsigned + Clone> Decode for Bitfield<BitVector<N>> {
fn is_ssz_fixed_len() -> bool {
false
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
Self::from_bytes(bytes.to_vec())
.ok_or_else(|| ssz::DecodeError::BytesInvalid("BitVector failed to decode".to_string()))
}
}
impl<N: Unsigned + Clone> Serialize for Bitfield<BitList<N>> {
/// Serde serialization is compliant with the Ethereum YAML test format.
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&hex_encode(self.as_ssz_bytes()))
}
}
impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield<BitList<N>> {
/// Serde serialization is compliant with the Ethereum YAML test format.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
// We reverse the bit-order so that the BitVec library can read its 0th
// bit from the end of the hex string, e.g.
// "0xef01" => [0xef, 0x01] => [0b1000_0000, 0b1111_1110]
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
Self::from_ssz_bytes(&bytes)
.map_err(|e| serde::de::Error::custom(format!("Bitfield {:?}", e)))
}
}
impl<N: Unsigned + Clone> Serialize for Bitfield<BitVector<N>> {
/// Serde serialization is compliant with the Ethereum YAML test format.
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&hex_encode(self.as_ssz_bytes()))
}
}
impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield<BitVector<N>> {
/// Serde serialization is compliant with the Ethereum YAML test format.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
// We reverse the bit-order so that the BitVec library can read its 0th
// bit from the end of the hex string, e.g.
// "0xef01" => [0xef, 0x01] => [0b1000_0000, 0b1111_1110]
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
Self::from_ssz_bytes(&bytes)
.map_err(|e| serde::de::Error::custom(format!("Bitfield {:?}", e)))
}
}
impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<BitList<N>> {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
unreachable!("List should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
unreachable!("List should never be packed.")
}
fn tree_hash_root(&self) -> Vec<u8> {
// TODO: pad this out to max length.
self.as_ssz_bytes().tree_hash_root()
}
}
impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<BitVector<N>> {
fn tree_hash_type() -> tree_hash::TreeHashType {
// TODO: move this to be a vector.
tree_hash::TreeHashType::List
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
// TODO: move this to be a vector.
unreachable!("Vector should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
// TODO: move this to be a vector.
unreachable!("Vector should never be packed.")
}
fn tree_hash_root(&self) -> Vec<u8> {
self.as_ssz_bytes().tree_hash_root()
}
}
// TODO: test ssz decode a zero-length bitlist.
#[cfg(test)]
mod test {
use super::*;
#[test]
fn from_bytes() {
assert!(Bitfield::from_bytes(vec![0b0000_0000], 0).is_some());
assert!(Bitfield::from_bytes(vec![0b0000_0001], 1).is_some());
assert!(Bitfield::from_bytes(vec![0b0000_0011], 2).is_some());
assert!(Bitfield::from_bytes(vec![0b0000_0111], 3).is_some());
assert!(Bitfield::from_bytes(vec![0b0000_1111], 4).is_some());
assert!(Bitfield::from_bytes(vec![0b0001_1111], 5).is_some());
assert!(Bitfield::from_bytes(vec![0b0011_1111], 6).is_some());
assert!(Bitfield::from_bytes(vec![0b0111_1111], 7).is_some());
assert!(Bitfield::from_bytes(vec![0b1111_1111], 8).is_some());
type Bitfield = super::Bitfield<BitList<typenum::U1024>>;
assert!(Bitfield::from_bytes(vec![0b0000_0001, 0b1111_1111], 9).is_some());
assert!(Bitfield::from_bytes(vec![0b0000_0011, 0b1111_1111], 10).is_some());
assert!(Bitfield::from_bytes(vec![0b0000_0111, 0b1111_1111], 11).is_some());
assert!(Bitfield::from_bytes(vec![0b0000_1111, 0b1111_1111], 12).is_some());
assert!(Bitfield::from_bytes(vec![0b0001_1111, 0b1111_1111], 13).is_some());
assert!(Bitfield::from_bytes(vec![0b0011_1111, 0b1111_1111], 14).is_some());
assert!(Bitfield::from_bytes(vec![0b0111_1111, 0b1111_1111], 15).is_some());
assert!(Bitfield::from_bytes(vec![0b1111_1111, 0b1111_1111], 16).is_some());
#[test]
fn from_raw_bytes() {
assert!(Bitfield::from_raw_bytes(vec![0b0000_0000], 0).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0001], 1).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0011], 2).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0111], 3).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_1111], 4).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0001_1111], 5).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0011_1111], 6).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0111_1111], 7).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b1111_1111], 8).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0001, 0b1111_1111], 9).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0011, 0b1111_1111], 10).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0111, 0b1111_1111], 11).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0000_1111, 0b1111_1111], 12).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0001_1111, 0b1111_1111], 13).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0011_1111, 0b1111_1111], 14).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b0111_1111, 0b1111_1111], 15).is_some());
assert!(Bitfield::from_raw_bytes(vec![0b1111_1111, 0b1111_1111], 16).is_some());
for i in 0..8 {
assert!(Bitfield::from_bytes(vec![], i).is_none());
assert!(Bitfield::from_bytes(vec![0b1111_1111], i).is_none());
assert!(Bitfield::from_bytes(vec![0b1111_1110, 0b0000_0000], i).is_none());
assert!(Bitfield::from_raw_bytes(vec![], i).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b1111_1111], i).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b1111_1110, 0b0000_0000], i).is_none());
}
assert!(Bitfield::from_bytes(vec![0b0000_0001], 0).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0001], 0).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_0001], 0).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_0011], 1).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_0111], 2).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_1111], 3).is_none());
assert!(Bitfield::from_bytes(vec![0b0001_1111], 4).is_none());
assert!(Bitfield::from_bytes(vec![0b0011_1111], 5).is_none());
assert!(Bitfield::from_bytes(vec![0b0111_1111], 6).is_none());
assert!(Bitfield::from_bytes(vec![0b1111_1111], 7).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0001], 0).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0011], 1).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0111], 2).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_1111], 3).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0001_1111], 4).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0011_1111], 5).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0111_1111], 6).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b1111_1111], 7).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_0001, 0b1111_1111], 8).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_0011, 0b1111_1111], 9).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_0111, 0b1111_1111], 10).is_none());
assert!(Bitfield::from_bytes(vec![0b0000_1111, 0b1111_1111], 11).is_none());
assert!(Bitfield::from_bytes(vec![0b0001_1111, 0b1111_1111], 12).is_none());
assert!(Bitfield::from_bytes(vec![0b0011_1111, 0b1111_1111], 13).is_none());
assert!(Bitfield::from_bytes(vec![0b0111_1111, 0b1111_1111], 14).is_none());
assert!(Bitfield::from_bytes(vec![0b1111_1111, 0b1111_1111], 15).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0001, 0b1111_1111], 8).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0011, 0b1111_1111], 9).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_0111, 0b1111_1111], 10).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0000_1111, 0b1111_1111], 11).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0001_1111, 0b1111_1111], 12).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0011_1111, 0b1111_1111], 13).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b0111_1111, 0b1111_1111], 14).is_none());
assert!(Bitfield::from_raw_bytes(vec![0b1111_1111, 0b1111_1111], 15).is_none());
}
fn test_set_unset(num_bits: usize) {
let mut bitfield = Bitfield::with_capacity(num_bits);
let mut bitfield = Bitfield::with_capacity(num_bits).unwrap();
for i in 0..num_bits + 1 {
dbg!(i);
@ -399,12 +526,12 @@ mod test {
dbg!(num_bits);
for i in 0..num_bits {
dbg!(i);
let mut bitfield = Bitfield::with_capacity(num_bits);
let mut bitfield = Bitfield::with_capacity(num_bits).unwrap();
bitfield.set(i, true).unwrap();
let bytes = bitfield.clone().to_bytes();
let bytes = bitfield.clone().to_raw_bytes();
dbg!(&bytes);
assert_eq!(bitfield, Bitfield::from_bytes(bytes, num_bits).unwrap());
assert_eq!(bitfield, Bitfield::from_raw_bytes(bytes, num_bits).unwrap());
}
}
@ -423,33 +550,93 @@ mod test {
}
#[test]
fn to_bytes() {
let mut bitfield = Bitfield::with_capacity(9);
fn to_raw_bytes() {
let mut bitfield = Bitfield::with_capacity(9).unwrap();
bitfield.set(0, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b0000_0001]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b0000_0001]
);
bitfield.set(1, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b0000_0011]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b0000_0011]
);
bitfield.set(2, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b0000_0111]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b0000_0111]
);
bitfield.set(3, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b0000_1111]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b0000_1111]
);
bitfield.set(4, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b0001_1111]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b0001_1111]
);
bitfield.set(5, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b0011_1111]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b0011_1111]
);
bitfield.set(6, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b0111_1111]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b0111_1111]
);
bitfield.set(7, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0000, 0b1111_1111]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0000, 0b1111_1111]
);
bitfield.set(8, true);
assert_eq!(bitfield.clone().to_bytes(), vec![0b0000_0001, 0b1111_1111]);
assert_eq!(
bitfield.clone().to_raw_bytes(),
vec![0b0000_0001, 0b1111_1111]
);
}
#[test]
fn highest_set_bit() {
assert_eq!(Bitfield::with_capacity(16).unwrap().highest_set_bit(), None);
assert_eq!(
Bitfield::from_raw_bytes(vec![0b0000_000, 0b0000_0001], 16)
.unwrap()
.highest_set_bit(),
Some(0)
);
assert_eq!(
Bitfield::from_raw_bytes(vec![0b0000_000, 0b0000_0010], 16)
.unwrap()
.highest_set_bit(),
Some(1)
);
assert_eq!(
Bitfield::from_raw_bytes(vec![0b0000_1000], 8)
.unwrap()
.highest_set_bit(),
Some(3)
);
assert_eq!(
Bitfield::from_raw_bytes(vec![0b1000_0000, 0b0000_0000], 16)
.unwrap()
.highest_set_bit(),
Some(15)
);
}
#[test]
fn intersection() {
let a = Bitfield::from_bytes(vec![0b1100, 0b0001], 16).unwrap();
let b = Bitfield::from_bytes(vec![0b1011, 0b1001], 16).unwrap();
let c = Bitfield::from_bytes(vec![0b1000, 0b0001], 16).unwrap();
let a = Bitfield::from_raw_bytes(vec![0b1100, 0b0001], 16).unwrap();
let b = Bitfield::from_raw_bytes(vec![0b1011, 0b1001], 16).unwrap();
let c = Bitfield::from_raw_bytes(vec![0b1000, 0b0001], 16).unwrap();
assert_eq!(a.intersection(&b).unwrap(), c);
assert_eq!(b.intersection(&a).unwrap(), c);
@ -462,9 +649,9 @@ mod test {
#[test]
fn union() {
let a = Bitfield::from_bytes(vec![0b1100, 0b0001], 16).unwrap();
let b = Bitfield::from_bytes(vec![0b1011, 0b1001], 16).unwrap();
let c = Bitfield::from_bytes(vec![0b1111, 0b1001], 16).unwrap();
let a = Bitfield::from_raw_bytes(vec![0b1100, 0b0001], 16).unwrap();
let b = Bitfield::from_raw_bytes(vec![0b1011, 0b1001], 16).unwrap();
let c = Bitfield::from_raw_bytes(vec![0b1111, 0b1001], 16).unwrap();
assert_eq!(a.union(&b).unwrap(), c);
assert_eq!(b.union(&a).unwrap(), c);
@ -475,10 +662,10 @@ mod test {
#[test]
fn difference() {
let a = Bitfield::from_bytes(vec![0b1100, 0b0001], 16).unwrap();
let b = Bitfield::from_bytes(vec![0b1011, 0b1001], 16).unwrap();
let a_b = Bitfield::from_bytes(vec![0b0100, 0b0000], 16).unwrap();
let b_a = Bitfield::from_bytes(vec![0b0011, 0b1000], 16).unwrap();
let a = Bitfield::from_raw_bytes(vec![0b1100, 0b0001], 16).unwrap();
let b = Bitfield::from_raw_bytes(vec![0b1011, 0b1001], 16).unwrap();
let a_b = Bitfield::from_raw_bytes(vec![0b0100, 0b0000], 16).unwrap();
let b_a = Bitfield::from_raw_bytes(vec![0b0011, 0b1000], 16).unwrap();
assert_eq!(a.difference(&b).unwrap(), a_b);
assert_eq!(b.difference(&a).unwrap(), b_a);
@ -487,7 +674,7 @@ mod test {
#[test]
fn iter() {
let mut bitfield = Bitfield::with_capacity(9);
let mut bitfield = Bitfield::with_capacity(9).unwrap();
bitfield.set(2, true);
bitfield.set(8, true);

View File

@ -1,12 +1,13 @@
#[macro_use]
mod bitfield;
mod bit_list;
mod bit_vector;
// mod bit_list;
// mod bit_vector;
mod fixed_vector;
mod variable_list;
pub use bit_list::BitList;
pub use bit_vector::BitVector;
// pub use bit_list::BitList;
// pub use bit_vector::BitVector;
pub use bitfield::{BitList, BitVector, Bitfield};
pub use fixed_vector::FixedVector;
pub use typenum;
pub use variable_list::VariableList;