-
Notifications
You must be signed in to change notification settings - Fork 144
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
Split generated code into two types #282
Conversation
@@ -382,77 +432,83 @@ macro_rules! bitflags { | |||
} | |||
} | |||
|
|||
const _: () = { |
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.
What's the reason for putting this in a const _: () = { ... }
?
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.
It turns out const _: () = { .. }
is a great container for code generated by macros because:
- It's supported in any context where other items can be generated. You can write them in modules, functions, and the middle of other expressions.
- It gives us a new scope to define items in. Any types we define in the body of this
const
don't conflict with any outside of it. Any types we define in theconst
are also unreachable from outside of it. That's why we've got a trait with an associated type that lets us peek inside. We can even wrap the contents of theconst
body in amod
to hide fields if we want.
Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
cc @konsumlamm @arturoc What do you all think of this new internal library organization? Are you happy with this direction? |
I like it! No complaints from me. |
looks good to me |
Alrighty! I think the only thing remaining before we could consider stabilizing is adding an associated type for the iterator to the I'll do that in a follow-up, then look at publishing a pre-release build. |
Closes #168
Closes #155
Closes #218
Closes #263
Closes #228
Following on from #264
Part of #262
This PR is a refactoring of the generated flags types to make the API ownership between us (
bitflags
) and them (callers ofbitflags!
) clearer. We generate a public type with an API that we can't make a lot of change to outside of theBitFlags
trait, and a private type that can hold trait implementations that users may choose to bubble up publicly.This makes it possible to pick different semantics for traits like
Ord
, and add optional support for external libraries likeArbitrary
.It comes with some breakage though:
#[derive(Serialize)]
impl, you'll need a#[serde(transparent)]
attribute to ensure your format doesn't change.#[derive(Debug)]
, you'll now get your flags wrapped in the name of your type (newtype formatting)..bits
field is now a.bits()
method.We'll need to give this a thorough test once it builds to make sure it's workable for all the places
bitflags
is currently used, but I think this is a promising future direction for the project. Kind of like thelazy_static
crate, a lot of our old complexity goes away when we don't need to work around some old language limitations.