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

Use common type alias for anonymous enums in consts mode #2191

Merged
merged 1 commit into from Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 2 additions & 6 deletions src/codegen/mod.rs
Expand Up @@ -2625,7 +2625,6 @@ enum EnumBuilder<'a> {
is_bitfield: bool,
},
Consts {
repr: proc_macro2::TokenStream,
variants: Vec<proc_macro2::TokenStream>,
codegen_depth: usize,
},
Expand Down Expand Up @@ -2696,7 +2695,6 @@ impl<'a> EnumBuilder<'a> {
});

EnumBuilder::Consts {
repr,
variants,
codegen_depth: enum_codegen_depth,
}
Expand Down Expand Up @@ -2800,20 +2798,18 @@ impl<'a> EnumBuilder<'a> {
self
}

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

let ty = if is_ty_named { &rust_ty } else { repr };

let ident = ctx.rust_ident(constant_name);
result.push(quote! {
#doc
pub const #ident : #ty = #expr ;
pub const #ident : #rust_ty = #expr ;
});

self
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/enum-default-consts.rs
Expand Up @@ -10,8 +10,8 @@
pub struct foo {
pub member: foo__bindgen_ty_1,
}
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
pub const foo_FOO_A: foo__bindgen_ty_1 = 0;
pub const foo_FOO_B: foo__bindgen_ty_1 = 1;
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
#[test]
fn bindgen_test_layout_foo() {
Expand Down Expand Up @@ -60,4 +60,4 @@ pub type NoDebug = ::std::os::raw::c_uint;
pub const Debug_Debug1: Debug = 0;
pub const Debug_Debug2: Debug = 1;
/// <div rustbindgen derive="Debug"></div>
pub type Debug = ::std::os::raw::c_uint;
pub type Debug = ::std::os::raw::c_uint;
6 changes: 3 additions & 3 deletions tests/expectations/tests/enum.rs
Expand Up @@ -10,8 +10,8 @@
pub struct foo {
pub member: foo__bindgen_ty_1,
}
pub const foo_FOO_A: ::std::os::raw::c_uint = 0;
pub const foo_FOO_B: ::std::os::raw::c_uint = 1;
pub const foo_FOO_A: foo__bindgen_ty_1 = 0;
pub const foo_FOO_B: foo__bindgen_ty_1 = 1;
pub type foo__bindgen_ty_1 = ::std::os::raw::c_uint;
#[test]
fn bindgen_test_layout_foo() {
Expand Down Expand Up @@ -58,4 +58,4 @@ pub type NoDebug = ::std::os::raw::c_uint;
pub const Debug_Debug1: Debug = 0;
pub const Debug_Debug2: Debug = 1;
/// <div rustbindgen derive="Debug"></div>
pub type Debug = ::std::os::raw::c_uint;
pub type Debug = ::std::os::raw::c_uint;
4 changes: 2 additions & 2 deletions tests/expectations/tests/enum_explicit_type_constants.rs
Expand Up @@ -26,6 +26,6 @@ pub type BoolEnumsAreFun = bool;
pub type MyType = bool;
pub const BoolEnumsAreFun2_Value2: BoolEnumsAreFun2 = true;
pub type BoolEnumsAreFun2 = MyType;
pub const AnonymousVariantOne: ::std::os::raw::c_uchar = 0;
pub const AnonymousVariantTwo: ::std::os::raw::c_uchar = 1;
pub const AnonymousVariantOne: _bindgen_ty_1 = 0;
pub const AnonymousVariantTwo: _bindgen_ty_1 = 1;
pub type _bindgen_ty_1 = ::std::os::raw::c_uchar;
Expand Up @@ -5,5 +5,5 @@
non_upper_case_globals
)]

pub const RENAMED_MyVal: ::std::os::raw::c_uint = 0;
pub const RENAMED_MyVal: _bindgen_ty_1 = 0;
pub type _bindgen_ty_1 = ::std::os::raw::c_uint;