Debug formatting leads to less desireable output · Issue #215 · bitflags/bitflags · GitHub
Skip to content

Debug formatting leads to less desireable output #215

Closed

Description

@damageboy

Link to rust playground

#[macro_use]
extern crate bitflags;

bitflags! {
    struct Flags: u32 {
        const A = 0b00000001;
        const B = 0b00000010;
        const C = 0b00000100;
        const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
    }
}

fn main() {
    println!("{:?}", Flags::A | Flags::B | Flags::C );
}

prints:

A | B | C | ABC

I find it somewhat less helpful that both the expanded (A | B | C) and "compressed" form (ABC) are reported...

Is there a reason behind this? Is this considered more correct for some reason?

Activity

  1. KodrAus commented on Oct 25, 2021

    @KodrAus
    Member

    I think the current algorithm used for debug output is to loop over all flags and append identifiers that correspond to set bits. I think an alternative here that could work would be to short-circuit when we’ve built a format that covers all the set bits. As an implementation note we wouldn’t be able to work off a single source and just mask out bits as we see them, we’ll need to use the whole set of bits to see if a flag is applicable and then a second set that’s masked to see when we’ve catered for all set bits. Otherwise something like this wouldn’t work:

    const A: 0b00000100
    const B: 0b00001100
    
    let input = B;
    

    we’d mask out bit 3 for A but then have one leftover for B that doesn’t correspond to it. In that example we’d end up writing A | B still, but wouldn’t duplicate compound identifiers the same.

    In general, I’m not sure if there’s a reasonable algorithm that would produce the smallest possible format for any given set of bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions