Fix bug in bitfield.

This commit is contained in:
Paul Hauner 2019-04-02 15:34:18 +11:00
parent f61db9cac8
commit 1d34e2b2a5
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6

View File

@ -33,14 +33,23 @@ impl BooleanBitfield {
}
/// 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) -> Self {
// BitVec can panic if we don't set the len to be a multiple of 8.
let len = ((initial_len + 7) / 8) * 8;
Self {
0: BitVec::from_elem(len, bit),
let full_len = ((initial_len + 7) / 8) * 8;
let mut bitfield = BitVec::from_elem(full_len, false);
if bit {
for i in 0..initial_len {
bitfield.set(i, true);
}
}
Self { 0: bitfield }
}
/// Create a new bitfield using the supplied `bytes` as input
pub fn from_bytes(bytes: &[u8]) -> Self {
Self {