Skip to content

Commit

Permalink
Implement Display for BitFlags (shows only the flags)
Browse files Browse the repository at this point in the history
  • Loading branch information
meithecatte committed Apr 9, 2022
1 parent e1c8b64 commit 91f4cc5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ let b_c_d = make_bitflags!(Test::{B | C | D});

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

// BitFlags<Test>(0b11, [A, B])
println!("{:?}", a_b);

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

// Iterate over the flags like a normal set
assert_eq!(a_b.iter().collect::<Vec<_>>(), &[Test::A, Test::B]);
Expand Down
15 changes: 11 additions & 4 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ where
}
}

impl<T> fmt::Display for BitFlags<T>
where
T: BitFlag + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&FlagFormatter(self.iter()), fmt)
}
}

impl<T> fmt::Binary for BitFlags<T>
where
T: BitFlag,
Expand Down Expand Up @@ -88,9 +97,7 @@ impl<T: Debug, I: Clone + Iterator<Item = T>> Debug for FlagFormatter<I> {
}
Ok(())
} else {
// convention would print "<empty>" or similar here, but this is an
// internal API that is never called that way, so just do nothing.
Ok(())
fmt.write_str("<empty>")
}
}
}
Expand Down Expand Up @@ -132,7 +139,7 @@ fn flag_formatter() {
};
}

assert_fmt!("{:?}", iter::empty::<u8>(), "");
assert_fmt!("{:?}", iter::empty::<u8>(), "<empty>");
assert_fmt!("{:?}", iter::once(1), "1");
assert_fmt!("{:?}", [1, 2].iter(), "1 | 2");
assert_fmt!("{:?}", [1, 2, 10].iter(), "1 | 2 | 10");
Expand Down
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
//!
//! // The debug output lets you inspect both the numeric value and
//! // the actual flags:
//! assert_eq!(format!("{:?}", a_b), "BitFlags<Test>(0b11, A | B)");
//!
//! // BitFlags<Test>(0b11, [A, B])
//! println!("{:?}", a_b);
//!
//! // BitFlags<Test>(0b1, [A])
//! println!("{:?}", a_b & a_c);
//! // But if you'd rather see only one of those, that's available too:
//! assert_eq!(format!("{}", a_b), "A | B");
//! assert_eq!(format!("{:04b}", a_b), "0011");
//!
//! // Iterate over the flags like a normal set
//! assert_eq!(a_b.iter().collect::<Vec<_>>(), &[Test::A, Test::B]);
Expand Down
27 changes: 27 additions & 0 deletions test_suite/tests/requires_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ fn debug_format() {
"BitFlags<Test>(0b0)"
);

assert_eq!(
format!("{:?}", BitFlags::from_flag(Test::B)),
"BitFlags<Test>(0b10, B)"
);

assert_eq!(
format!("{:04x?}", BitFlags::<Test>::all()),
"BitFlags<Test>(0x0f, A | B | C | D)"
Expand Down Expand Up @@ -56,6 +61,28 @@ fn debug_format_alternate() {
);
}

#[test]
fn display_format() {
use enumflags2::BitFlags;

// Assert that our Debug output format meets expectations

assert_eq!(
format!("{}", BitFlags::<Test>::all()),
"A | B | C | D"
);

assert_eq!(
format!("{}", BitFlags::<Test>::empty()),
"<empty>"
);

assert_eq!(
format!("{}", BitFlags::from_flag(Test::B)),
"B"
);
}

#[test]
fn format() {
use enumflags2::BitFlags;
Expand Down

0 comments on commit 91f4cc5

Please sign in to comment.