Skip to content

Commit

Permalink
feat(bitfield): skip _ fields in fmt::Debug (#375)
Browse files Browse the repository at this point in the history
Currently, the derived `fmt::Debug` impl for bitfields will print the
value of reserved fields (those whose names start with `_`). This is
rarely useful, and adds noise to the formatted output.

This branch updates the `fmt::Debug` impl to skip formatting those
fields.
  • Loading branch information
hawkw committed Dec 1, 2022
1 parent 8ba3273 commit 46ca526
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion bitfield/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,16 @@ macro_rules! bitfield {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut dbg = f.debug_struct(stringify!($Name));
$(
dbg.field(stringify!($Field), &self.get(Self::$Field));
{
// skip reserved fields (names starting with `_`).
//
// NOTE(eliza): i hope this `if` gets const-folded...we
// could probably do this in a macro and guarantee that
// it happens at compile-time, but this is fine for now.
if !stringify!($Field).starts_with('_') {
dbg.field(stringify!($Field), &self.get(Self::$Field));
}
}
)+
dbg.finish()

Expand Down

0 comments on commit 46ca526

Please sign in to comment.