-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debug impl changes #7
Conversation
Yeah, that part is ugly and will need more explanation. The story is basically:
Which leaves the only decent stable way I know of determining the formatter used to be the |
045e261
to
6c900d3
Compare
Moved the strangeness into its own formatter struct so it's fairly isolated and documented. I wish it were possible to do things a bit better/neater, but the Formatter API is pretty inflexible. This is yet another case of std not dogfooding and just resorting to mutating private fields to achieve desired behaviour, which disadvantages external types since they obviously can't mimic the same approach. Also implemented the binary/hex/etc. formatters for BitFlags, since it seems pretty reasonable to expect the bits to be explicitly formatted. I'm holding off on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked for a while, and I couldn't find any better way to implement this, so I guess the `#[allow(deprecated)]
I'm holding off on
Display
because I'm not sure there's much value in just formatting them as a decimal?
Not only is there little value in decimal output, but it's also most likely not the right choice for user-facing output.
I feel like this is actually a case where it'd be a good idea to bring back
BitFlagsFmt
as a public API to allow the downstream enum itself to implDisplay
however it wishes
I wouldn't really worry about this. You can't impl Display for Vec<MyType>
and nobody complains.
Would you mind adding some unit tests for this functionality? (assert_eq!(format!(...), "...");
) I'd put them as a mod tests
right next to the actual formatting code.
enumflags/src/lib.rs
Outdated
@@ -69,21 +70,57 @@ mod details { | |||
impl BitFlagNum for u32 {} | |||
impl BitFlagNum for u64 {} | |||
impl BitFlagNum for usize {} | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The helper formatter structs really shouldn't be in this module. What it's for is only the BitFlagNum
trait - it needs to be a part of public interface, but we don't want anyone implementing it on their own types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, would you prefer...
- their own private formatting module?
- root of the crate above the Debug impl?
- inside the debug fmt() function? (too indenty/messy imo)
Also possibly a public user-facing flag_fmt function or something on BitFlags? A | B
may sometimes be useful to provide without the rest of the Debug output wrapping it.
Likely not, though there are a few cases where having ToString (and a mirror FromStr) is useful for interacting with text interfaces/serialization? But yeah the context for what "user-facing" means is vague, for example one could imagine wanting a "r-x" encoding for file permission flags. Another thought about BitFlagsFmt: we could also bring the
I'd argue the difference is that This is a general shortcoming of the design though; despite there often being want to, you can't really impl anything for
Yup! |
Whee more changes:
|
The internal `BitFlagsFmt` trait has been removed and replaced with a `bitflags_type_name()` method.
Good job! I don't see anything to improve here, making this perfect by at least one measure :) By the way, would you like to add yourself to |
This is a common
Debug
implementation with a lot less codegen per enum required. You can now use format specifiers like{:02x?}
to getBitFlags<T>(0x03, A | B)
, and customDebug
impls for the enum will be used in place ofA
orB
if used.The internal
BitFlagsFmt
trait has been removed and replaced with abitflags_type_name()
method to get the same oldBitFlags<T>(...)
behaviour. A consequence of this is that the enum types must now be explicitly annotated with#[derive(Debug)]
in order for debug to work.