-
Notifications
You must be signed in to change notification settings - Fork 746
Add bitfield allocation unit constructors #707
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
Conversation
This commit gives bindgen the ability to generate constructors for bitfield allocation units. This enables more ergonomic use of struct literals for bindings structs that contain bitfields. Additionally, when we are generating unstable Rust, these constructors are marked as const functions. This enables the creation of const binding structs that contain bitfields.
So I side stepped the unstable testing question by always generating bitfield constructors, but only making them |
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.
There may be a better way to generate less verbose code, but I'm fine with the code being a bit simpler on the bindgen side, so r=me with nits addressed.
let bitfield_unit_val = | ||
{ | ||
let bitfield_unit_val = | ||
{ |
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.
Wow
src/codegen/mod.rs
Outdated
ast::ItemKind::Impl(_, _, _, _, _, ref items) => { | ||
assert_eq!(items.len(), 1); | ||
|
||
match items.get(0).unwrap().node { |
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'm pretty sure just doing items[0]
will yield a better error message. Can't it be done that way?
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.
Also, I think this method may be more readable writing it as:
let items = match ctor_impl.unwrap().node {
ast::ItemKind::Impl(_, _, ..., items) => items,
_ => unreachable!(),
};
assert_eq!(items.len(), 1);
let (sig, body) = match items[0].node {
ast::ImplItemKind::Method(sig, body) => (sig, body),
_ => unreachable!(),
};
// ...
Also, thanks for testing the constructors with enums, that's quite nice :) |
This commit flattens the nesting in `Bitfield::extend_ctor_impl`, as requested in review, because it was getting pretty deep. Should be easier to read now.
@bors-servo r=emilio Thanks for the speedy review! Yeah, the generated code isn't exactly pretty, but I agree that it isn't a major concern. What is important is making it correct and easy to maintain. |
📌 Commit 15b72b7 has been approved by |
Add bitfield allocation unit constructors This commit gives bindgen the ability to generate constructors for bitfield allocation units. This enables more ergonomic use of struct literals for bindings structs that contain bitfields. Additionally, when we are generating unstable Rust, these constructors are marked as const functions. This enables the creation of const binding structs that contain bitfields. (Something necessary for Servo's usage of SpiderMonkey). r? @emilio
☀️ Test successful - status-travis |
This commit gives bindgen the ability to generate constructors for bitfield allocation units. This enables more ergonomic use of struct literals for bindings structs that contain bitfields.
Additionally, when we are generating unstable Rust, these constructors are marked as const functions. This enables the creation of const binding structs that contain bitfields. (Something necessary for Servo's usage of SpiderMonkey).
r? @emilio