diff options
-rw-r--r-- | src/codegen/mod.rs | 6 | ||||
-rw-r--r-- | src/lib.rs | 17 | ||||
-rw-r--r-- | tests/expectations/tests/issue-1198-alias-rust-const-mod-enum.rs | 16 | ||||
-rw-r--r-- | tests/expectations/tests/issue-1198-alias-rust-enum.rs | 18 | ||||
-rw-r--r-- | tests/headers/issue-1198-alias-rust-const-mod-enum.h | 13 | ||||
-rw-r--r-- | tests/headers/issue-1198-alias-rust-enum.h | 14 |
6 files changed, 82 insertions, 2 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index a6ab3997..cf1a4f3b 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2477,12 +2477,14 @@ impl CodeGenerator for Enum { } }; + // ModuleConsts has higher precedence before Rust in order to avoid problems with + // overlapping match patterns let variation = if self.is_bitfield(ctx, item) { EnumVariation::Bitfield - } else if self.is_rustified_enum(ctx, item) { - EnumVariation::Rust } else if self.is_constified_enum_module(ctx, item) { EnumVariation::ModuleConsts + } else if self.is_rustified_enum(ctx, item) { + EnumVariation::Rust } else { // We generate consts by default EnumVariation::Consts @@ -155,6 +155,23 @@ impl Default for CodegenConfig { /// // Write the generated bindings to an output file. /// bindings.write_to_file("path/to/output.rs")?; /// ``` +/// +/// # Enums +/// +/// Bindgen can map C/C++ enums into Rust in different ways. The way bindgen maps enums depends on +/// the pattern passed to several methods: +/// +/// 1. [`bitfield_enum()`](#method.bitfield_enum) +/// 2. [`constified_enum_module()`](#method.constified_enum_module) +/// 3. [`rustified_enum()`](#method.rustified_enum) +/// +/// For each C enum, bindgen tries to match the pattern in the following order: +/// +/// 1. Bitfield enum +/// 2. Constified enum module +/// 3. Rustified enum +/// +/// If none of the above patterns match, then bindgen will generate a set of Rust constants. #[derive(Debug, Default)] pub struct Builder { options: BindgenOptions, diff --git a/tests/expectations/tests/issue-1198-alias-rust-const-mod-enum.rs b/tests/expectations/tests/issue-1198-alias-rust-const-mod-enum.rs new file mode 100644 index 00000000..ed53fdf9 --- /dev/null +++ b/tests/expectations/tests/issue-1198-alias-rust-const-mod-enum.rs @@ -0,0 +1,16 @@ +/* automatically generated by rust-bindgen */ + +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + +pub mod MyDupeEnum { + pub type Type = u32; + pub const A: Type = 0; + pub const A_alias: Type = 0; + pub const B: Type = 1; +} +pub mod MyOtherDupeEnum { + pub type Type = u32; + pub const C: Type = 0; + pub const C_alias: Type = 0; + pub const D: Type = 1; +} diff --git a/tests/expectations/tests/issue-1198-alias-rust-enum.rs b/tests/expectations/tests/issue-1198-alias-rust-enum.rs new file mode 100644 index 00000000..b2902c89 --- /dev/null +++ b/tests/expectations/tests/issue-1198-alias-rust-enum.rs @@ -0,0 +1,18 @@ +/* automatically generated by rust-bindgen */ + +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + +pub const MyDupeEnum_A_alias: MyDupeEnum = MyDupeEnum::A; +#[repr(u32)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum MyDupeEnum { + A = 0, + B = 1, +} +pub const MyOtherDupeEnum_C_alias: MyOtherDupeEnum = MyOtherDupeEnum::C; +#[repr(u32)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub enum MyOtherDupeEnum { + C = 0, + D = 1, +} diff --git a/tests/headers/issue-1198-alias-rust-const-mod-enum.h b/tests/headers/issue-1198-alias-rust-const-mod-enum.h new file mode 100644 index 00000000..944fac31 --- /dev/null +++ b/tests/headers/issue-1198-alias-rust-const-mod-enum.h @@ -0,0 +1,13 @@ +// bindgen-flags: --rustified-enum '.*' --constified-enum-module '.*' + +typedef enum MyDupeEnum { + A = 0, + A_alias = 0, + B, +} MyDupeEnum; + +enum MyOtherDupeEnum { + C = 0, + C_alias = 0, + D, +};
\ No newline at end of file diff --git a/tests/headers/issue-1198-alias-rust-enum.h b/tests/headers/issue-1198-alias-rust-enum.h new file mode 100644 index 00000000..ede44f0a --- /dev/null +++ b/tests/headers/issue-1198-alias-rust-enum.h @@ -0,0 +1,14 @@ +// bindgen-flags: --rustified-enum '.*' + + +typedef enum MyDupeEnum { + A = 0, + A_alias = 0, + B, +} MyDupeEnum; + +enum MyOtherDupeEnum { + C = 0, + C_alias = 0, + D, +};
\ No newline at end of file |