Skip to content
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

Add support for non_exhaustive rustified enums. #1575

Merged
merged 6 commits into from Jun 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/codegen/mod.rs
Expand Up @@ -2170,10 +2170,10 @@ impl MethodCodegen for Method {
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum EnumVariation {
/// The code for this enum will use a Rust enum
///
/// When the boolean parameter on this variant is set to true,
/// the generated enum should be non_exhaustive.
Rust(bool),
Rust {
/// Indicates whether the generated struct should be #[non_exhaustive]
non_exhaustive: bool
},
/// The code for this enum will use a bitfield
Bitfield,
/// The code for this enum will use consts
Expand All @@ -2185,7 +2185,7 @@ pub enum EnumVariation {
impl EnumVariation {
fn is_rust(&self) -> bool {
match *self {
EnumVariation::Rust(_) => true,
EnumVariation::Rust{ non_exhaustive: _ } => true,
agodnic marked this conversation as resolved.
Show resolved Hide resolved
_ => false
}
}
Expand All @@ -2212,8 +2212,8 @@ impl std::str::FromStr for EnumVariation {
/// Create a `EnumVariation` from a string.
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"rust" => Ok(EnumVariation::Rust(false)),
"rust_non_exhaustive" => Ok(EnumVariation::Rust(true)),
"rust" => Ok(EnumVariation::Rust{ non_exhaustive: false }),
"rust_non_exhaustive" => Ok(EnumVariation::Rust{ non_exhaustive: true }),
"bitfield" => Ok(EnumVariation::Bitfield),
"consts" => Ok(EnumVariation::Consts),
"moduleconsts" => Ok(EnumVariation::ModuleConsts),
Expand Down Expand Up @@ -2285,7 +2285,7 @@ impl<'a> EnumBuilder<'a> {
}
}

EnumVariation::Rust(_) => {
EnumVariation::Rust { non_exhaustive: _ } => {
agodnic marked this conversation as resolved.
Show resolved Hide resolved
let tokens = quote!();
EnumBuilder::Rust {
codegen_depth: enum_codegen_depth + 1,
Expand Down Expand Up @@ -2578,9 +2578,9 @@ impl CodeGenerator for Enum {

// TODO(emilio): Delegate this to the builders?
match variation {
EnumVariation::Rust(non_exhaustive) => {
EnumVariation::Rust { non_exhaustive: nh } => {
agodnic marked this conversation as resolved.
Show resolved Hide resolved
attrs.push(attributes::repr(repr_name));
if non_exhaustive {
if nh {
attrs.push(attributes::non_exhaustive());
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/ir/enum_ty.rs
Expand Up @@ -164,9 +164,9 @@ impl Enum {
} else if self.is_matching_enum(ctx, &ctx.options().bitfield_enums, item) {
EnumVariation::Bitfield
} else if self.is_matching_enum(ctx, &ctx.options().rustified_enums, item) {
EnumVariation::Rust(false)
} else if self.is_matching_enum(ctx, &ctx.options().rustified_enums_non_exhaustive, item) {
EnumVariation::Rust(true)
EnumVariation::Rust { non_exhaustive: false }
} else if self.is_matching_enum(ctx, &ctx.options().rustified_non_exhaustive_enums, item) {
EnumVariation::Rust { non_exhaustive: true }
} else if self.is_matching_enum(ctx, &ctx.options().constified_enums, item) {
EnumVariation::Consts
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Expand Up @@ -226,8 +226,8 @@ impl Builder {
if self.options.default_enum_style != Default::default() {
output_vector.push("--default-enum-style=".into());
output_vector.push(match self.options.default_enum_style {
codegen::EnumVariation::Rust(false) => "rust",
codegen::EnumVariation::Rust(true) => "rust_non_exhaustive",
codegen::EnumVariation::Rust { non_exhaustive: false } => "rust",
codegen::EnumVariation::Rust { non_exhaustive: true } => "rust_non_exhaustive",
codegen::EnumVariation::Bitfield => "bitfield",
codegen::EnumVariation::Consts => "consts",
codegen::EnumVariation::ModuleConsts => "moduleconsts",
Expand Down Expand Up @@ -255,7 +255,7 @@ impl Builder {
.count();

self.options
.rustified_enums_non_exhaustive
.rustified_non_exhaustive_enums
.get_items()
.iter()
.map(|item| {
Expand Down Expand Up @@ -834,8 +834,8 @@ impl Builder {
///
/// This makes bindgen generate enums instead of constants. Regular
/// expressions are supported.
pub fn rustified_enum_non_exhaustive<T: AsRef<str>>(mut self, arg: T) -> Builder {
self.options.rustified_enums_non_exhaustive.insert(arg);
pub fn rustified_non_exhaustive_enum<T: AsRef<str>>(mut self, arg: T) -> Builder {
self.options.rustified_non_exhaustive_enums.insert(arg);
self
}

Expand Down Expand Up @@ -1387,7 +1387,7 @@ struct BindgenOptions {
/// The enum patterns to mark an enum as a Rust enum.
rustified_enums: RegexSet,

rustified_enums_non_exhaustive: RegexSet,
rustified_non_exhaustive_enums: RegexSet,

/// The enum patterns to mark an enum as a module of constants.
constified_enum_modules: RegexSet,
Expand Down Expand Up @@ -1642,7 +1642,7 @@ impl Default for BindgenOptions {
default_enum_style: Default::default(),
bitfield_enums: Default::default(),
rustified_enums: Default::default(),
rustified_enums_non_exhaustive: Default::default(),
rustified_non_exhaustive_enums: Default::default(),
constified_enums: Default::default(),
constified_enum_modules: Default::default(),
builtins: false,
Expand Down