@@ -308,7 +308,11 @@ impl Generics {
308
308
} ;
309
309
310
310
let lt_iter = self . params . iter_lt ( ) . map ( from_lt_id ( self ) ) ;
311
- self . params . iter ( ) . map ( from_toc_id ( self ) ) . chain ( lt_iter) . chain ( self . iter_parent ( ) )
311
+ self . params
312
+ . iter_type_or_consts ( )
313
+ . map ( from_toc_id ( self ) )
314
+ . chain ( lt_iter)
315
+ . chain ( self . iter_parent ( ) )
312
316
}
313
317
314
318
/// Iterate over types and const params without parent params.
@@ -340,16 +344,19 @@ impl Generics {
340
344
}
341
345
} ;
342
346
343
- self . params . iter ( ) . map ( from_toc_id ( self ) ) . chain ( self . params . iter_lt ( ) . map ( from_lt_id ( self ) ) )
347
+ self . params
348
+ . iter_type_or_consts ( )
349
+ . map ( from_toc_id ( self ) )
350
+ . chain ( self . params . iter_lt ( ) . map ( from_lt_id ( self ) ) )
344
351
}
345
352
346
353
/// Iterator over types and const params of parent.
347
- #[ allow( clippy:: needless_lifetimes) ]
348
- pub ( crate ) fn iter_parent < ' a > (
349
- & ' a self ,
350
- ) -> impl DoubleEndedIterator < Item = ( GenericParamId , GenericParamDataRef < ' a > ) > + ' a {
354
+ pub ( crate ) fn iter_parent (
355
+ & self ,
356
+ ) -> impl DoubleEndedIterator < Item = ( GenericParamId , GenericParamDataRef < ' _ > ) > + ' _ {
351
357
self . parent_generics ( ) . into_iter ( ) . flat_map ( |it| {
352
- let from_toc_id = move |( local_id, p) : ( _ , & ' a TypeOrConstParamData ) | {
358
+ let from_toc_id = move |( local_id, p) | {
359
+ let p: & _ = p;
353
360
let id = TypeOrConstParamId { parent : it. def , local_id } ;
354
361
match p {
355
362
TypeOrConstParamData :: TypeParamData ( p) => (
@@ -363,14 +370,14 @@ impl Generics {
363
370
}
364
371
} ;
365
372
366
- let from_lt_id = move |( local_id, p) : ( _ , & ' a LifetimeParamData ) | {
373
+ let from_lt_id = move |( local_id, p) : ( _ , _ ) | {
367
374
(
368
375
GenericParamId :: LifetimeParamId ( LifetimeParamId { parent : it. def , local_id } ) ,
369
376
GenericParamDataRef :: LifetimeParamData ( p) ,
370
377
)
371
378
} ;
372
379
let lt_iter = it. params . iter_lt ( ) . map ( from_lt_id) ;
373
- it. params . iter ( ) . map ( from_toc_id) . chain ( lt_iter)
380
+ it. params . iter_type_or_consts ( ) . map ( from_toc_id) . chain ( lt_iter)
374
381
} )
375
382
}
376
383
@@ -387,7 +394,7 @@ impl Generics {
387
394
}
388
395
389
396
/// Returns number of generic parameter excluding those from parent
390
- fn len_params ( & self ) -> usize {
397
+ fn len_type_and_const_params ( & self ) -> usize {
391
398
self . params . type_or_consts . len ( )
392
399
}
393
400
@@ -398,7 +405,7 @@ impl Generics {
398
405
let mut impl_trait_params = 0 ;
399
406
let mut const_params = 0 ;
400
407
let mut lifetime_params = 0 ;
401
- self . params . iter ( ) . for_each ( |( _, data) | match data {
408
+ self . params . iter_type_or_consts ( ) . for_each ( |( _, data) | match data {
402
409
TypeOrConstParamData :: TypeParamData ( p) => match p. provenance {
403
410
TypeParamProvenance :: TypeParamList => type_params += 1 ,
404
411
TypeParamProvenance :: TraitSelf => self_params += 1 ,
@@ -413,18 +420,23 @@ impl Generics {
413
420
( parent_len, self_params, type_params, const_params, impl_trait_params, lifetime_params)
414
421
}
415
422
416
- pub ( crate ) fn param_idx ( & self , param : TypeOrConstParamId ) -> Option < usize > {
417
- Some ( self . find_param ( param) ?. 0 )
423
+ pub ( crate ) fn type_or_const_param_idx ( & self , param : TypeOrConstParamId ) -> Option < usize > {
424
+ Some ( self . find_type_or_const_param ( param) ?. 0 )
418
425
}
419
426
420
- fn find_param ( & self , param : TypeOrConstParamId ) -> Option < ( usize , & TypeOrConstParamData ) > {
427
+ fn find_type_or_const_param (
428
+ & self ,
429
+ param : TypeOrConstParamId ,
430
+ ) -> Option < ( usize , & TypeOrConstParamData ) > {
421
431
if param. parent == self . def {
422
- let ( idx, ( _local_id, data) ) =
423
- self . params . iter ( ) . enumerate ( ) . find ( |( _, ( idx, _) ) | * idx == param. local_id ) ?;
424
- Some ( ( idx, data) )
432
+ let idx = param. local_id . into_raw ( ) . into_u32 ( ) as usize ;
433
+ if idx >= self . params . type_or_consts . len ( ) {
434
+ return None ;
435
+ }
436
+ Some ( ( idx, & self . params . type_or_consts [ param. local_id ] ) )
425
437
} else {
426
438
self . parent_generics ( )
427
- . and_then ( |g| g. find_param ( param) )
439
+ . and_then ( |g| g. find_type_or_const_param ( param) )
428
440
// Remember that parent parameters come after parameters for self.
429
441
. map ( |( idx, data) | ( self . len_self ( ) + idx, data) )
430
442
}
@@ -436,13 +448,14 @@ impl Generics {
436
448
437
449
fn find_lifetime ( & self , lifetime : LifetimeParamId ) -> Option < ( usize , & LifetimeParamData ) > {
438
450
if lifetime. parent == self . def {
439
- let ( idx, ( _local_id, data) ) = self
440
- . params
441
- . iter_lt ( )
442
- . enumerate ( )
443
- . find ( |( _, ( idx, _) ) | * idx == lifetime. local_id ) ?;
444
-
445
- Some ( ( self . len_params ( ) + idx, data) )
451
+ let idx = lifetime. local_id . into_raw ( ) . into_u32 ( ) as usize ;
452
+ if idx >= self . params . lifetimes . len ( ) {
453
+ return None ;
454
+ }
455
+ Some ( (
456
+ self . len_type_and_const_params ( ) + idx,
457
+ & self . params . lifetimes [ lifetime. local_id ] ,
458
+ ) )
446
459
} else {
447
460
self . parent_generics ( )
448
461
. and_then ( |g| g. find_lifetime ( lifetime) )
0 commit comments