summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2018-07-30 19:23:36 +1000
committerGitHub <noreply@github.com>2018-07-30 19:23:36 +1000
commitcb8bfea157fb6e493ac508af73c9f153726cd9f7 (patch)
tree0fec2798d5bccceb8495d72b36064d054a79afcb
parent29dad405165756be9ce8da659a76c4cfa331a01e (diff)
parent4b5f86fb4c3a7ef1adb9cd9befb527dd18051066 (diff)
Merge pull request #1355 from Lytigas/associated-duplicate-enum. r=emilio
Use associated constants for rust enums when available
-rw-r--r--src/codegen/mod.rs34
-rw-r--r--tests/expectations/tests/anon_enum_trait.rs7
-rw-r--r--tests/expectations/tests/anon_union.rs12
-rw-r--r--tests/expectations/tests/constify-enum.rs12
-rw-r--r--tests/expectations/tests/enum_dupe.rs11
-rw-r--r--tests/expectations/tests/enum_in_template_with_typedef.rs12
-rw-r--r--tests/expectations/tests/issue-1198-alias-rust-enum.rs15
-rw-r--r--tests/expectations/tests/prepend-enum-constified-variant.rs11
8 files changed, 88 insertions, 26 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 2a0891d9..6e80447f 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -2645,15 +2645,31 @@ impl CodeGenerator for Enum {
};
let existing_variant_name = entry.get();
- add_constant(
- ctx,
- enum_ty,
- &ident,
- &*mangled_name,
- existing_variant_name,
- enum_rust_ty.clone(),
- result,
- );
+ // Use associated constants for named enums
+ if enum_ty.name().is_some() &&
+ ctx.options().rust_features().associated_const {
+ let enum_rust_ty_ = enum_rust_ty.clone();
+ let enum_canonical_name = &ident;
+ let variant_name = &*mangled_name;
+ let constant_name: String = variant_name.into();
+ let constant_name = ctx.rust_ident(constant_name);
+ result.push(quote! {
+ impl #enum_rust_ty_ {
+ pub const #constant_name : #enum_rust_ty_ =
+ #enum_canonical_name :: #existing_variant_name ;
+ }
+ });
+ } else {
+ add_constant(
+ ctx,
+ enum_ty,
+ &ident,
+ &*mangled_name,
+ existing_variant_name,
+ enum_rust_ty.clone(),
+ result,
+ );
+ }
} else {
builder = builder.with_variant(
ctx,
diff --git a/tests/expectations/tests/anon_enum_trait.rs b/tests/expectations/tests/anon_enum_trait.rs
index 6eb9f440..e109c90d 100644
--- a/tests/expectations/tests/anon_enum_trait.rs
+++ b/tests/expectations/tests/anon_enum_trait.rs
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */
-#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
diff --git a/tests/expectations/tests/anon_union.rs b/tests/expectations/tests/anon_union.rs
index 446cd589..a9b60292 100644
--- a/tests/expectations/tests/anon_union.rs
+++ b/tests/expectations/tests/anon_union.rs
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */
-#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
#[repr(C)]
pub struct TErrorResult {
@@ -9,8 +14,9 @@ pub struct TErrorResult {
pub mMightHaveUnreported: bool,
pub mUnionState: TErrorResult_UnionState,
}
-pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState =
- TErrorResult_UnionState::HasMessage;
+impl TErrorResult_UnionState {
+ pub const HasException: TErrorResult_UnionState = TErrorResult_UnionState::HasMessage;
+}
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum TErrorResult_UnionState {
diff --git a/tests/expectations/tests/constify-enum.rs b/tests/expectations/tests/constify-enum.rs
index 07279cf3..a1744a2a 100644
--- a/tests/expectations/tests/constify-enum.rs
+++ b/tests/expectations/tests/constify-enum.rs
@@ -1,11 +1,17 @@
/* automatically generated by rust-bindgen */
-#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
pub const nsCSSPropertyID_eCSSProperty_COUNT_unexistingVariantValue: nsCSSPropertyID =
nsCSSPropertyID::eCSSProperty_COUNT_unexistingVariantValue;
-pub const nsCSSPropertyID_eCSSProperty_COUNT: nsCSSPropertyID =
- nsCSSPropertyID::eCSSPropertyAlias_aa;
+impl nsCSSPropertyID {
+ pub const eCSSProperty_COUNT: nsCSSPropertyID = nsCSSPropertyID::eCSSPropertyAlias_aa;
+}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum nsCSSPropertyID {
diff --git a/tests/expectations/tests/enum_dupe.rs b/tests/expectations/tests/enum_dupe.rs
index 29bd7502..8462f6d2 100644
--- a/tests/expectations/tests/enum_dupe.rs
+++ b/tests/expectations/tests/enum_dupe.rs
@@ -1,8 +1,15 @@
/* automatically generated by rust-bindgen */
-#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
-pub const Foo_Dupe: Foo = Foo::Bar;
+impl Foo {
+ pub const Dupe: Foo = Foo::Bar;
+}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Foo {
diff --git a/tests/expectations/tests/enum_in_template_with_typedef.rs b/tests/expectations/tests/enum_in_template_with_typedef.rs
index a3b4e776..91c22fcd 100644
--- a/tests/expectations/tests/enum_in_template_with_typedef.rs
+++ b/tests/expectations/tests/enum_in_template_with_typedef.rs
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */
-#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
@@ -8,8 +13,9 @@ pub struct std_fbstring_core {
pub _address: u8,
}
pub type std_fbstring_core_category_type = u8;
-pub const std_fbstring_core_Category_Bar: std_fbstring_core_Category =
- std_fbstring_core_Category::Foo;
+impl std_fbstring_core_Category {
+ pub const Bar: std_fbstring_core_Category = std_fbstring_core_Category::Foo;
+}
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum std_fbstring_core_Category {
diff --git a/tests/expectations/tests/issue-1198-alias-rust-enum.rs b/tests/expectations/tests/issue-1198-alias-rust-enum.rs
index b2902c89..1659e29e 100644
--- a/tests/expectations/tests/issue-1198-alias-rust-enum.rs
+++ b/tests/expectations/tests/issue-1198-alias-rust-enum.rs
@@ -1,15 +1,24 @@
/* automatically generated by rust-bindgen */
-#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
-pub const MyDupeEnum_A_alias: MyDupeEnum = MyDupeEnum::A;
+impl MyDupeEnum {
+ pub const 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;
+impl MyOtherDupeEnum {
+ pub const C_alias: MyOtherDupeEnum = MyOtherDupeEnum::C;
+}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MyOtherDupeEnum {
diff --git a/tests/expectations/tests/prepend-enum-constified-variant.rs b/tests/expectations/tests/prepend-enum-constified-variant.rs
index 7f3b6746..d0323e12 100644
--- a/tests/expectations/tests/prepend-enum-constified-variant.rs
+++ b/tests/expectations/tests/prepend-enum-constified-variant.rs
@@ -1,8 +1,15 @@
/* automatically generated by rust-bindgen */
-#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
-pub const AV_CODEC_ID_TTF: AVCodecID = AVCodecID::AV_CODEC_ID_FIRST_UNKNOWN;
+impl AVCodecID {
+ pub const AV_CODEC_ID_TTF: AVCodecID = AVCodecID::AV_CODEC_ID_FIRST_UNKNOWN;
+}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum AVCodecID {