Skip to content

Commit 91f4cc5

Browse files
committed
Implement Display for BitFlags (shows only the flags)
1 parent e1c8b64 commit 91f4cc5

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ let b_c_d = make_bitflags!(Test::{B | C | D});
4141

4242
// The debug output lets you inspect both the numeric value and
4343
// the actual flags:
44+
assert_eq!(format!("{:?}", a_b), "BitFlags<Test>(0b11, A | B)");
4445

45-
// BitFlags<Test>(0b11, [A, B])
46-
println!("{:?}", a_b);
47-
48-
// BitFlags<Test>(0b1, [A])
49-
println!("{:?}", a_b & a_c);
46+
// But if you'd rather see only one of those, that's available too:
47+
assert_eq!(format!("{}", a_b), "A | B");
48+
assert_eq!(format!("{:04b}", a_b), "0011");
5049

5150
// Iterate over the flags like a normal set
5251
assert_eq!(a_b.iter().collect::<Vec<_>>(), &[Test::A, Test::B]);

src/formatting.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ where
3434
}
3535
}
3636

37+
impl<T> fmt::Display for BitFlags<T>
38+
where
39+
T: BitFlag + fmt::Debug,
40+
{
41+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
42+
fmt::Debug::fmt(&FlagFormatter(self.iter()), fmt)
43+
}
44+
}
45+
3746
impl<T> fmt::Binary for BitFlags<T>
3847
where
3948
T: BitFlag,
@@ -88,9 +97,7 @@ impl<T: Debug, I: Clone + Iterator<Item = T>> Debug for FlagFormatter<I> {
8897
}
8998
Ok(())
9099
} else {
91-
// convention would print "<empty>" or similar here, but this is an
92-
// internal API that is never called that way, so just do nothing.
93-
Ok(())
100+
fmt.write_str("<empty>")
94101
}
95102
}
96103
}
@@ -132,7 +139,7 @@ fn flag_formatter() {
132139
};
133140
}
134141

135-
assert_fmt!("{:?}", iter::empty::<u8>(), "");
142+
assert_fmt!("{:?}", iter::empty::<u8>(), "<empty>");
136143
assert_fmt!("{:?}", iter::once(1), "1");
137144
assert_fmt!("{:?}", [1, 2].iter(), "1 | 2");
138145
assert_fmt!("{:?}", [1, 2, 10].iter(), "1 | 2 | 10");

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
//!
2525
//! // The debug output lets you inspect both the numeric value and
2626
//! // the actual flags:
27+
//! assert_eq!(format!("{:?}", a_b), "BitFlags<Test>(0b11, A | B)");
2728
//!
28-
//! // BitFlags<Test>(0b11, [A, B])
29-
//! println!("{:?}", a_b);
30-
//!
31-
//! // BitFlags<Test>(0b1, [A])
32-
//! println!("{:?}", a_b & a_c);
29+
//! // But if you'd rather see only one of those, that's available too:
30+
//! assert_eq!(format!("{}", a_b), "A | B");
31+
//! assert_eq!(format!("{:04b}", a_b), "0011");
3332
//!
3433
//! // Iterate over the flags like a normal set
3534
//! assert_eq!(a_b.iter().collect::<Vec<_>>(), &[Test::A, Test::B]);

test_suite/tests/requires_std.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ fn debug_format() {
1818
"BitFlags<Test>(0b0)"
1919
);
2020

21+
assert_eq!(
22+
format!("{:?}", BitFlags::from_flag(Test::B)),
23+
"BitFlags<Test>(0b10, B)"
24+
);
25+
2126
assert_eq!(
2227
format!("{:04x?}", BitFlags::<Test>::all()),
2328
"BitFlags<Test>(0x0f, A | B | C | D)"
@@ -56,6 +61,28 @@ fn debug_format_alternate() {
5661
);
5762
}
5863

64+
#[test]
65+
fn display_format() {
66+
use enumflags2::BitFlags;
67+
68+
// Assert that our Debug output format meets expectations
69+
70+
assert_eq!(
71+
format!("{}", BitFlags::<Test>::all()),
72+
"A | B | C | D"
73+
);
74+
75+
assert_eq!(
76+
format!("{}", BitFlags::<Test>::empty()),
77+
"<empty>"
78+
);
79+
80+
assert_eq!(
81+
format!("{}", BitFlags::from_flag(Test::B)),
82+
"B"
83+
);
84+
}
85+
5986
#[test]
6087
fn format() {
6188
use enumflags2::BitFlags;

0 commit comments

Comments
 (0)