Skip to content

Commit 2e6fa94

Browse files
authored
Merge pull request #76 from bachue/features/inherit_attrs_for_generated_methods_const_or_types
inherit attributes for generated methods / consts / types
2 parents a91f68c + 8023066 commit 2e6fa94

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/gen.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
22
use proc_macro_error::{abort, emit_error};
33
use quote::{ToTokens, TokenStreamExt};
44
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,
88
};
99

1010
use crate::{
@@ -13,8 +13,6 @@ use crate::{
1313
proxy::ProxyType,
1414
};
1515

16-
17-
1816
/// Generates one complete impl of the given trait for each of the given proxy
1917
/// types. All impls are returned as token stream.
2018
pub(crate) fn gen_impls(proxy_types: &[ProxyType], trait_def: &syn::ItemTrait) -> TokenStream2 {
@@ -59,7 +57,6 @@ fn gen_header(
5957
let trait_ident = &trait_def.ident;
6058
let trait_path = quote! { #trait_ident #trait_generics };
6159

62-
6360
// Here we assemble the parameter list of the impl (the thing in
6461
// `impl< ... >`). This is simply the parameter list of the trait with
6562
// one or two parameters added. For a trait `trait Foo<'x, 'y, A, B>`,
@@ -248,7 +245,6 @@ fn gen_header(
248245
params
249246
};
250247

251-
252248
// The tokens after `for` in the impl header (the type the trait is
253249
// implemented for).
254250
#[rustfmt::skip]
@@ -345,7 +341,6 @@ fn gen_fn_type_for_trait(proxy_type: &ProxyType, trait_def: &ItemTrait) -> Token
345341
);
346342
}
347343

348-
349344
// =======================================================================
350345
// Check if the trait can be implemented for the given proxy type
351346
let self_type = SelfType::from_sig(sig);
@@ -439,7 +434,6 @@ fn gen_fn_type_for_trait(proxy_type: &ProxyType, trait_def: &ItemTrait) -> Token
439434
}
440435
}
441436

442-
443437
quote! {
444438
for< #(#local_lifetimes),* > #fn_name (#arg_types) #ret
445439
}
@@ -528,9 +522,10 @@ fn gen_const_item(
528522
// We simply use the associated const from our type parameter.
529523
let const_name = &item.ident;
530524
let const_ty = &item.ty;
525+
let attrs = filter_attrs(&item.attrs);
531526

532527
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;
534529
})
535530
}
536531

@@ -560,9 +555,10 @@ fn gen_type_item(
560555

561556
// We simply use the associated type from our type parameter.
562557
let assoc_name = &item.ident;
558+
let attrs = filter_attrs(&item.attrs);
563559

564560
Ok(quote! {
565-
type #assoc_name = #proxy_ty_param::#assoc_name;
561+
#(#attrs)* type #assoc_name = #proxy_ty_param::#assoc_name;
566562
})
567563
}
568564

@@ -597,6 +593,7 @@ fn gen_method_item(
597593
// Determine the kind of the method, determined by the self type.
598594
let sig = &item.sig;
599595
let self_arg = SelfType::from_sig(sig);
596+
let attrs = filter_attrs(&item.attrs);
600597

601598
// Check self type and proxy type combination
602599
check_receiver_compatible(proxy_type, self_arg, &trait_def.ident, sig.span().into());
@@ -669,10 +666,9 @@ fn gen_method_item(
669666
};
670667

671668
// Combine body with signature
672-
Ok(quote! { #sig { #body }})
669+
Ok(quote! { #(#attrs)* #sig { #body }})
673670
}
674671

675-
676672
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
677673
enum SelfType {
678674
None,
@@ -842,3 +838,11 @@ fn should_keep_default_for(m: &TraitItemMethod, proxy_type: &ProxyType) -> bool
842838

843839
out
844840
}
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

Comments
 (0)