@@ -308,7 +308,11 @@ impl Generics {
308308} ;
309309
310310let 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 ( ) )
312316}
313317
314318/// Iterate over types and const params without parent params.
@@ -340,16 +344,19 @@ impl Generics {
340344}
341345} ;
342346
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 ) ) )
344351}
345352
346353/// 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 < ' _ > ) > + ' _ {
351357self . 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;
353360let id = TypeOrConstParamId { parent : it. def , local_id } ;
354361match p {
355362TypeOrConstParamData :: TypeParamData ( p) => (
@@ -363,14 +370,14 @@ impl Generics {
363370}
364371} ;
365372
366- let from_lt_id = move |( local_id, p) : ( _ , & ' a LifetimeParamData ) | {
373+ let from_lt_id = move |( local_id, p) : ( _ , _ ) | {
367374(
368375GenericParamId :: LifetimeParamId ( LifetimeParamId { parent : it. def , local_id } ) ,
369376GenericParamDataRef :: LifetimeParamData ( p) ,
370377)
371378} ;
372379let 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)
374381} )
375382}
376383
@@ -387,7 +394,7 @@ impl Generics {
387394}
388395
389396/// Returns number of generic parameter excluding those from parent
390- fn len_params ( & self ) -> usize {
397+ fn len_type_and_const_params ( & self ) -> usize {
391398self . params . type_or_consts . len ( )
392399}
393400
@@ -398,7 +405,7 @@ impl Generics {
398405let mut impl_trait_params = 0 ;
399406let mut const_params = 0 ;
400407let 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 {
402409TypeOrConstParamData :: TypeParamData ( p) => match p. provenance {
403410TypeParamProvenance :: TypeParamList => type_params += 1 ,
404411TypeParamProvenance :: TraitSelf => self_params += 1 ,
@@ -413,18 +420,23 @@ impl Generics {
413420( parent_len, self_params, type_params, const_params, impl_trait_params, lifetime_params)
414421}
415422
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 )
418425}
419426
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 ) > {
421431if 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 ] ) )
425437} else {
426438self . parent_generics ( )
427- . and_then ( |g| g. find_param ( param) )
439+ . and_then ( |g| g. find_type_or_const_param ( param) )
428440// Remember that parent parameters come after parameters for self.
429441. map ( |( idx, data) | ( self . len_self ( ) + idx, data) )
430442}
@@ -436,13 +448,14 @@ impl Generics {
436448
437449fn find_lifetime ( & self , lifetime : LifetimeParamId ) -> Option < ( usize , & LifetimeParamData ) > {
438450if 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+ ) )
446459} else {
447460self . parent_generics ( )
448461. and_then ( |g| g. find_lifetime ( lifetime) )
0 commit comments