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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,15 @@ impl Cursor {

/// Get the signed constant value for this cursor's enum variant referent.
///
/// 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).
pub fn enum_val_signed(&self) -> i64 {
unsafe { clang_getEnumConstantDeclValue(self.x) as i64 }
/// Returns None if the cursor's referent is not an enum variant.
pub fn enum_val_signed(&self) -> Option<i64> {
unsafe {
if self.kind() == CXCursor_EnumConstantDecl {
Some(clang_getEnumConstantDeclValue(self.x) as i64)
} else {
None
}
}
}

/// Get the unsigned constant value for this cursor's enum variant referent.
Expand Down
15 changes: 8 additions & 7 deletions src/ir/enum_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,19 @@ 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 :)

let name = cursor.spelling();
let comment = cursor.raw_comment();
let val = if is_signed {
EnumVariantValue::Signed(cursor.enum_val_signed())
let value = if is_signed {
cursor.enum_val_signed().map(EnumVariantValue::Signed)
} else {
EnumVariantValue::Unsigned(cursor.enum_val_unsigned())
Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned()))
};
variants.push(EnumVariant::new(name, comment, val));
if let Some(val) = value {
let name = cursor.spelling();
let comment = cursor.raw_comment();
variants.push(EnumVariant::new(name, comment, val));
}
}
CXChildVisit_Continue
});

Ok(Enum::new(repr, variants))
}
}
Expand Down