@@ -110,17 +110,13 @@ bitflags! {
110
110
}
111
111
}
112
112
113
- fn derives_of_item ( item : & Item , ctx : & BindgenContext ) -> DerivableTraits {
113
+ fn derives_of_item (
114
+ item : & Item ,
115
+ ctx : & BindgenContext ,
116
+ packed : bool ,
117
+ ) -> DerivableTraits {
114
118
let mut derivable_traits = DerivableTraits :: empty ( ) ;
115
119
116
- if item. can_derive_debug ( ctx) && !item. annotations ( ) . disallow_debug ( ) {
117
- derivable_traits |= DerivableTraits :: DEBUG ;
118
- }
119
-
120
- if item. can_derive_default ( ctx) && !item. annotations ( ) . disallow_default ( ) {
121
- derivable_traits |= DerivableTraits :: DEFAULT ;
122
- }
123
-
124
120
let all_template_params = item. all_template_params ( ctx) ;
125
121
126
122
if item. can_derive_copy ( ctx) && !item. annotations ( ) . disallow_copy ( ) {
@@ -137,6 +133,18 @@ fn derives_of_item(item: &Item, ctx: &BindgenContext) -> DerivableTraits {
137
133
// It's not hard to fix though.
138
134
derivable_traits |= DerivableTraits :: CLONE ;
139
135
}
136
+ } else if packed {
137
+ // If the struct or union is packed, deriving from Copy is required for
138
+ // deriving from any other trait.
139
+ return derivable_traits;
140
+ }
141
+
142
+ if item. can_derive_debug ( ctx) && !item. annotations ( ) . disallow_debug ( ) {
143
+ derivable_traits |= DerivableTraits :: DEBUG ;
144
+ }
145
+
146
+ if item. can_derive_default ( ctx) && !item. annotations ( ) . disallow_default ( ) {
147
+ derivable_traits |= DerivableTraits :: DEFAULT ;
140
148
}
141
149
142
150
if item. can_derive_hash ( ctx) {
@@ -926,7 +934,9 @@ impl CodeGenerator for Type {
926
934
927
935
let mut attributes =
928
936
vec ! [ attributes:: repr( "transparent" ) ] ;
929
- let derivable_traits = derives_of_item ( item, ctx) ;
937
+ let packed = false ; // Types can't be packed in Rust.
938
+ let derivable_traits =
939
+ derives_of_item ( item, ctx, packed) ;
930
940
if !derivable_traits. is_empty ( ) {
931
941
let derives: Vec < _ > = derivable_traits. into ( ) ;
932
942
attributes. push ( attributes:: derives ( & derives) )
@@ -2026,7 +2036,7 @@ impl CodeGenerator for CompInfo {
2026
2036
}
2027
2037
}
2028
2038
2029
- let derivable_traits = derives_of_item ( item, ctx) ;
2039
+ let derivable_traits = derives_of_item ( item, ctx, packed ) ;
2030
2040
if !derivable_traits. contains ( DerivableTraits :: DEBUG ) {
2031
2041
needs_debug_impl = ctx. options ( ) . derive_debug &&
2032
2042
ctx. options ( ) . impl_debug &&
@@ -3048,7 +3058,8 @@ impl CodeGenerator for Enum {
3048
3058
}
3049
3059
3050
3060
if !variation. is_const ( ) {
3051
- let mut derives = derives_of_item ( item, ctx) ;
3061
+ let packed = false ; // Enums can't be packed in Rust.
3062
+ let mut derives = derives_of_item ( item, ctx, packed) ;
3052
3063
// For backwards compat, enums always derive
3053
3064
// Clone/Eq/PartialEq/Hash, even if we don't generate those by
3054
3065
// default.
0 commit comments