diff options
author | Christian Vetter <christian.vetter@here.com> | 2021-08-03 09:03:46 +0200 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2021-08-24 11:49:26 +0200 |
commit | 098b416283610b3adbb5a51325644a8a66a4bfd2 (patch) | |
tree | 5469f5951561cd9fba12d04e4253e5827ac0d1ad | |
parent | 0f641061a78b8af93e977e072cfd8106461b3ca5 (diff) |
Use annotations on enumerations: This enables users to add additional derives, or prevent deriving traits
Fixes #2076
-rw-r--r-- | src/codegen/mod.rs | 16 | ||||
-rw-r--r-- | tests/expectations/tests/enum-default-bitfield.rs | 36 | ||||
-rw-r--r-- | tests/expectations/tests/enum-default-consts.rs | 4 | ||||
-rw-r--r-- | tests/expectations/tests/enum-default-module.rs | 6 | ||||
-rw-r--r-- | tests/expectations/tests/enum-default-rust.rs | 7 | ||||
-rw-r--r-- | tests/expectations/tests/enum.rs | 4 | ||||
-rw-r--r-- | tests/headers/enum.h | 6 |
7 files changed, 76 insertions, 3 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index b823fb34..8858377f 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -3010,16 +3010,26 @@ impl CodeGenerator for Enum { if !variation.is_const() { let mut derives = derives_of_item(item, ctx); - // For backwards compat, enums always derive Clone/Eq/PartialEq/Hash, even + // For backwards compat, enums always derive Debug/Clone/Eq/PartialEq/Hash, even // if we don't generate those by default. + if !item.annotations().disallow_debug() { + derives.insert(DerivableTraits::DEBUG); + } + if !item.annotations().disallow_copy() { + derives.insert(DerivableTraits::COPY); + } derives.insert( DerivableTraits::CLONE | - DerivableTraits::COPY | DerivableTraits::HASH | DerivableTraits::PARTIAL_EQ | DerivableTraits::EQ, ); - let derives: Vec<_> = derives.into(); + let mut derives: Vec<_> = derives.into(); + for derive in item.annotations().derives().iter() { + if !derives.contains(&derive.as_str()) { + derives.push(&derive); + } + } attrs.push(attributes::derives(&derives)); } diff --git a/tests/expectations/tests/enum-default-bitfield.rs b/tests/expectations/tests/enum-default-bitfield.rs index 1520bea7..1dc27fdc 100644 --- a/tests/expectations/tests/enum-default-bitfield.rs +++ b/tests/expectations/tests/enum-default-bitfield.rs @@ -149,3 +149,39 @@ impl ::std::ops::BitAndAssign for NoDebug { /// <div rustbindgen nodebug></div> #[derive(Copy, Clone, Hash, PartialEq, Eq)] pub struct NoDebug(pub ::std::os::raw::c_uint); +impl Debug { + pub const Debug1: Debug = Debug(0); +} +impl Debug { + pub const Debug2: Debug = Debug(1); +} +impl ::std::ops::BitOr<Debug> for Debug { + type Output = Self; + #[inline] + fn bitor(self, other: Self) -> Self { + Debug(self.0 | other.0) + } +} +impl ::std::ops::BitOrAssign for Debug { + #[inline] + fn bitor_assign(&mut self, rhs: Debug) { + self.0 |= rhs.0; + } +} +impl ::std::ops::BitAnd<Debug> for Debug { + type Output = Self; + #[inline] + fn bitand(self, other: Self) -> Self { + Debug(self.0 & other.0) + } +} +impl ::std::ops::BitAndAssign for Debug { + #[inline] + fn bitand_assign(&mut self, rhs: Debug) { + self.0 &= rhs.0; + } +} +#[repr(transparent)] +/// <div rustbindgen derive="Debug"></div> +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub struct Debug(pub ::std::os::raw::c_uint);
\ No newline at end of file diff --git a/tests/expectations/tests/enum-default-consts.rs b/tests/expectations/tests/enum-default-consts.rs index 1a9513d9..5c023a3c 100644 --- a/tests/expectations/tests/enum-default-consts.rs +++ b/tests/expectations/tests/enum-default-consts.rs @@ -57,3 +57,7 @@ pub const NoDebug_NoDebug1: NoDebug = 0; pub const NoDebug_NoDebug2: NoDebug = 1; /// <div rustbindgen nodebug></div> 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;
\ No newline at end of file diff --git a/tests/expectations/tests/enum-default-module.rs b/tests/expectations/tests/enum-default-module.rs index 73a0462e..156bbcaa 100644 --- a/tests/expectations/tests/enum-default-module.rs +++ b/tests/expectations/tests/enum-default-module.rs @@ -63,3 +63,9 @@ pub mod NoDebug { pub const NoDebug1: Type = 0; pub const NoDebug2: Type = 1; } +pub mod Debug { + /// <div rustbindgen derive="Debug"></div> + pub type Type = ::std::os::raw::c_uint; + pub const Debug1: Type = 0; + pub const Debug2: Type = 1; +}
\ No newline at end of file diff --git a/tests/expectations/tests/enum-default-rust.rs b/tests/expectations/tests/enum-default-rust.rs index c47521a2..045330a8 100644 --- a/tests/expectations/tests/enum-default-rust.rs +++ b/tests/expectations/tests/enum-default-rust.rs @@ -68,3 +68,10 @@ pub enum NoDebug { NoDebug1 = 0, NoDebug2 = 1, } +#[repr(u32)] +/// <div rustbindgen derive="Debug"></div> +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum Debug { + Debug1 = 0, + Debug2 = 1, +}
\ No newline at end of file diff --git a/tests/expectations/tests/enum.rs b/tests/expectations/tests/enum.rs index 67d263e9..cc3f4932 100644 --- a/tests/expectations/tests/enum.rs +++ b/tests/expectations/tests/enum.rs @@ -55,3 +55,7 @@ pub const NoDebug_NoDebug1: NoDebug = 0; pub const NoDebug_NoDebug2: NoDebug = 1; /// <div rustbindgen nodebug></div> 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;
\ No newline at end of file diff --git a/tests/headers/enum.h b/tests/headers/enum.h index 8b41f855..0147433e 100644 --- a/tests/headers/enum.h +++ b/tests/headers/enum.h @@ -23,3 +23,9 @@ enum NoDebug { NoDebug1, NoDebug2, }; + +/** <div rustbindgen derive="Debug"></div> */ +enum Debug { + Debug1, + Debug2, +};
\ No newline at end of file |