From c85d5c8d762e1f5044f0f738dfa115ba3aaa4e2a Mon Sep 17 00:00:00 2001 From: Piotr Czarnecki Date: Sun, 1 Dec 2024 10:37:16 +0100 Subject: [PATCH] Implement Display and fix Debug --- src/lib.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 899b9dd..4f46df8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -900,6 +900,14 @@ impl BitSet { } impl fmt::Debug for BitSet { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("BitSet") + .field("bit_vec", &self.bit_vec) + .finish() + } +} + +impl fmt::Display for BitSet { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_set().entries(self).finish() } @@ -1144,13 +1152,27 @@ mod tests { use std::{format, vec}; #[test] - fn test_bit_set_show() { + fn test_bit_set_display() { + let mut s = BitSet::new(); + s.insert(1); + s.insert(10); + s.insert(50); + s.insert(2); + assert_eq!("{1, 2, 10, 50}", format!("{}", s)); + } + + #[test] + fn test_bit_set_debug() { let mut s = BitSet::new(); s.insert(1); s.insert(10); s.insert(50); s.insert(2); - assert_eq!("{1, 2, 10, 50}", format!("{:?}", s)); + let expected = "BitSet { bit_vec: BitVec { storage: \ + \"01100000001000000000000000000000 \ + 0000000000000000001\", nbits: 51 } }"; + let actual = format!("{:?}", s); + assert_eq!(expected, actual); } #[test]