From 3a0f1725f1fb2c5eeee895ffa555a8ae33aa5a77 Mon Sep 17 00:00:00 2001 From: wyhaya Date: Thu, 4 Jul 2024 15:59:58 +0800 Subject: [PATCH 1/2] Implement the Debug trait for BitVec --- src/lib.rs | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 60f6563..415fdbe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1708,10 +1708,18 @@ impl fmt::Display for BitVec { impl fmt::Debug for BitVec { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.ensure_invariant(); - for bit in self { - write!(fmt, "{}", if bit { 1 } else { 0 })?; + let mut storage = std::string::String::new(); + let mut iter = self.iter().enumerate(); + while let Some((i, bit)) = iter.next() { + if i != 0 && i % B::bits() == 0 { + storage.push(' '); + } + storage.push(if bit { '1' } else { '0' }); } - Ok(()) + fmt.debug_struct("BitVec") + .field("storage", &storage) + .field("nbits", &self.nbits) + .finish() } } @@ -1935,12 +1943,30 @@ mod tests { const U32_BITS: usize = 32; #[test] - fn test_to_str() { - let zerolen = BitVec::new(); - assert_eq!(format!("{:?}", zerolen), ""); + fn test_display_output() { + assert_eq!(format!("{}", BitVec::new()), ""); + assert_eq!(format!("{}", BitVec::from_elem(1, true)), "1"); + assert_eq!(format!("{}", BitVec::from_elem(8, false)), "00000000") + } - let eightbits = BitVec::from_elem(8, false); - assert_eq!(format!("{:?}", eightbits), "00000000") + #[test] + fn test_debug_output() { + assert_eq!( + format!("{:?}", BitVec::new()), + "BitVec { storage: \"\", nbits: 0 }" + ); + assert_eq!( + format!("{:?}", BitVec::from_elem(1, true)), + "BitVec { storage: \"1\", nbits: 1 }" + ); + assert_eq!( + format!("{:?}", BitVec::from_elem(8, false)), + "BitVec { storage: \"00000000\", nbits: 8 }" + ); + assert_eq!( + format!("{:?}", BitVec::from_elem(33, true)), + "BitVec { storage: \"11111111111111111111111111111111 1\", nbits: 33 }" + ) } #[test] @@ -1966,7 +1992,7 @@ mod tests { let mut b = BitVec::from_elem(2, false); b.set(0, true); b.set(1, false); - assert_eq!(format!("{:?}", b), "10"); + assert_eq!(format!("{}", b), "10"); assert!(!b.none() && !b.all()); } @@ -2316,7 +2342,7 @@ mod tests { fn test_from_bytes() { let bit_vec = BitVec::from_bytes(&[0b10110110, 0b00000000, 0b11111111]); let str = concat!("10110110", "00000000", "11111111"); - assert_eq!(format!("{:?}", bit_vec), str); + assert_eq!(format!("{}", bit_vec), str); } #[test] @@ -2335,7 +2361,7 @@ mod tests { fn test_from_bools() { let bools = [true, false, true, true]; let bit_vec: BitVec = bools.iter().copied().collect(); - assert_eq!(format!("{:?}", bit_vec), "1011"); + assert_eq!(format!("{}", bit_vec), "1011"); } #[test] From 90dd6c2f10fbece066aee323c5b759205f34e0b1 Mon Sep 17 00:00:00 2001 From: wyhaya Date: Thu, 4 Jul 2024 16:36:46 +0800 Subject: [PATCH 2/2] Fix debug impl --- src/lib.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 415fdbe..c940c86 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,6 +91,8 @@ extern crate std; #[cfg(feature = "std")] use std::rc::Rc; #[cfg(feature = "std")] +use std::string::String; +#[cfg(feature = "std")] use std::vec::Vec; #[cfg(feature = "serde")] @@ -112,6 +114,8 @@ extern crate alloc; #[cfg(not(feature = "std"))] use alloc::rc::Rc; #[cfg(not(feature = "std"))] +use alloc::string::String; +#[cfg(not(feature = "std"))] use alloc::vec::Vec; use core::cell::RefCell; @@ -1708,9 +1712,8 @@ impl fmt::Display for BitVec { impl fmt::Debug for BitVec { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { self.ensure_invariant(); - let mut storage = std::string::String::new(); - let mut iter = self.iter().enumerate(); - while let Some((i, bit)) = iter.next() { + let mut storage = String::with_capacity(self.len() + self.len() / B::bits()); + for (i, bit) in self.iter().enumerate() { if i != 0 && i % B::bits() == 0 { storage.push(' '); } @@ -1966,6 +1969,13 @@ mod tests { assert_eq!( format!("{:?}", BitVec::from_elem(33, true)), "BitVec { storage: \"11111111111111111111111111111111 1\", nbits: 33 }" + ); + assert_eq!( + format!( + "{:?}", + BitVec::from_bytes(&[0b111, 0b000, 0b1110, 0b0001, 0b11111111, 0b00000000]) + ), + "BitVec { storage: \"00000111000000000000111000000001 1111111100000000\", nbits: 48 }" ) }