Skip to content

Commit cb8bfea

Browse files
authored
Merge pull request #1355 from Lytigas/associated-duplicate-enum. r=emilio
Use associated constants for rust enums when available
2 parents 29dad40 + 4b5f86f commit cb8bfea

8 files changed

+88
-26
lines changed

src/codegen/mod.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -2645,15 +2645,31 @@ impl CodeGenerator for Enum {
26452645
};
26462646

26472647
let existing_variant_name = entry.get();
2648-
add_constant(
2649-
ctx,
2650-
enum_ty,
2651-
&ident,
2652-
&*mangled_name,
2653-
existing_variant_name,
2654-
enum_rust_ty.clone(),
2655-
result,
2656-
);
2648+
// Use associated constants for named enums
2649+
if enum_ty.name().is_some() &&
2650+
ctx.options().rust_features().associated_const {
2651+
let enum_rust_ty_ = enum_rust_ty.clone();
2652+
let enum_canonical_name = &ident;
2653+
let variant_name = &*mangled_name;
2654+
let constant_name: String = variant_name.into();
2655+
let constant_name = ctx.rust_ident(constant_name);
2656+
result.push(quote! {
2657+
impl #enum_rust_ty_ {
2658+
pub const #constant_name : #enum_rust_ty_ =
2659+
#enum_canonical_name :: #existing_variant_name ;
2660+
}
2661+
});
2662+
} else {
2663+
add_constant(
2664+
ctx,
2665+
enum_ty,
2666+
&ident,
2667+
&*mangled_name,
2668+
existing_variant_name,
2669+
enum_rust_ty.clone(),
2670+
result,
2671+
);
2672+
}
26572673
} else {
26582674
builder = builder.with_variant(
26592675
ctx,

tests/expectations/tests/anon_enum_trait.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(C)]
611
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]

tests/expectations/tests/anon_union.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(C)]
611
pub struct TErrorResult {
@@ -9,8 +14,9 @@ pub struct TErrorResult {
914
pub mMightHaveUnreported: bool,
1015
pub mUnionState: TErrorResult_UnionState,
1116
}
12-
pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
13-
TErrorResult_UnionState::HasMessage;
17+
impl TErrorResult_UnionState {
18+
pub const HasException: TErrorResult_UnionState = TErrorResult_UnionState::HasMessage;
19+
}
1420
#[repr(i32)]
1521
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1622
pub enum TErrorResult_UnionState {

tests/expectations/tests/constify-enum.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
pub const nsCSSPropertyID_eCSSProperty_COUNT_unexistingVariantValue: nsCSSPropertyID =
611
nsCSSPropertyID::eCSSProperty_COUNT_unexistingVariantValue;
7-
pub const nsCSSPropertyID_eCSSProperty_COUNT: nsCSSPropertyID =
8-
nsCSSPropertyID::eCSSPropertyAlias_aa;
12+
impl nsCSSPropertyID {
13+
pub const eCSSProperty_COUNT: nsCSSPropertyID = nsCSSPropertyID::eCSSPropertyAlias_aa;
14+
}
915
#[repr(u32)]
1016
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1117
pub enum nsCSSPropertyID {

tests/expectations/tests/enum_dupe.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

5-
pub const Foo_Dupe: Foo = Foo::Bar;
10+
impl Foo {
11+
pub const Dupe: Foo = Foo::Bar;
12+
}
613
#[repr(u32)]
714
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
815
pub enum Foo {

tests/expectations/tests/enum_in_template_with_typedef.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(C)]
611
#[derive(Debug, Default, Copy, Clone)]
712
pub struct std_fbstring_core {
813
pub _address: u8,
914
}
1015
pub type std_fbstring_core_category_type = u8;
11-
pub const std_fbstring_core_Category_Bar: std_fbstring_core_Category =
12-
std_fbstring_core_Category::Foo;
16+
impl std_fbstring_core_Category {
17+
pub const Bar: std_fbstring_core_Category = std_fbstring_core_Category::Foo;
18+
}
1319
#[repr(u8)]
1420
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1521
pub enum std_fbstring_core_Category {

tests/expectations/tests/issue-1198-alias-rust-enum.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

5-
pub const MyDupeEnum_A_alias: MyDupeEnum = MyDupeEnum::A;
10+
impl MyDupeEnum {
11+
pub const A_alias: MyDupeEnum = MyDupeEnum::A;
12+
}
613
#[repr(u32)]
714
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
815
pub enum MyDupeEnum {
916
A = 0,
1017
B = 1,
1118
}
12-
pub const MyOtherDupeEnum_C_alias: MyOtherDupeEnum = MyOtherDupeEnum::C;
19+
impl MyOtherDupeEnum {
20+
pub const C_alias: MyOtherDupeEnum = MyOtherDupeEnum::C;
21+
}
1322
#[repr(u32)]
1423
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1524
pub enum MyOtherDupeEnum {

tests/expectations/tests/prepend-enum-constified-variant.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

5-
pub const AV_CODEC_ID_TTF: AVCodecID = AVCodecID::AV_CODEC_ID_FIRST_UNKNOWN;
10+
impl AVCodecID {
11+
pub const AV_CODEC_ID_TTF: AVCodecID = AVCodecID::AV_CODEC_ID_FIRST_UNKNOWN;
12+
}
613
#[repr(u32)]
714
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
815
pub enum AVCodecID {

0 commit comments

Comments
 (0)