Skip to content

Wrap enum_val_signed in an Option. #220

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
Nov 6, 2016

Conversation

jcdyer
Copy link

@jcdyer jcdyer commented Nov 6, 2016

Also reorganize calling code to avoid duplicate checking of cursor type.

Fixes #127

@highfive
Copy link

highfive commented Nov 6, 2016

warning Warning warning

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

@@ -352,8 +352,14 @@ impl Cursor {
/// Returns `LLONG_MIN` if the cursor's referent is not an enum variant,
/// which is also a valid enum value, so callers should check the cursor
/// kind before calling this method (see issue #127).
Copy link
Contributor

Choose a reason for hiding this comment

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

That comment is now obsolete.

Copy link
Author

Choose a reason for hiding this comment

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

So it is. Fixed.

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.

Looks good to me with those changes, thanks for doing this! :)

@@ -71,16 +71,20 @@ impl Enum {
};

declaration.visit(|cursor| {
if cursor.kind() == CXCursor_EnumConstantDecl {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not keeping the if above as before, and then just unwrap the result of enum_val_signed?

Copy link
Author

Choose a reason for hiding this comment

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

I like to avoid unwrapping when it's easy to do, because it makes the code here less intertwined with code elsewhere; if enum_val_signed changes to return None for some other reason, that's a future bug avoided. I also think once #128 lands, it'll look nice and clean.

I'm pretty new at rust though, and brand new to rust-bindgen, so if I'm missing some bigger picture reason why it's worth keeping the duplicate check, I'm happy to change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I sort of agree, and in general it uses to be good advice. But in this case, IMO at least, this is inverting the dependency instead.

That is, if for some other reason enum_val_signed returns Some when it's not a CXCursor_EnumConstantDecl, the code meaning is completely different, and we're in trouble.

We're traversing the ast looking for EnumConstantDecl nodes, so I think that if should stay there instead of relying that this only returns Some if that's the case.

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense. Here's a new version with the outer if, but still without unwrap(), for the reason above. If I understand rust correctly, there shouldn't be any runtime time penalty, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there's a runtime penalty for using unwrap vs if let. Maybe rust annotates unwrap as unlikely so it has a bit better branch prediction, but nothing measurable for this case I think :)

@@ -71,16 +71,20 @@ impl Enum {
};

declaration.visit(|cursor| {
if cursor.kind() == CXCursor_EnumConstantDecl {
let val = if is_signed {
cursor.enum_val_signed().map(|val| EnumVariantValue::Signed(val))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just for reference, you can do cursor.enum_val_signed().map(EnumVariantValue::Signed) :)

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense. Updated.

variants.push(EnumVariant::new(name, comment, val));
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This semicolon shouldn't be needed.

Copy link
Author

@jcdyer jcdyer Nov 6, 2016

Choose a reason for hiding this comment

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

Removed.

Also reorganize calling function to avoid duplicate checking of cursor
type.

Fixes rust-lang#127
@jcdyer
Copy link
Author

jcdyer commented Nov 6, 2016

Squashed commits.

let val = if is_signed {
cursor.enum_val_signed().map(EnumVariantValue::Signed)
} else {
if cursor.kind() == CXCursor_EnumConstantDecl {
Copy link
Member

Choose a reason for hiding this comment

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

This looks like too much rightward drift. Why not meld this into the previous level and use else if?

Copy link
Author

Choose a reason for hiding this comment

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

I wanted to keep it visually separate, because the inner if/else block can go away after #128 lands.

@emilio
Copy link
Contributor

emilio commented Nov 6, 2016

@bors-servo r+

Thanks! :)

@bors-servo
Copy link

📌 Commit fda0548 has been approved by emilio

@bors-servo
Copy link

⌛ Testing commit fda0548 with merge b47f405...

bors-servo pushed a commit that referenced this pull request Nov 6, 2016
Wrap enum_val_signed in an Option.

Also reorganize calling code to avoid duplicate checking of cursor type.

Fixes #127
@bors-servo
Copy link

💡 This pull request was already approved, no need to approve it again.

  • This pull request is currently being tested. If there's no response from the continuous integration service, you may use retry to trigger a build again.

@bors-servo
Copy link

📌 Commit fda0548 has been approved by emilio

@bors-servo
Copy link

☀️ Test successful - status-travis

@bors-servo bors-servo merged commit fda0548 into rust-lang:master Nov 6, 2016
bors-servo pushed a commit that referenced this pull request Nov 7, 2016
Wrap enum_val_unsigned in an Option

Patterned after the changes merged in #220.

Fixes #128
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.

6 participants