Skip to content

Commit

Permalink
Minor cleanup after #1419.
Browse files Browse the repository at this point in the history
The previous PR ended up with a lot of just-called-once methods. Just inline
them since they're confusing otherwise.

Also avoid testing all the variants of an enum if there was a match already, or
if the enum is not anonymous. This is mostly a minor optimization.
  • Loading branch information
emilio committed Oct 11, 2018
1 parent c137196 commit 6055f36
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions src/ir/enum_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,46 +143,29 @@ impl Enum {
let path = item.canonical_path(ctx);
let enum_ty = item.expect_type();

let path_matches = enums.matches(&path[1..].join("::"));
let enum_is_anon = enum_ty.name().is_none();
let a_variant_matches = self.variants().iter().any(|v| {
enums.matches(&v.name())
});
path_matches || (enum_is_anon && a_variant_matches)
}

/// Whether the enum was explicitly specified to be a bitfield.
fn is_bitfield(&self, ctx: &BindgenContext, item: &Item) -> bool {
self.is_matching_enum(ctx, &ctx.options().bitfield_enums, item)
}

/// Whether the enum was explicitly specified to be an constified enum
/// module.
fn is_constified_enum_module(&self, ctx: &BindgenContext, item: &Item) -> bool {
self.is_matching_enum(ctx, &ctx.options().constified_enum_modules, item)
}
if enums.matches(&path[1..].join("::")) {
return true;
}

/// Whether the enum was explicitly specified to be an set of constants.
fn is_constified_enum(&self, ctx: &BindgenContext, item: &Item) -> bool {
self.is_matching_enum(ctx, &ctx.options().constified_enums, item)
}
// Test the variants if the enum is anonymous.
if enum_ty.name().is_some() {
return false;
}

/// Whether the enum was explicitly specified to be a Rust enum.
fn is_rustified_enum(&self, ctx: &BindgenContext, item: &Item) -> bool {
self.is_matching_enum(ctx, &ctx.options().rustified_enums, item)
self.variants().iter().any(|v| enums.matches(&v.name()))
}

/// Returns the final representation of the enum.
pub fn computed_enum_variation(&self, ctx: &BindgenContext, item: &Item) -> EnumVariation {
// ModuleConsts has higher precedence before Rust in order to avoid
// problems with overlapping match patterns.
if self.is_constified_enum_module(ctx, item) {
if self.is_matching_enum(ctx, &ctx.options().constified_enum_modules, item) {
EnumVariation::ModuleConsts
} else if self.is_bitfield(ctx, item) {
} else if self.is_matching_enum(ctx, &ctx.options().bitfield_enums, item) {
EnumVariation::Bitfield
} else if self.is_rustified_enum(ctx, item) {
} else if self.is_matching_enum(ctx, &ctx.options().rustified_enums, item) {
EnumVariation::Rust
} else if self.is_constified_enum(ctx, item) {
} else if self.is_matching_enum(ctx, &ctx.options().constified_enums, item) {
EnumVariation::Consts
} else {
ctx.options().default_enum_style
Expand Down

0 comments on commit 6055f36

Please sign in to comment.