@@ -2,14 +2,14 @@ use crate::lifetime::{has_async_lifetime, CollectLifetimes};
2
2
use crate :: parse:: Item ;
3
3
use crate :: receiver:: { has_self_in_block, has_self_in_sig, ReplaceReceiver } ;
4
4
use proc_macro2:: { Span , TokenStream } ;
5
- use quote:: { quote, ToTokens } ;
5
+ use quote:: { format_ident , quote, ToTokens } ;
6
6
use std:: mem;
7
7
use syn:: punctuated:: Punctuated ;
8
8
use syn:: visit_mut:: VisitMut ;
9
9
use syn:: {
10
- parse_quote, ArgCaptured , ArgSelf , ArgSelfRef , Block , FnArg , GenericParam , Generics , Ident ,
11
- ImplItem , Lifetime , MethodSig , Pat , PatIdent , Path , ReturnType , Token , TraitItem , Type ,
12
- TypeParam , TypeParamBound , WhereClause ,
10
+ parse_quote, Block , FnArg , GenericParam , Generics , Ident , ImplItem , Lifetime , Pat , PatIdent ,
11
+ Path , Receiver , ReturnType , Signature , Token , TraitItem , Type , TypeParam , TypeParamBound ,
12
+ WhereClause ,
13
13
} ;
14
14
15
15
impl ToTokens for Item {
@@ -97,39 +97,37 @@ pub fn expand(input: &mut Item, is_local: bool) {
97
97
// Self: Sync + 'async_trait;
98
98
fn transform_sig (
99
99
context : Context ,
100
- sig : & mut MethodSig ,
100
+ sig : & mut Signature ,
101
101
has_self : bool ,
102
102
has_default : bool ,
103
103
is_local : bool ,
104
104
) {
105
- sig. decl . fn_token . span = sig. asyncness . take ( ) . unwrap ( ) . span ;
105
+ sig. fn_token . span = sig. asyncness . take ( ) . unwrap ( ) . span ;
106
106
107
- let ret = match & sig. decl . output {
107
+ let ret = match & sig. output {
108
108
ReturnType :: Default => quote ! ( ( ) ) ,
109
109
ReturnType :: Type ( _, ret) => quote ! ( #ret) ,
110
110
} ;
111
111
112
112
let mut elided = CollectLifetimes :: new ( ) ;
113
- for arg in sig. decl . inputs . iter_mut ( ) {
113
+ for arg in sig. inputs . iter_mut ( ) {
114
114
match arg {
115
- FnArg :: SelfRef ( arg) => elided. visit_arg_self_ref_mut ( arg) ,
116
- FnArg :: Captured ( arg) => elided. visit_type_mut ( & mut arg. ty ) ,
117
- _ => { }
115
+ FnArg :: Receiver ( arg) => elided. visit_receiver_mut ( arg) ,
116
+ FnArg :: Typed ( arg) => elided. visit_type_mut ( & mut arg. ty ) ,
118
117
}
119
118
}
120
119
121
120
let lifetime: Lifetime ;
122
- if !sig. decl . generics . params . is_empty ( ) || !elided. lifetimes . is_empty ( ) || has_self {
121
+ if !sig. generics . params . is_empty ( ) || !elided. lifetimes . is_empty ( ) || has_self {
123
122
lifetime = parse_quote ! ( ' async_trait) ;
124
123
let where_clause = sig
125
- . decl
126
124
. generics
127
125
. where_clause
128
126
. get_or_insert_with ( || WhereClause {
129
127
where_token : Default :: default ( ) ,
130
128
predicates : Punctuated :: new ( ) ,
131
129
} ) ;
132
- for param in & sig. decl . generics . params {
130
+ for param in & sig. generics . params {
133
131
match param {
134
132
GenericParam :: Type ( param) => {
135
133
let param = & param. ident ;
@@ -147,16 +145,18 @@ fn transform_sig(
147
145
}
148
146
}
149
147
for elided in elided. lifetimes {
150
- sig. decl . generics . params . push ( parse_quote ! ( #elided) ) ;
148
+ sig. generics . params . push ( parse_quote ! ( #elided) ) ;
151
149
where_clause
152
150
. predicates
153
151
. push ( parse_quote ! ( #elided: #lifetime) ) ;
154
152
}
155
- sig. decl . generics . params . push ( parse_quote ! ( #lifetime) ) ;
153
+ sig. generics . params . push ( parse_quote ! ( #lifetime) ) ;
156
154
if has_self {
157
- let bound: Ident = match sig. decl . inputs . iter ( ) . next ( ) {
158
- Some ( FnArg :: SelfRef ( ArgSelfRef {
159
- mutability : None , ..
155
+ let bound: Ident = match sig. inputs . iter ( ) . next ( ) {
156
+ Some ( FnArg :: Receiver ( Receiver {
157
+ reference : Some ( _) ,
158
+ mutability : None ,
159
+ ..
160
160
} ) ) => parse_quote ! ( Sync ) ,
161
161
_ => parse_quote ! ( Send ) ,
162
162
} ;
@@ -176,22 +176,21 @@ fn transform_sig(
176
176
lifetime = parse_quote ! ( ' static ) ;
177
177
} ;
178
178
179
- for ( i, arg) in sig. decl . inputs . iter_mut ( ) . enumerate ( ) {
179
+ for ( i, arg) in sig. inputs . iter_mut ( ) . enumerate ( ) {
180
180
match arg {
181
- FnArg :: SelfRef ( _ ) => { }
182
- FnArg :: SelfValue ( arg ) => arg . mutability = None ,
183
- FnArg :: Captured ( ArgCaptured {
184
- pat : Pat :: Ident ( ident ) ,
185
- ..
186
- } ) => {
187
- ident. by_ref = None ;
188
- ident. mutability = None ;
189
- }
190
- FnArg :: Captured ( arg ) => {
191
- let positional = positional_arg ( i ) ;
192
- arg . pat = parse_quote ! ( #positional ) ;
181
+ FnArg :: Receiver ( Receiver {
182
+ reference : Some ( _ ) , ..
183
+ } ) => { }
184
+ FnArg :: Receiver ( arg ) => arg . mutability = None ,
185
+ FnArg :: Typed ( arg ) => {
186
+ if let Pat :: Ident ( ident ) = & mut * arg . pat {
187
+ ident. by_ref = None ;
188
+ ident. mutability = None ;
189
+ } else {
190
+ let positional = positional_arg ( i ) ;
191
+ * arg . pat = parse_quote ! ( #positional ) ;
192
+ }
193
193
}
194
- FnArg :: Inferred ( _) | FnArg :: Ignored ( _) => panic ! ( "unsupported arg" ) ,
195
194
}
196
195
}
197
196
@@ -201,7 +200,7 @@ fn transform_sig(
201
200
quote ! ( core:: marker:: Send + #lifetime)
202
201
} ;
203
202
204
- sig. decl . output = parse_quote ! {
203
+ sig. output = parse_quote ! {
205
204
-> core:: pin:: Pin <Box <
206
205
dyn core:: future:: Future <Output = #ret> + #bounds
207
206
>>
@@ -220,7 +219,7 @@ fn transform_sig(
220
219
// Pin::from(Box::new(async_trait_method::<T, Self>(self, x)))
221
220
fn transform_block (
222
221
context : Context ,
223
- sig : & mut MethodSig ,
222
+ sig : & mut Signature ,
224
223
block : & mut Block ,
225
224
has_self : bool ,
226
225
is_local : bool ,
@@ -234,20 +233,17 @@ fn transform_block(
234
233
return ;
235
234
}
236
235
237
- let inner = Ident :: new ( & format ! ( "__{}" , sig. ident) , sig. ident . span ( ) ) ;
238
- let args = sig
239
- . decl
240
- . inputs
241
- . iter ( )
242
- . enumerate ( )
243
- . map ( |( i, arg) | match arg {
244
- FnArg :: SelfRef ( _) | FnArg :: SelfValue ( _) => quote ! ( self ) ,
245
- FnArg :: Captured ( ArgCaptured {
246
- pat : Pat :: Ident ( PatIdent { ident, .. } ) ,
247
- ..
248
- } ) => quote ! ( #ident) ,
249
- _ => positional_arg ( i) . into_token_stream ( ) ,
250
- } ) ;
236
+ let inner = format_ident ! ( "__{}" , sig. ident, span = sig. ident. span( ) ) ;
237
+ let args = sig. inputs . iter ( ) . enumerate ( ) . map ( |( i, arg) | match arg {
238
+ FnArg :: Receiver ( _) => quote ! ( self ) ,
239
+ FnArg :: Typed ( arg) => {
240
+ if let Pat :: Ident ( PatIdent { ident, .. } ) = & * arg. pat {
241
+ quote ! ( #ident)
242
+ } else {
243
+ positional_arg ( i) . into_token_stream ( )
244
+ }
245
+ }
246
+ } ) ;
251
247
252
248
let mut standalone = sig. clone ( ) ;
253
249
standalone. ident = inner. clone ( ) ;
@@ -256,38 +252,36 @@ fn transform_block(
256
252
Context :: Trait { generics, .. } => generics,
257
253
Context :: Impl { impl_generics, .. } => impl_generics,
258
254
} ;
259
- let fn_generics = mem:: replace ( & mut standalone. decl . generics , outer_generics. clone ( ) ) ;
260
- standalone. decl . generics . params . extend ( fn_generics. params ) ;
255
+ let fn_generics = mem:: replace ( & mut standalone. generics , outer_generics. clone ( ) ) ;
256
+ standalone. generics . params . extend ( fn_generics. params ) ;
261
257
if let Some ( where_clause) = fn_generics. where_clause {
262
258
standalone
263
- . decl
264
259
. generics
265
260
. make_where_clause ( )
266
261
. predicates
267
262
. extend ( where_clause. predicates ) ;
268
263
}
269
264
270
265
if has_async_lifetime ( & mut standalone, block) {
271
- standalone
272
- . decl
273
- . generics
274
- . params
275
- . push ( parse_quote ! ( ' async_trait) ) ;
266
+ standalone. generics . params . push ( parse_quote ! ( ' async_trait) ) ;
276
267
}
277
268
278
269
let mut types = standalone
279
- . decl
280
270
. generics
281
271
. type_params ( )
282
272
. map ( |param| param. ident . clone ( ) )
283
273
. collect :: < Vec < _ > > ( ) ;
284
274
285
275
let mut self_bound = None :: < TypeParamBound > ;
286
- match standalone. decl . inputs . iter_mut ( ) . next ( ) {
287
- Some ( arg @ FnArg :: SelfRef ( _) ) => {
276
+ match standalone. inputs . iter_mut ( ) . next ( ) {
277
+ Some (
278
+ arg @ FnArg :: Receiver ( Receiver {
279
+ reference : Some ( _) , ..
280
+ } ) ,
281
+ ) => {
288
282
let ( lifetime, mutability, self_token) = match arg {
289
- FnArg :: SelfRef ( ArgSelfRef {
290
- lifetime,
283
+ FnArg :: Receiver ( Receiver {
284
+ reference : Some ( ( _ , lifetime) ) ,
291
285
mutability,
292
286
self_token,
293
287
..
@@ -312,9 +306,9 @@ fn transform_block(
312
306
}
313
307
}
314
308
}
315
- Some ( arg @ FnArg :: SelfValue ( _) ) => {
309
+ Some ( arg @ FnArg :: Receiver ( _) ) => {
316
310
let self_token = match arg {
317
- FnArg :: SelfValue ( ArgSelf { self_token, .. } ) => self_token,
311
+ FnArg :: Receiver ( Receiver { self_token, .. } ) => self_token,
318
312
_ => unreachable ! ( ) ,
319
313
} ;
320
314
let under_self = Ident :: new ( "_self" , self_token. span ) ;
@@ -332,12 +326,11 @@ fn transform_block(
332
326
}
333
327
}
334
328
}
335
- Some ( FnArg :: Captured ( ArgCaptured {
336
- pat : Pat :: Ident ( arg) ,
337
- ..
338
- } ) ) => {
339
- if arg. ident == "self" {
340
- arg. ident = Ident :: new ( "_self" , arg. ident . span ( ) ) ;
329
+ Some ( FnArg :: Typed ( arg) ) => {
330
+ if let Pat :: Ident ( arg) = & mut * arg. pat {
331
+ if arg. ident == "self" {
332
+ arg. ident = Ident :: new ( "_self" , arg. ident . span ( ) ) ;
333
+ }
341
334
}
342
335
}
343
336
_ => { }
@@ -351,15 +344,14 @@ fn transform_block(
351
344
self_param. bounds . extend ( self_bound) ;
352
345
}
353
346
standalone
354
- . decl
355
347
. generics
356
348
. params
357
349
. push ( GenericParam :: Type ( self_param) ) ;
358
350
types. push ( Ident :: new ( "Self" , Span :: call_site ( ) ) ) ;
359
351
}
360
352
}
361
353
362
- if let Some ( where_clause) = & mut standalone. decl . generics . where_clause {
354
+ if let Some ( where_clause) = & mut standalone. generics . where_clause {
363
355
// Work around an input bound like `where Self::Output: Send` expanding
364
356
// to `where <AsyncTrait>::Output: Send` which is illegal syntax because
365
357
// `where<T>` is reserved for future use... :(
@@ -372,7 +364,7 @@ fn transform_block(
372
364
receiver, as_trait, ..
373
365
} => ReplaceReceiver :: with_as_trait ( receiver. clone ( ) , as_trait. clone ( ) ) ,
374
366
} ;
375
- replace. visit_method_sig_mut ( & mut standalone) ;
367
+ replace. visit_signature_mut ( & mut standalone) ;
376
368
replace. visit_block_mut ( block) ;
377
369
378
370
let brace = block. brace_token ;
@@ -385,13 +377,13 @@ fn transform_block(
385
377
}
386
378
387
379
fn positional_arg ( i : usize ) -> Ident {
388
- Ident :: new ( & format ! ( "__arg{}" , i) , Span :: call_site ( ) )
380
+ format_ident ! ( "__arg{}" , i)
389
381
}
390
382
391
383
fn has_bound ( supertraits : & Supertraits , marker : & Ident ) -> bool {
392
384
for bound in supertraits {
393
385
if let TypeParamBound :: Trait ( bound) = bound {
394
- if bound. path . is_ident ( marker. clone ( ) ) {
386
+ if bound. path . is_ident ( marker) {
395
387
return true ;
396
388
}
397
389
}
0 commit comments