Skip to content

Commit 098b416

Browse files
VeaaCemilio
authored andcommitted
Use annotations on enumerations: This enables users to add additional derives, or prevent deriving traits
Fixes #2076
1 parent 0f64106 commit 098b416

File tree

7 files changed

+76
-3
lines changed

7 files changed

+76
-3
lines changed

src/codegen/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,16 +3010,26 @@ impl CodeGenerator for Enum {
30103010

30113011
if !variation.is_const() {
30123012
let mut derives = derives_of_item(item, ctx);
3013-
// For backwards compat, enums always derive Clone/Eq/PartialEq/Hash, even
3013+
// For backwards compat, enums always derive Debug/Clone/Eq/PartialEq/Hash, even
30143014
// if we don't generate those by default.
3015+
if !item.annotations().disallow_debug() {
3016+
derives.insert(DerivableTraits::DEBUG);
3017+
}
3018+
if !item.annotations().disallow_copy() {
3019+
derives.insert(DerivableTraits::COPY);
3020+
}
30153021
derives.insert(
30163022
DerivableTraits::CLONE |
3017-
DerivableTraits::COPY |
30183023
DerivableTraits::HASH |
30193024
DerivableTraits::PARTIAL_EQ |
30203025
DerivableTraits::EQ,
30213026
);
3022-
let derives: Vec<_> = derives.into();
3027+
let mut derives: Vec<_> = derives.into();
3028+
for derive in item.annotations().derives().iter() {
3029+
if !derives.contains(&derive.as_str()) {
3030+
derives.push(&derive);
3031+
}
3032+
}
30233033
attrs.push(attributes::derives(&derives));
30243034
}
30253035

tests/expectations/tests/enum-default-bitfield.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,39 @@ impl ::std::ops::BitAndAssign for NoDebug {
149149
/// <div rustbindgen nodebug></div>
150150
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
151151
pub struct NoDebug(pub ::std::os::raw::c_uint);
152+
impl Debug {
153+
pub const Debug1: Debug = Debug(0);
154+
}
155+
impl Debug {
156+
pub const Debug2: Debug = Debug(1);
157+
}
158+
impl ::std::ops::BitOr<Debug> for Debug {
159+
type Output = Self;
160+
#[inline]
161+
fn bitor(self, other: Self) -> Self {
162+
Debug(self.0 | other.0)
163+
}
164+
}
165+
impl ::std::ops::BitOrAssign for Debug {
166+
#[inline]
167+
fn bitor_assign(&mut self, rhs: Debug) {
168+
self.0 |= rhs.0;
169+
}
170+
}
171+
impl ::std::ops::BitAnd<Debug> for Debug {
172+
type Output = Self;
173+
#[inline]
174+
fn bitand(self, other: Self) -> Self {
175+
Debug(self.0 & other.0)
176+
}
177+
}
178+
impl ::std::ops::BitAndAssign for Debug {
179+
#[inline]
180+
fn bitand_assign(&mut self, rhs: Debug) {
181+
self.0 &= rhs.0;
182+
}
183+
}
184+
#[repr(transparent)]
185+
/// <div rustbindgen derive="Debug"></div>
186+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
187+
pub struct Debug(pub ::std::os::raw::c_uint);

tests/expectations/tests/enum-default-consts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ pub const NoDebug_NoDebug1: NoDebug = 0;
5757
pub const NoDebug_NoDebug2: NoDebug = 1;
5858
/// <div rustbindgen nodebug></div>
5959
pub type NoDebug = ::std::os::raw::c_uint;
60+
pub const Debug_Debug1: Debug = 0;
61+
pub const Debug_Debug2: Debug = 1;
62+
/// <div rustbindgen derive="Debug"></div>
63+
pub type Debug = ::std::os::raw::c_uint;

tests/expectations/tests/enum-default-module.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ pub mod NoDebug {
6363
pub const NoDebug1: Type = 0;
6464
pub const NoDebug2: Type = 1;
6565
}
66+
pub mod Debug {
67+
/// <div rustbindgen derive="Debug"></div>
68+
pub type Type = ::std::os::raw::c_uint;
69+
pub const Debug1: Type = 0;
70+
pub const Debug2: Type = 1;
71+
}

tests/expectations/tests/enum-default-rust.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ pub enum NoDebug {
6868
NoDebug1 = 0,
6969
NoDebug2 = 1,
7070
}
71+
#[repr(u32)]
72+
/// <div rustbindgen derive="Debug"></div>
73+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
74+
pub enum Debug {
75+
Debug1 = 0,
76+
Debug2 = 1,
77+
}

tests/expectations/tests/enum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ pub const NoDebug_NoDebug1: NoDebug = 0;
5555
pub const NoDebug_NoDebug2: NoDebug = 1;
5656
/// <div rustbindgen nodebug></div>
5757
pub type NoDebug = ::std::os::raw::c_uint;
58+
pub const Debug_Debug1: Debug = 0;
59+
pub const Debug_Debug2: Debug = 1;
60+
/// <div rustbindgen derive="Debug"></div>
61+
pub type Debug = ::std::os::raw::c_uint;

tests/headers/enum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ enum NoDebug {
2323
NoDebug1,
2424
NoDebug2,
2525
};
26+
27+
/** <div rustbindgen derive="Debug"></div> */
28+
enum Debug {
29+
Debug1,
30+
Debug2,
31+
};

0 commit comments

Comments
 (0)