Skip to content

Commit c314a2f

Browse files
author
bors-servo
authored
Auto merge of #222 - afluth:enum_val_unsigned_option, r=emilio
Wrap enum_val_unsigned in an Option Patterned after the changes merged in #220. Fixes #128
2 parents b47f405 + feb6f67 commit c314a2f

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/clang.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,15 @@ impl Cursor {
362362

363363
/// Get the unsigned constant value for this cursor's enum variant referent.
364364
///
365-
/// Returns `ULLONG_MAX` if the cursor's referent is not an enum variant,
366-
/// which is also a valid enum value, so callers should check the cursor
367-
/// kind before calling this method (see issue #128).
368-
pub fn enum_val_unsigned(&self) -> u64 {
369-
unsafe { clang_getEnumConstantDeclUnsignedValue(self.x) as u64 }
365+
/// Returns None if the cursor's referent is not an enum variant.
366+
pub fn enum_val_unsigned(&self) -> Option<u64> {
367+
unsafe {
368+
if self.kind() == CXCursor_EnumConstantDecl {
369+
Some(clang_getEnumConstantDeclUnsignedValue(self.x) as u64)
370+
} else {
371+
None
372+
}
373+
}
370374
}
371375

372376
/// Given that this cursor's referent is a `typedef`, get the `Type` that is

src/ir/enum_ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Enum {
7575
let value = if is_signed {
7676
cursor.enum_val_signed().map(EnumVariantValue::Signed)
7777
} else {
78-
Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned()))
78+
cursor.enum_val_unsigned().map(EnumVariantValue::Unsigned)
7979
};
8080
if let Some(val) = value {
8181
let name = cursor.spelling();

0 commit comments

Comments
 (0)