@@ -110,17 +110,9 @@ bitflags! {
110
110
}
111
111
}
112
112
113
- fn derives_of_item ( item : & Item , ctx : & BindgenContext ) -> DerivableTraits {
113
+ fn derives_of_item ( item : & Item , ctx : & BindgenContext , packed : Option < bool > ) -> DerivableTraits {
114
114
let mut derivable_traits = DerivableTraits :: empty ( ) ;
115
115
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
116
let all_template_params = item. all_template_params ( ctx) ;
125
117
126
118
if item. can_derive_copy ( ctx) && !item. annotations ( ) . disallow_copy ( ) {
@@ -137,6 +129,20 @@ fn derives_of_item(item: &Item, ctx: &BindgenContext) -> DerivableTraits {
137
129
// It's not hard to fix though.
138
130
derivable_traits |= DerivableTraits :: CLONE ;
139
131
}
132
+ } else if let Some ( packed) = packed {
133
+ // If the struct or union is packed, deriving from Copy is required for
134
+ // deriving from any other trait.
135
+ if packed {
136
+ return derivable_traits;
137
+ }
138
+ }
139
+
140
+ if item. can_derive_debug ( ctx) && !item. annotations ( ) . disallow_debug ( ) {
141
+ derivable_traits |= DerivableTraits :: DEBUG ;
142
+ }
143
+
144
+ if item. can_derive_default ( ctx) && !item. annotations ( ) . disallow_default ( ) {
145
+ derivable_traits |= DerivableTraits :: DEFAULT ;
140
146
}
141
147
142
148
if item. can_derive_hash ( ctx) {
@@ -926,7 +932,7 @@ impl CodeGenerator for Type {
926
932
927
933
let mut attributes =
928
934
vec ! [ attributes:: repr( "transparent" ) ] ;
929
- let derivable_traits = derives_of_item ( item, ctx) ;
935
+ let derivable_traits = derives_of_item ( item, ctx, None ) ;
930
936
if !derivable_traits. is_empty ( ) {
931
937
let derives: Vec < _ > = derivable_traits. into ( ) ;
932
938
attributes. push ( attributes:: derives ( & derives) )
@@ -2026,7 +2032,7 @@ impl CodeGenerator for CompInfo {
2026
2032
}
2027
2033
}
2028
2034
2029
- let derivable_traits = derives_of_item ( item, ctx) ;
2035
+ let derivable_traits = derives_of_item ( item, ctx, Some ( packed ) ) ;
2030
2036
if !derivable_traits. contains ( DerivableTraits :: DEBUG ) {
2031
2037
needs_debug_impl = ctx. options ( ) . derive_debug &&
2032
2038
ctx. options ( ) . impl_debug &&
@@ -3048,7 +3054,7 @@ impl CodeGenerator for Enum {
3048
3054
}
3049
3055
3050
3056
if !variation. is_const ( ) {
3051
- let mut derives = derives_of_item ( item, ctx) ;
3057
+ let mut derives = derives_of_item ( item, ctx, None ) ;
3052
3058
// For backwards compat, enums always derive
3053
3059
// Clone/Eq/PartialEq/Hash, even if we don't generate those by
3054
3060
// default.
0 commit comments