Skip to content

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

Merged
merged 2 commits into from
May 19, 2017

Conversation

fitzgen
Copy link
Member

@fitzgen fitzgen commented May 19, 2017

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

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.
@highfive
Copy link

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@fitzgen
Copy link
Member Author

fitzgen commented May 19, 2017

So I side stepped the unstable testing question by always generating bitfield constructors, but only making them const if we're generating unstable Rust. There is a tiny amount of untested code here, but I'm pretty happy with the result, and I think it makes sense to give access to these bitfield constructors all the time.

Copy link
Contributor

@emilio emilio left a 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 =
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow

ast::ItemKind::Impl(_, _, _, _, _, ref items) => {
assert_eq!(items.len(), 1);

match items.get(0).unwrap().node {
Copy link
Contributor

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?

Copy link
Contributor

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!(),
};

// ...

@emilio
Copy link
Contributor

emilio commented May 19, 2017

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.
@fitzgen
Copy link
Member Author

fitzgen commented May 19, 2017

@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.

@bors-servo
Copy link

📌 Commit 15b72b7 has been approved by emilio

@bors-servo
Copy link

⌛ Testing commit 15b72b7 with merge 25150ca...

bors-servo pushed a commit that referenced this pull request May 19, 2017
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
@bors-servo
Copy link

☀️ Test successful - status-travis
Approved by: emilio
Pushing 25150ca to master...

@bors-servo bors-servo merged commit 15b72b7 into rust-lang:master May 19, 2017
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.

4 participants