Fix bug in bitfield.
This commit is contained in:
parent
f61db9cac8
commit
1d34e2b2a5
@ -33,14 +33,23 @@ impl BooleanBitfield {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new bitfield with the given length `initial_len` and all values set to `bit`.
|
/// 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 {
|
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.
|
// BitVec can panic if we don't set the len to be a multiple of 8.
|
||||||
let len = ((initial_len + 7) / 8) * 8;
|
let full_len = ((initial_len + 7) / 8) * 8;
|
||||||
Self {
|
let mut bitfield = BitVec::from_elem(full_len, false);
|
||||||
0: BitVec::from_elem(len, bit),
|
|
||||||
|
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
|
/// Create a new bitfield using the supplied `bytes` as input
|
||||||
pub fn from_bytes(bytes: &[u8]) -> Self {
|
pub fn from_bytes(bytes: &[u8]) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
Loading…
Reference in New Issue
Block a user