FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Make a nice Debug impl for each bitflags. by emberian · Pull Request #7 · bitflags/bitflags · GitHub

Make a nice Debug impl for each bitflags. - #7

Merged
alexcrichton merged 1 commit into
bitflags:masterfrom
emberian:master
Jun 1, 2015
Merged

Make a nice Debug impl for each bitflags.#7
alexcrichton merged 1 commit into
bitflags:masterfrom
emberian:master

Conversation

Copy link
Copy Markdown
Contributor

This adds a Debug impl where previously there was none, so this is a

[breaking-change]

Trying to actually debug anything using bitflags is otherwise absolutely infuriating.

Copy link
Copy Markdown
Contributor Author

r? @alexcrichton

This adds a Debug impl where previously there was none, so this is a

[breaking-change]

Copy link
Copy Markdown
Contributor

Thanks!

alexcrichton added a commit that referenced this pull request Jun 1, 2015
Make a nice Debug impl for each bitflags.
alexcrichton merged commit 772c404 into bitflags:master Jun 1, 2015
chris-morgan added a commit to chris-morgan/bitflags that referenced this pull request Jun 26, 2015
`Debug` was implemented using the nice readable flag names in bitflags#7, but
the flag names were removed in bitflags#9 because it used `$Flags` which broke
trivial `#[cfg]`-based removal of flags. Changing `.contains($Flag)` to
`.contains($BitFlags { bits: $value })` instead (akin to what `.all()`
did) would have fixed that issue, allowing a friendly `Debug` again, but
then I found a further improvement to make, because the `#[cfg]`
behaviour was still off, as a flag’s value was still written out
regardless of the `#[cfg]` attribute, so if *it* contained stuff that
didn’t exist `bitflags!` would still blow up. This is inconsistent with
how such things work in Rust proper, so I’ve fixed it.

The fix is rather convoluted, involving a dummy module, a bunch of dummy
constants, a nested function and glob imports, but it works, avoiding
referring to `$value` successfully. This change causes this to work:

```rust
bitflags! {
    flags Foo: u32 {
        #[cfg(a)]
        const A = 0b1,
        #[cfg(a)]
        const B = A.bits,  // or anything else that is `#[cfg(a)]`
    }
}
```

(Formerly `all()` was still trying to use `A.bits` when `cfg(not(a))`
and thus `A` wasn’t defined.)

This change allows `#[cfg]` on a flag to work like it should, allowing
it to depend on things that don’t exist unless the conditions are met.
Sure, that seems a pretty rare case for flags, but it’s real. Beyond
the style of example above, my imagined scenario is a -sys crate
having declared certain flag values, but only when certain features are
enabled, and so when defining the bitflags in another crate (for the
-sys crate doesn’t use bitflags) you can only use those values with the
appropriate features.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL