99use std:: iter;
1010
1111use proc_macro2:: TokenStream ;
12- use quote:: quote;
12+ use quote:: { quote, ToTokens } ;
1313use syn:: {
1414 parse:: { Parse , ParseStream } ,
1515 parse_macro_input,
1616 punctuated:: Punctuated ,
17- token:: Plus ,
18- Error , FnArg , Generics , Ident , ItemTrait , Pat , PatType , Result , ReturnType , Signature , Token ,
19- TraitBound , TraitItem , TraitItemConst , TraitItemFn , TraitItemType , Type , TypeImplTrait ,
20- TypeParamBound ,
17+ token:: { Comma , Plus } ,
18+ Error , FnArg , GenericParam , Generics , Ident , ItemTrait , Lifetime , Pat , PatType , Result ,
19+ ReturnType , Signature , Token , TraitBound , TraitItem , TraitItemConst , TraitItemFn ,
20+ TraitItemType , Type , TypeImplTrait , TypeParamBound ,
2121} ;
2222
2323struct Attrs {
@@ -162,16 +162,59 @@ fn transform_item(item: &TraitItem, bounds: &Vec<TypeParamBound>) -> TraitItem {
162162
163163fn mk_blanket_impl ( attrs : & Attrs , tr : & ItemTrait ) -> TokenStream {
164164 let orig = & tr. ident ;
165+ let generics = & tr. generics . params ;
166+ let mut generic_names = tr
167+ . generics
168+ . params
169+ . iter ( )
170+ . map ( |generic| match generic {
171+ GenericParam :: Lifetime ( lt) => GenericParamName :: Lifetime ( & lt. lifetime ) ,
172+ GenericParam :: Type ( ty) => GenericParamName :: Type ( & ty. ident ) ,
173+ GenericParam :: Const ( co) => GenericParamName :: Const ( & co. ident ) ,
174+ } )
175+ . collect :: < Punctuated < _ , Comma > > ( ) ;
176+ let trailing_comma = if !generic_names. is_empty ( ) {
177+ generic_names. push_punct ( Comma :: default ( ) ) ;
178+ quote ! { , }
179+ } else {
180+ quote ! { }
181+ } ;
165182 let variant = & attrs. variant . name ;
166- let items = tr. items . iter ( ) . map ( |item| blanket_impl_item ( item, variant) ) ;
183+ let items = tr
184+ . items
185+ . iter ( )
186+ . map ( |item| blanket_impl_item ( item, variant, & generic_names) ) ;
187+ let where_clauses = tr. generics . where_clause . as_ref ( ) . map ( |wh| & wh. predicates ) ;
167188 quote ! {
168- impl <T > #orig for T where T : #variant {
189+ impl <#generics #trailing_comma T > #orig<#generic_names> for T
190+ where T : #variant<#generic_names>, #where_clauses
191+ {
169192 #( #items) *
170193 }
171194 }
172195}
173196
174- fn blanket_impl_item ( item : & TraitItem , variant : & Ident ) -> TokenStream {
197+ enum GenericParamName < ' s > {
198+ Lifetime ( & ' s Lifetime ) ,
199+ Type ( & ' s Ident ) ,
200+ Const ( & ' s Ident ) ,
201+ }
202+
203+ impl ToTokens for GenericParamName < ' _ > {
204+ fn to_tokens ( & self , tokens : & mut TokenStream ) {
205+ match self {
206+ GenericParamName :: Lifetime ( lt) => lt. to_tokens ( tokens) ,
207+ GenericParamName :: Type ( ty) => ty. to_tokens ( tokens) ,
208+ GenericParamName :: Const ( co) => co. to_tokens ( tokens) ,
209+ }
210+ }
211+ }
212+
213+ fn blanket_impl_item (
214+ item : & TraitItem ,
215+ variant : & Ident ,
216+ generic_names : & Punctuated < GenericParamName < ' _ > , Comma > ,
217+ ) -> TokenStream {
175218 // impl<T> IntFactory for T where T: SendIntFactory {
176219 // const NAME: &'static str = <Self as SendIntFactory>::NAME;
177220 // type MyFut<'a> = <Self as SendIntFactory>::MyFut<'a> where Self: 'a;
@@ -187,7 +230,7 @@ fn blanket_impl_item(item: &TraitItem, variant: &Ident) -> TokenStream {
187230 ..
188231 } ) => {
189232 quote ! {
190- const #ident #generics: #ty = <Self as #variant>:: #ident;
233+ const #ident #generics: #ty = <Self as #variant<#generic_names> >:: #ident;
191234 }
192235 }
193236 TraitItem :: Fn ( TraitItemFn { sig, .. } ) => {
@@ -207,7 +250,7 @@ fn blanket_impl_item(item: &TraitItem, variant: &Ident) -> TokenStream {
207250 } ;
208251 quote ! {
209252 #sig {
210- <Self as #variant>:: #ident( #( #args) , * ) #maybe_await
253+ <Self as #variant<#generic_names> >:: #ident( #( #args) , * ) #maybe_await
211254 }
212255 }
213256 }
@@ -222,7 +265,7 @@ fn blanket_impl_item(item: &TraitItem, variant: &Ident) -> TokenStream {
222265 ..
223266 } ) => {
224267 quote ! {
225- type #ident<#params> = <Self as #variant>:: #ident<#params> #where_clause;
268+ type #ident<#params> = <Self as #variant<#generic_names> >:: #ident<#params> #where_clause;
226269 }
227270 }
228271 _ => Error :: new_spanned ( item, "unsupported item type" ) . into_compile_error ( ) ,
0 commit comments