-
Notifications
You must be signed in to change notification settings - Fork 682
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
Constify bitfield constructor for Rust 1.57+ #2314
Conversation
ddb2f4b
to
027bf68
Compare
@@ -236,6 +239,9 @@ rust_feature_def!( | |||
Stable_1_47 { | |||
=> larger_arrays; | |||
} | |||
Stable_1_57 { | |||
=> const_panic; |
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.
Could be lowered to 1.56 (gated by constification of core::mem::transmute
) if we are okay with removing the debug_asserts!
in the generated code.
027bf68
to
81f71ea
Compare
r? @fitzgen |
@hugwijst I like |
@kulp, I can provide an example. A lot of C APIs require there to be global (mutable) struct variables and these structures may have bitfields. In order to mimic this in Rust, we need to use This change lets the entire structure to be defined initially, as it is in C. Hopefully that provides some context 🙂 |
I've updated the PR description, happy to discuss alternatives too. |
bump |
Thanks @rockboynton for more explanation. I am sorry for the long delays. It feels like a lot of code changes to me for what it yields, but I will not pretend I am equipped to weigh the trade-offs here. |
👋 I think this is a nice addition! I'm wondering if there's a way to avoid having both
I would suggest to do change 1 and then do change 2 behind version 1.57 so most people can transition from the non-const version to the const version if they update the rust version. Take this suggestion with a grain of salt as I don't have the last word on this matter 😅 I would merge this without this suggestion anyway. |
Any update on this? We are actually relying on @hugwijst's fork right now and it would be great to see it merged. |
☔ The latest upstream changes (presumably 2034a0f) made this pull request unmergeable. Please resolve the merge conflicts. |
FYI, the current PR's closure may or may not have been intended. @pvdrz can comment. |
Allows creating structs with bitfields in a const context, for example:
This can be useful when interacting with existing C libraries in a bare-metal environment, as these sometimes expect global structs with specific symbols to exist.
The current workaround is to create a mutable struct with the bitfield assigned to something like
__BindgenBitfieldUnit::new([0; 2])
, and then call the appropriate field setters before the struct gets used. This has multiple downsides:__BindgenBitfieldUnit
.unsafe
if the struct also has to be static.