-
Notifications
You must be signed in to change notification settings - Fork 748
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
Conversation
@@ -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). |
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.
That comment is now obsolete.
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.
So it is. Fixed.
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.
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 { |
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.
why not keeping the if
above as before, and then just unwrap
the result of enum_val_signed
?
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 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.
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 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.
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.
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?
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 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)) |
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.
Just for reference, you can do cursor.enum_val_signed().map(EnumVariantValue::Signed)
:)
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.
Makes sense. Updated.
variants.push(EnumVariant::new(name, comment, 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.
This semicolon shouldn't be needed.
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.
Removed.
Also reorganize calling function to avoid duplicate checking of cursor type. Fixes rust-lang#127
Squashed commits. |
let val = if is_signed { | ||
cursor.enum_val_signed().map(EnumVariantValue::Signed) | ||
} else { | ||
if cursor.kind() == CXCursor_EnumConstantDecl { |
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.
This looks like too much rightward drift. Why not meld this into the previous level and use else if
?
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 wanted to keep it visually separate, because the inner if/else block can go away after #128 lands.
0bdc4de
to
fda0548
Compare
@bors-servo r+ Thanks! :) |
📌 Commit fda0548 has been approved by |
Wrap enum_val_signed in an Option. Also reorganize calling code to avoid duplicate checking of cursor type. Fixes #127
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit fda0548 has been approved by |
☀️ Test successful - status-travis |
Also reorganize calling code to avoid duplicate checking of cursor type.
Fixes #127