@@ -2,9 +2,9 @@ use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
2
2
use proc_macro_error:: { abort, emit_error} ;
3
3
use quote:: { ToTokens , TokenStreamExt } ;
4
4
use syn:: {
5
- spanned:: Spanned , FnArg , Ident , ItemTrait , Lifetime , Pat , PatIdent , ReturnType , Signature ,
6
- TraitBound , TraitBoundModifier , TraitItem , TraitItemConst , TraitItemMethod , TraitItemType ,
7
- Type , TypeParamBound , WherePredicate ,
5
+ spanned:: Spanned , Attribute , FnArg , Ident , ItemTrait , Lifetime , Pat , PatIdent , ReturnType ,
6
+ Signature , TraitBound , TraitBoundModifier , TraitItem , TraitItemConst , TraitItemMethod ,
7
+ TraitItemType , Type , TypeParamBound , WherePredicate ,
8
8
} ;
9
9
10
10
use crate :: {
@@ -13,8 +13,6 @@ use crate::{
13
13
proxy:: ProxyType ,
14
14
} ;
15
15
16
-
17
-
18
16
/// Generates one complete impl of the given trait for each of the given proxy
19
17
/// types. All impls are returned as token stream.
20
18
pub ( crate ) fn gen_impls ( proxy_types : & [ ProxyType ] , trait_def : & syn:: ItemTrait ) -> TokenStream2 {
@@ -59,7 +57,6 @@ fn gen_header(
59
57
let trait_ident = & trait_def. ident ;
60
58
let trait_path = quote ! { #trait_ident #trait_generics } ;
61
59
62
-
63
60
// Here we assemble the parameter list of the impl (the thing in
64
61
// `impl< ... >`). This is simply the parameter list of the trait with
65
62
// one or two parameters added. For a trait `trait Foo<'x, 'y, A, B>`,
@@ -248,7 +245,6 @@ fn gen_header(
248
245
params
249
246
} ;
250
247
251
-
252
248
// The tokens after `for` in the impl header (the type the trait is
253
249
// implemented for).
254
250
#[ rustfmt:: skip]
@@ -345,7 +341,6 @@ fn gen_fn_type_for_trait(proxy_type: &ProxyType, trait_def: &ItemTrait) -> Token
345
341
) ;
346
342
}
347
343
348
-
349
344
// =======================================================================
350
345
// Check if the trait can be implemented for the given proxy type
351
346
let self_type = SelfType :: from_sig ( sig) ;
@@ -439,7 +434,6 @@ fn gen_fn_type_for_trait(proxy_type: &ProxyType, trait_def: &ItemTrait) -> Token
439
434
}
440
435
}
441
436
442
-
443
437
quote ! {
444
438
for < #( #local_lifetimes) , * > #fn_name ( #arg_types) #ret
445
439
}
@@ -528,9 +522,10 @@ fn gen_const_item(
528
522
// We simply use the associated const from our type parameter.
529
523
let const_name = & item. ident ;
530
524
let const_ty = & item. ty ;
525
+ let attrs = filter_attrs ( & item. attrs ) ;
531
526
532
527
Ok ( quote ! {
533
- const #const_name: #const_ty = #proxy_ty_param:: #const_name;
528
+ # ( #attrs ) * const #const_name: #const_ty = #proxy_ty_param:: #const_name;
534
529
} )
535
530
}
536
531
@@ -560,9 +555,10 @@ fn gen_type_item(
560
555
561
556
// We simply use the associated type from our type parameter.
562
557
let assoc_name = & item. ident ;
558
+ let attrs = filter_attrs ( & item. attrs ) ;
563
559
564
560
Ok ( quote ! {
565
- type #assoc_name = #proxy_ty_param:: #assoc_name;
561
+ # ( #attrs ) * type #assoc_name = #proxy_ty_param:: #assoc_name;
566
562
} )
567
563
}
568
564
@@ -597,6 +593,7 @@ fn gen_method_item(
597
593
// Determine the kind of the method, determined by the self type.
598
594
let sig = & item. sig ;
599
595
let self_arg = SelfType :: from_sig ( sig) ;
596
+ let attrs = filter_attrs ( & item. attrs ) ;
600
597
601
598
// Check self type and proxy type combination
602
599
check_receiver_compatible ( proxy_type, self_arg, & trait_def. ident , sig. span ( ) . into ( ) ) ;
@@ -669,10 +666,9 @@ fn gen_method_item(
669
666
} ;
670
667
671
668
// Combine body with signature
672
- Ok ( quote ! { #sig { #body } } )
669
+ Ok ( quote ! { #( #attrs ) * # sig { #body } } )
673
670
}
674
671
675
-
676
672
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
677
673
enum SelfType {
678
674
None ,
@@ -842,3 +838,11 @@ fn should_keep_default_for(m: &TraitItemMethod, proxy_type: &ProxyType) -> bool
842
838
843
839
out
844
840
}
841
+
842
+ fn filter_attrs ( attrs : & [ Attribute ] ) -> Vec < Attribute > {
843
+ attrs
844
+ . iter ( )
845
+ . filter ( |attr| attr. path . is_ident ( "cfg" ) )
846
+ . cloned ( )
847
+ . collect ( )
848
+ }
0 commit comments