From 928bafb3da920b9c9172b1e3b0d6e5f92ba4826c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 19 Sep 2018 16:45:32 +1000 Subject: [PATCH] Ensure an empty bitfield serializes to 1 byte --- boolean-bitfield/src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/boolean-bitfield/src/lib.rs b/boolean-bitfield/src/lib.rs index 0a690bf3d..a22d9cde4 100644 --- a/boolean-bitfield/src/lib.rs +++ b/boolean-bitfield/src/lib.rs @@ -19,15 +19,17 @@ impl BooleanBitfield { pub fn new() -> Self { Self { len: 0, - vec: vec![] + vec: vec![0] } } /// Create a new bitfield of a certain capacity pub fn with_capacity(capacity: usize) -> Self { + let mut vec = Vec::with_capacity(capacity / 8 + 1); + vec.push(0); Self { len: 0, - vec: Vec::with_capacity(capacity / 8 + 1) + vec } } @@ -112,6 +114,17 @@ impl PartialEq for BooleanBitfield { mod tests { use super::*; + #[test] + fn test_new_bitfield_len() { + let b = BooleanBitfield::new(); + assert_eq!(b.len(), 0); + assert_eq!(b.to_be_vec(), vec![0]); + + let b = BooleanBitfield::with_capacity(100); + assert_eq!(b.len(), 0); + assert_eq!(b.to_be_vec(), vec![0]); + } + #[test] fn test_bitfield_set() { let mut b = BooleanBitfield::new();