@@ -15,9 +15,9 @@ use syn::{
15
15
parse_macro_input, parse_quote,
16
16
punctuated:: Punctuated ,
17
17
token:: { Comma , Plus } ,
18
- Error , FnArg , GenericParam , Generics , Ident , ItemTrait , Lifetime , Pat , PatType , Receiver ,
19
- Result , ReturnType , Signature , Token , TraitBound , TraitItem , TraitItemConst , TraitItemFn ,
20
- TraitItemType , Type , TypeImplTrait , TypeParamBound , WhereClause ,
18
+ Error , FnArg , GenericParam , Generics , Ident , ItemTrait , Lifetime , Pat , PatIdent , PatType ,
19
+ Receiver , Result , ReturnType , Signature , Token , TraitBound , TraitItem , TraitItemConst ,
20
+ TraitItemFn , TraitItemType , Type , TypeImplTrait , TypeParamBound , WhereClause ,
21
21
} ;
22
22
23
23
struct Attrs {
@@ -145,10 +145,32 @@ fn transform_item(item: &TraitItem, bounds: &Vec<TypeParamBound>) -> TraitItem {
145
145
output : ReturnType :: Type ( syn:: parse2 ( quote ! { -> } ) . unwrap ( ) , Box :: new ( ty) ) ,
146
146
..sig. clone ( )
147
147
} ,
148
- fn_item
149
- . default
150
- . as_ref ( )
151
- . map ( |b| syn:: parse2 ( quote ! { { async move #b } } ) . unwrap ( ) ) ,
148
+ fn_item. default . as_ref ( ) . map ( |b| {
149
+ let items = sig. inputs . iter ( ) . map ( |i| match i {
150
+ FnArg :: Receiver ( Receiver { self_token, .. } ) => {
151
+ quote ! { let __self = #self_token; }
152
+ }
153
+ FnArg :: Typed ( PatType { pat, .. } ) => match pat. as_ref ( ) {
154
+ Pat :: Ident ( PatIdent { ident, .. } ) => quote ! { let #ident = #ident; } ,
155
+ _ => todo ! ( ) ,
156
+ } ,
157
+ } ) ;
158
+
159
+ struct ReplaceSelfVisitor ;
160
+ impl syn:: visit_mut:: VisitMut for ReplaceSelfVisitor {
161
+ fn visit_ident_mut ( & mut self , ident : & mut syn:: Ident ) {
162
+ if ident == "self" {
163
+ * ident = syn:: Ident :: new ( "__self" , ident. span ( ) ) ;
164
+ }
165
+ syn:: visit_mut:: visit_ident_mut ( self , ident) ;
166
+ }
167
+ }
168
+
169
+ let mut block = b. clone ( ) ;
170
+ syn:: visit_mut:: visit_block_mut ( & mut ReplaceSelfVisitor , & mut block) ;
171
+
172
+ parse_quote ! { { async move { #( #items) * #block} } }
173
+ } ) ,
152
174
)
153
175
} else {
154
176
match & sig. output {
0 commit comments