@@ -6,6 +6,7 @@ use std::cell::Cell;
6
6
use std:: cmp;
7
7
use super :: annotations:: Annotations ;
8
8
use super :: context:: { BindgenContext , ItemId } ;
9
+ use super :: derive:: { CanDeriveCopy , CanDeriveDebug } ;
9
10
use super :: item:: Item ;
10
11
use super :: layout:: Layout ;
11
12
use super :: ty:: { RUST_DERIVE_IN_ARRAY_LIMIT , Type } ;
@@ -154,6 +155,26 @@ impl Field {
154
155
}
155
156
}
156
157
158
+ impl CanDeriveDebug for Field {
159
+ type Extra = ( ) ;
160
+
161
+ fn can_derive_debug ( & self , ctx : & BindgenContext , _: ( ) ) -> bool {
162
+ self . ty . can_derive_debug ( ctx, ( ) )
163
+ }
164
+ }
165
+
166
+ impl < ' a > CanDeriveCopy < ' a > for Field {
167
+ type Extra = ( ) ;
168
+
169
+ fn can_derive_copy ( & self , ctx : & BindgenContext , _: ( ) ) -> bool {
170
+ self . ty . can_derive_copy ( ctx, ( ) )
171
+ }
172
+
173
+ fn can_derive_copy_in_array ( & self , ctx : & BindgenContext , _: ( ) ) -> bool {
174
+ self . ty . can_derive_copy_in_array ( ctx, ( ) )
175
+ }
176
+ }
177
+
157
178
/// A compound type.
158
179
///
159
180
/// Either a struct or union, a compound type is built up from the combination
@@ -263,50 +284,6 @@ impl CompInfo {
263
284
}
264
285
}
265
286
266
- /// Can we derive the `Debug` trait for this compound type?
267
- pub fn can_derive_debug ( & self ,
268
- ctx : & BindgenContext ,
269
- layout : Option < Layout > )
270
- -> bool {
271
- // We can reach here recursively via template parameters of a member,
272
- // for example.
273
- if self . detect_derive_debug_cycle . get ( ) {
274
- warn ! ( "Derive debug cycle detected!" ) ;
275
- return true ;
276
- }
277
-
278
- if self . kind == CompKind :: Union {
279
- if ctx. options ( ) . unstable_rust {
280
- return false ;
281
- }
282
-
283
- let layout = layout. unwrap_or_else ( Layout :: zero) ;
284
- let size_divisor = cmp:: max ( 1 , layout. align ) ;
285
- return layout. size / size_divisor <= RUST_DERIVE_IN_ARRAY_LIMIT ;
286
- }
287
-
288
- self . detect_derive_debug_cycle . set ( true ) ;
289
-
290
- let can_derive_debug = {
291
- self . base_members
292
- . iter ( )
293
- . all ( |ty| ctx. resolve_type ( * ty) . can_derive_debug ( ctx) ) &&
294
- self . template_args
295
- . iter ( )
296
- . all ( |ty| ctx. resolve_type ( * ty) . can_derive_debug ( ctx) ) &&
297
- self . fields
298
- . iter ( )
299
- . all ( |f| ctx. resolve_type ( f. ty ) . can_derive_debug ( ctx) ) &&
300
- self . ref_template . map_or ( true , |template| {
301
- ctx. resolve_type ( template) . can_derive_debug ( ctx)
302
- } )
303
- } ;
304
-
305
- self . detect_derive_debug_cycle . set ( false ) ;
306
-
307
- can_derive_debug
308
- }
309
-
310
287
/// Is this compound type unsized?
311
288
pub fn is_unsized ( & self , ctx : & BindgenContext ) -> bool {
312
289
!self . has_vtable ( ctx) && self . fields . is_empty ( ) &&
@@ -356,41 +333,6 @@ impl CompInfo {
356
333
has_destructor
357
334
}
358
335
359
- /// Can we derive the `Copy` trait for this type?
360
- pub fn can_derive_copy ( & self , ctx : & BindgenContext , item : & Item ) -> bool {
361
- // NOTE: Take into account that while unions in C and C++ are copied by
362
- // default, the may have an explicit destructor in C++, so we can't
363
- // defer this check just for the union case.
364
- if self . has_destructor ( ctx) {
365
- return false ;
366
- }
367
-
368
- if self . kind == CompKind :: Union {
369
- if !ctx. options ( ) . unstable_rust {
370
- return true ;
371
- }
372
-
373
- // https://github.com/rust-lang/rust/issues/36640
374
- if !self . template_args . is_empty ( ) || self . ref_template . is_some ( ) ||
375
- !item. applicable_template_args ( ctx) . is_empty ( ) {
376
- return false ;
377
- }
378
- }
379
-
380
- // With template args, use a safe subset of the types,
381
- // since copyability depends on the types itself.
382
- self . ref_template
383
- . as_ref ( )
384
- . map_or ( true , |t| ctx. resolve_item ( * t) . can_derive_copy ( ctx) ) &&
385
- self . base_members
386
- . iter ( )
387
- . all ( |t| ctx. resolve_item ( * t) . can_derive_copy ( ctx) ) &&
388
- self . fields . iter ( ) . all ( |field| {
389
- ctx. resolve_item ( field. ty )
390
- . can_derive_copy ( ctx)
391
- } )
392
- }
393
-
394
336
/// Is this type a template specialization?
395
337
pub fn is_template_specialization ( & self ) -> bool {
396
338
self . ref_template . is_some ( )
@@ -858,6 +800,108 @@ impl CompInfo {
858
800
}
859
801
}
860
802
803
+ impl CanDeriveDebug for CompInfo {
804
+ type Extra = Option < Layout > ;
805
+
806
+ fn can_derive_debug ( & self ,
807
+ ctx : & BindgenContext ,
808
+ layout : Option < Layout > )
809
+ -> bool {
810
+ if self . has_non_type_template_params ( ) {
811
+ return layout. map_or ( false , |l| l. opaque ( ) . can_derive_debug ( ctx, ( ) ) ) ;
812
+ }
813
+
814
+ // We can reach here recursively via template parameters of a member,
815
+ // for example.
816
+ if self . detect_derive_debug_cycle . get ( ) {
817
+ warn ! ( "Derive debug cycle detected!" ) ;
818
+ return true ;
819
+ }
820
+
821
+ if self . kind == CompKind :: Union {
822
+ if ctx. options ( ) . unstable_rust {
823
+ return false ;
824
+ }
825
+
826
+ let layout = layout. unwrap_or_else ( Layout :: zero) ;
827
+ let size_divisor = cmp:: max ( 1 , layout. align ) ;
828
+ return layout. size / size_divisor <= RUST_DERIVE_IN_ARRAY_LIMIT ;
829
+ }
830
+
831
+ self . detect_derive_debug_cycle . set ( true ) ;
832
+
833
+ let can_derive_debug = {
834
+ self . base_members
835
+ . iter ( )
836
+ . all ( |id| id. can_derive_debug ( ctx, ( ) ) ) &&
837
+ self . template_args
838
+ . iter ( )
839
+ . all ( |id| id. can_derive_debug ( ctx, ( ) ) ) &&
840
+ self . fields
841
+ . iter ( )
842
+ . all ( |f| f. can_derive_debug ( ctx, ( ) ) ) &&
843
+ self . ref_template . map_or ( true , |id| {
844
+ id. can_derive_debug ( ctx, ( ) )
845
+ } )
846
+ } ;
847
+
848
+ self . detect_derive_debug_cycle . set ( false ) ;
849
+
850
+ can_derive_debug
851
+ }
852
+ }
853
+
854
+ impl < ' a > CanDeriveCopy < ' a > for CompInfo {
855
+ type Extra = ( & ' a Item , Option < Layout > ) ;
856
+
857
+ fn can_derive_copy ( & self ,
858
+ ctx : & BindgenContext ,
859
+ ( item, layout) : ( & Item , Option < Layout > ) )
860
+ -> bool {
861
+ if self . has_non_type_template_params ( ) {
862
+ return layout. map_or ( false , |l| l. opaque ( ) . can_derive_copy ( ctx, ( ) ) ) ;
863
+ }
864
+
865
+ // NOTE: Take into account that while unions in C and C++ are copied by
866
+ // default, the may have an explicit destructor in C++, so we can't
867
+ // defer this check just for the union case.
868
+ if self . has_destructor ( ctx) {
869
+ return false ;
870
+ }
871
+
872
+ if self . kind == CompKind :: Union {
873
+ if !ctx. options ( ) . unstable_rust {
874
+ return true ;
875
+ }
876
+
877
+ // https://github.com/rust-lang/rust/issues/36640
878
+ if !self . template_args . is_empty ( ) || self . ref_template . is_some ( ) ||
879
+ !item. applicable_template_args ( ctx) . is_empty ( ) {
880
+ return false ;
881
+ }
882
+ }
883
+
884
+ // With template args, use a safe subset of the types,
885
+ // since copyability depends on the types itself.
886
+ self . ref_template
887
+ . as_ref ( )
888
+ . map_or ( true , |t| t. can_derive_copy ( ctx, ( ) ) ) &&
889
+ self . base_members
890
+ . iter ( )
891
+ . all ( |t| t. can_derive_copy ( ctx, ( ) ) ) &&
892
+ self . fields . iter ( ) . all ( |field| {
893
+ field. can_derive_copy ( ctx, ( ) )
894
+ } )
895
+ }
896
+
897
+ fn can_derive_copy_in_array ( & self ,
898
+ ctx : & BindgenContext ,
899
+ extra : ( & Item , Option < Layout > ) )
900
+ -> bool {
901
+ self . can_derive_copy ( ctx, extra)
902
+ }
903
+ }
904
+
861
905
impl TypeCollector for CompInfo {
862
906
type Extra = Item ;
863
907
0 commit comments