@@ -10,7 +10,7 @@ use syn::punctuated::Punctuated;
10
10
use syn:: visit_mut:: { self , VisitMut } ;
11
11
use syn:: {
12
12
parse_quote, parse_quote_spanned, Attribute , Block , FnArg , GenericArgument , GenericParam ,
13
- Generics , Ident , ImplItem , Lifetime , LifetimeDef , Pat , PatIdent , PathArguments , Receiver ,
13
+ Generics , Ident , ImplItem , Lifetime , LifetimeParam , Pat , PatIdent , PathArguments , Receiver ,
14
14
ReturnType , Signature , Stmt , Token , TraitItem , Type , TypePath , WhereClause ,
15
15
} ;
16
16
@@ -36,7 +36,7 @@ enum Context<'a> {
36
36
}
37
37
38
38
impl Context < ' _ > {
39
- fn lifetimes < ' a > ( & ' a self , used : & ' a [ Lifetime ] ) -> impl Iterator < Item = & ' a LifetimeDef > {
39
+ fn lifetimes < ' a > ( & ' a self , used : & ' a [ Lifetime ] ) -> impl Iterator < Item = & ' a LifetimeParam > {
40
40
let generics = match self {
41
41
Context :: Trait { generics, .. } => generics,
42
42
Context :: Impl { impl_generics, .. } => impl_generics,
@@ -60,7 +60,7 @@ pub fn expand(input: &mut Item, is_local: bool) {
60
60
supertraits : & input. supertraits ,
61
61
} ;
62
62
for inner in & mut input. items {
63
- if let TraitItem :: Method ( method) = inner {
63
+ if let TraitItem :: Fn ( method) = inner {
64
64
let sig = & mut method. sig ;
65
65
if sig. asyncness . is_some ( ) {
66
66
let block = & mut method. default ;
@@ -94,7 +94,7 @@ pub fn expand(input: &mut Item, is_local: bool) {
94
94
associated_type_impl_traits : & associated_type_impl_traits,
95
95
} ;
96
96
for inner in & mut input. items {
97
- if let ImplItem :: Method ( method) = inner {
97
+ if let ImplItem :: Fn ( method) = inner {
98
98
let sig = & mut method. sig ;
99
99
if sig. asyncness . is_some ( ) {
100
100
let block = & mut method. block ;
@@ -208,7 +208,7 @@ fn transform_sig(
208
208
sig. generics . lt_token = Some ( Token ! [ <] ( sig. ident . span ( ) ) ) ;
209
209
}
210
210
if sig. generics . gt_token . is_none ( ) {
211
- sig. generics . gt_token = Some ( Token ! [ >] ( sig. paren_token . span ) ) ;
211
+ sig. generics . gt_token = Some ( Token ! [ >] ( sig. paren_token . span . join ( ) ) ) ;
212
212
}
213
213
214
214
for elided in lifetimes. elided {
@@ -223,46 +223,35 @@ fn transform_sig(
223
223
. push ( parse_quote_spanned ! ( default_span=> ' async_trait) ) ;
224
224
225
225
if has_self {
226
- let bounds: & [ InferredBound ] = match sig. inputs . iter ( ) . next ( ) {
227
- Some ( FnArg :: Receiver ( Receiver {
228
- reference : Some ( _) ,
229
- mutability : None ,
230
- ..
231
- } ) ) => & [ InferredBound :: Sync ] ,
232
- Some ( FnArg :: Typed ( arg) )
233
- if match arg. pat . as_ref ( ) {
234
- Pat :: Ident ( pat) => pat. ident == "self" ,
235
- _ => false ,
236
- } =>
237
- {
238
- match arg. ty . as_ref ( ) {
239
- // self: &Self
240
- Type :: Reference ( ty) if ty. mutability . is_none ( ) => & [ InferredBound :: Sync ] ,
241
- // self: Arc<Self>
242
- Type :: Path ( ty)
243
- if {
244
- let segment = ty. path . segments . last ( ) . unwrap ( ) ;
245
- segment. ident == "Arc"
246
- && match & segment. arguments {
247
- PathArguments :: AngleBracketed ( arguments) => {
248
- arguments. args . len ( ) == 1
249
- && match & arguments. args [ 0 ] {
250
- GenericArgument :: Type ( Type :: Path ( arg) ) => {
251
- arg. path . is_ident ( "Self" )
252
- }
253
- _ => false ,
226
+ let bounds: & [ InferredBound ] = if let Some ( receiver) = sig. receiver ( ) {
227
+ match receiver. ty . as_ref ( ) {
228
+ // self: &Self
229
+ Type :: Reference ( ty) if ty. mutability . is_none ( ) => & [ InferredBound :: Sync ] ,
230
+ // self: Arc<Self>
231
+ Type :: Path ( ty)
232
+ if {
233
+ let segment = ty. path . segments . last ( ) . unwrap ( ) ;
234
+ segment. ident == "Arc"
235
+ && match & segment. arguments {
236
+ PathArguments :: AngleBracketed ( arguments) => {
237
+ arguments. args . len ( ) == 1
238
+ && match & arguments. args [ 0 ] {
239
+ GenericArgument :: Type ( Type :: Path ( arg) ) => {
240
+ arg. path . is_ident ( "Self" )
254
241
}
255
- }
256
- _ => false ,
242
+ _ => false ,
243
+ }
257
244
}
258
- } =>
259
- {
260
- & [ InferredBound :: Sync , InferredBound :: Send ]
261
- }
262
- _ => & [ InferredBound :: Send ] ,
245
+ _ => false ,
246
+ }
247
+ } =>
248
+ {
249
+ & [ InferredBound :: Sync , InferredBound :: Send ]
263
250
}
251
+ _ => & [ InferredBound :: Send ] ,
264
252
}
265
- _ => & [ InferredBound :: Send ] ,
253
+ } else {
254
+ & [ InferredBound :: Send ]
266
255
} ;
267
256
268
257
let bounds = bounds. iter ( ) . filter_map ( |bound| {
@@ -286,24 +275,24 @@ fn transform_sig(
286
275
287
276
for ( i, arg) in sig. inputs . iter_mut ( ) . enumerate ( ) {
288
277
match arg {
289
- FnArg :: Receiver ( Receiver {
290
- reference : Some ( _) , ..
291
- } ) => { }
292
- FnArg :: Receiver ( arg) => arg. mutability = None ,
278
+ FnArg :: Receiver ( receiver) => {
279
+ if receiver. reference . is_none ( ) {
280
+ receiver. mutability = None ;
281
+ }
282
+ }
293
283
FnArg :: Typed ( arg) => {
294
- let type_is_reference = match * arg. ty {
295
- Type :: Reference ( _) => true ,
296
- _ => false ,
297
- } ;
298
- if let Pat :: Ident ( pat) = & mut * arg. pat {
299
- if pat. ident == "self" || !type_is_reference {
284
+ if match * arg. ty {
285
+ Type :: Reference ( _) => false ,
286
+ _ => true ,
287
+ } {
288
+ if let Pat :: Ident ( pat) = & mut * arg. pat {
300
289
pat. by_ref = None ;
301
290
pat. mutability = None ;
291
+ } else {
292
+ let positional = positional_arg ( i, & arg. pat ) ;
293
+ let m = mut_pat ( & mut arg. pat ) ;
294
+ arg. pat = parse_quote ! ( #m #positional) ;
302
295
}
303
- } else if !type_is_reference {
304
- let positional = positional_arg ( i, & arg. pat ) ;
305
- let m = mut_pat ( & mut arg. pat ) ;
306
- arg. pat = parse_quote ! ( #m #positional) ;
307
296
}
308
297
AddLifetimeToImplTrait . visit_type_mut ( & mut arg. ty ) ;
309
298
}
@@ -366,26 +355,18 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
366
355
// the parameter, forward it to the variable.
367
356
//
368
357
// This is currently not applied to the `self` parameter.
369
- let attrs = arg. attrs . iter ( ) . filter ( |attr| attr. path . is_ident ( "cfg" ) ) ;
358
+ let attrs = arg. attrs . iter ( ) . filter ( |attr| attr. path ( ) . is_ident ( "cfg" ) ) ;
370
359
371
- if let Pat :: Ident ( PatIdent {
360
+ if let Type :: Reference ( _) = * arg. ty {
361
+ quote ! ( )
362
+ } else if let Pat :: Ident ( PatIdent {
372
363
ident, mutability, ..
373
364
} ) = & * arg. pat
374
365
{
375
- if ident == "self" {
376
- self_span = Some ( ident. span ( ) ) ;
377
- let prefixed = Ident :: new ( "__self" , ident. span ( ) ) ;
378
- quote ! ( let #mutability #prefixed = #ident; )
379
- } else if let Type :: Reference ( _) = * arg. ty {
380
- quote ! ( )
381
- } else {
382
- quote ! {
383
- #( #attrs) *
384
- let #mutability #ident = #ident;
385
- }
366
+ quote ! {
367
+ #( #attrs) *
368
+ let #mutability #ident = #ident;
386
369
}
387
- } else if let Type :: Reference ( _) = * arg. ty {
388
- quote ! ( )
389
370
} else {
390
371
let pat = & arg. pat ;
391
372
let ident = positional_arg ( i, pat) ;
0 commit comments