Skip to content

optionally use associated constants in bitfields #1293

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 4 commits into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 30 additions & 17 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,7 @@ impl MethodCodegen for Method {
#[derive(Copy, Clone)]
enum EnumVariation {
Rust,
Bitfield,
Bitfield { use_associated_consts: bool },
Consts,
ModuleConsts
}
Expand All @@ -2126,7 +2126,7 @@ impl EnumVariation {

fn is_bitfield(&self) -> bool {
match *self {
EnumVariation::Bitfield => true,
EnumVariation::Bitfield {..} => true,
_ => false
}
}
Expand All @@ -2153,6 +2153,7 @@ enum EnumBuilder<'a> {
Bitfield {
codegen_depth: usize,
canonical_name: &'a str,
use_associated_consts: bool,
tokens: quote::Tokens,
},
Consts {
Expand Down Expand Up @@ -2189,10 +2190,11 @@ impl<'a> EnumBuilder<'a> {
let ident = quote::Ident::new(name);

match enum_variation {
EnumVariation::Bitfield => {
EnumVariation::Bitfield { use_associated_consts, .. } => {
EnumBuilder::Bitfield {
codegen_depth: enum_codegen_depth,
canonical_name: name,
use_associated_consts,
tokens: quote! {
#( #attrs )*
pub struct #ident (pub #repr);
Expand Down Expand Up @@ -2278,19 +2280,28 @@ impl<'a> EnumBuilder<'a> {
}
}

EnumBuilder::Bitfield { .. } => {
let constant_name = match mangling_prefix {
Some(prefix) => {
Cow::Owned(format!("{}_{}", prefix, variant_name))
}
None => variant_name,
};

let ident = ctx.rust_ident(constant_name);
result.push(quote! {
#doc
pub const #ident : #rust_ty = #rust_ty ( #expr );
});
EnumBuilder::Bitfield { canonical_name, use_associated_consts, .. } => {
if use_associated_consts {
let enum_ident = ctx.rust_ident(canonical_name);
let variant_ident = ctx.rust_ident(variant_name);
result.push(quote! {
impl #enum_ident {
#doc
pub const #variant_ident : #rust_ty = #rust_ty ( #expr );
Copy link
Contributor

Choose a reason for hiding this comment

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

This is probably even not worth the option, and just worth guarding it after a feature guard, wdyt? see src/features.rs.

}
});
} else {
let ident = ctx.rust_ident(match mangling_prefix {
Some(prefix) => {
Cow::Owned(format!("{}_{}", prefix, variant_name))
}
None => variant_name,
});
result.push(quote! {
#doc
pub const #ident : #rust_ty = #rust_ty ( #expr );
});
}

self
}
Expand Down Expand Up @@ -2482,7 +2493,9 @@ impl CodeGenerator for Enum {
let variation = if self.is_constified_enum_module(ctx, item) {
EnumVariation::ModuleConsts
} else if self.is_bitfield(ctx, item) {
EnumVariation::Bitfield
EnumVariation::Bitfield {
use_associated_consts: ctx.options().use_associated_consts
}
} else if self.is_rustified_enum(ctx, item) {
EnumVariation::Rust
} else {
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,12 @@ impl Builder {
self.options.no_hash_types.insert(arg.into());
self
}

/// Use associated constants for bitfields.
pub fn use_associated_consts(mut self, doIt: bool) -> Builder {
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably deserves a bit more specific name. And please use snake_case for the argument name.

self.options.use_associated_consts = doIt;
self
}
}

/// Configuration options for generated bindings.
Expand Down Expand Up @@ -1431,6 +1437,9 @@ struct BindgenOptions {

/// The set of types that we should not derive `Hash` for.
no_hash_types: RegexSet,

/// Whether to generated associated constants for bitfields.
use_associated_consts: bool,
}

/// TODO(emilio): This is sort of a lie (see the error message that results from
Expand Down Expand Up @@ -1525,6 +1534,7 @@ impl Default for BindgenOptions {
no_partialeq_types: Default::default(),
no_copy_types: Default::default(),
no_hash_types: Default::default(),
use_associated_consts: false,
}
}
}
Expand Down