@@ -422,21 +422,9 @@ impl Rewrite for ast::WherePredicate {
422
422
423
423
let colon = type_bound_colon ( context) ;
424
424
425
- if bound_generic_params
426
- . iter ( )
427
- . filter ( |p| p. is_lifetime_param ( ) )
428
- . count ( ) > 0
425
+ if let Some ( lifetime_str) =
426
+ rewrite_lifetime_param ( context, shape, bound_generic_params)
429
427
{
430
- let lifetime_str: String = bound_generic_params
431
- . iter ( )
432
- . filter_map ( |p| match p {
433
- & ast:: GenericParam :: Lifetime ( ref l) => Some ( l) ,
434
- _ => None ,
435
- } )
436
- . map ( |lt| lt. rewrite ( context, shape) )
437
- . collect :: < Option < Vec < _ > > > ( ) ?
438
- . join ( ", " ) ;
439
-
440
428
// 6 = "for<> ".len()
441
429
let used_width = lifetime_str. len ( ) + type_str. len ( ) + colon. len ( ) + 6 ;
442
430
let ty_shape = shape. offset_left ( used_width) ?;
@@ -598,21 +586,9 @@ impl Rewrite for ast::TyParam {
598
586
599
587
impl Rewrite for ast:: PolyTraitRef {
600
588
fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
601
- if self . bound_generic_params
602
- . iter ( )
603
- . filter ( |p| p. is_lifetime_param ( ) )
604
- . count ( ) > 0
589
+ if let Some ( lifetime_str) =
590
+ rewrite_lifetime_param ( context, shape, & self . bound_generic_params )
605
591
{
606
- let lifetime_str: String = self . bound_generic_params
607
- . iter ( )
608
- . filter_map ( |p| match p {
609
- & ast:: GenericParam :: Lifetime ( ref l) => Some ( l) ,
610
- _ => None ,
611
- } )
612
- . map ( |lt| lt. rewrite ( context, shape) )
613
- . collect :: < Option < Vec < _ > > > ( ) ?
614
- . join ( ", " ) ;
615
-
616
592
// 6 is "for<> ".len()
617
593
let extra_offset = lifetime_str. len ( ) + 6 ;
618
594
let path_str = self . trait_ref
@@ -762,31 +738,13 @@ fn rewrite_bare_fn(
762
738
) -> Option < String > {
763
739
let mut result = String :: with_capacity ( 128 ) ;
764
740
765
- if bare_fn
766
- . generic_params
767
- . iter ( )
768
- . filter ( |p| p. is_lifetime_param ( ) )
769
- . count ( ) > 0
741
+ if let Some ( ref lifetime_str) = rewrite_lifetime_param ( context, shape, & bare_fn. generic_params )
770
742
{
771
743
result. push_str ( "for<" ) ;
772
744
// 6 = "for<> ".len(), 4 = "for<".
773
745
// This doesn't work out so nicely for mutliline situation with lots of
774
746
// rightward drift. If that is a problem, we could use the list stuff.
775
- result. push_str ( & bare_fn
776
- . generic_params
777
- . iter ( )
778
- . filter_map ( |p| match p {
779
- & ast:: GenericParam :: Lifetime ( ref l) => Some ( l) ,
780
- _ => None ,
781
- } )
782
- . map ( |l| {
783
- l. rewrite (
784
- context,
785
- Shape :: legacy ( shape. width . checked_sub ( 6 ) ?, shape. indent + 4 ) ,
786
- )
787
- } )
788
- . collect :: < Option < Vec < _ > > > ( ) ?
789
- . join ( ", " ) ) ;
747
+ result. push_str ( lifetime_str) ;
790
748
result. push_str ( "> " ) ;
791
749
}
792
750
@@ -841,3 +799,22 @@ pub fn can_be_overflowed_type(context: &RewriteContext, ty: &ast::Ty, len: usize
841
799
_ => false ,
842
800
}
843
801
}
802
+
803
+ /// Returns `None` if there is no `LifetimeDef` in the given generic parameters.
804
+ fn rewrite_lifetime_param (
805
+ context : & RewriteContext ,
806
+ shape : Shape ,
807
+ generic_params : & [ ast:: GenericParam ] ,
808
+ ) -> Option < String > {
809
+ let result = generic_params
810
+ . iter ( )
811
+ . filter ( |p| p. is_lifetime_param ( ) )
812
+ . map ( |lt| lt. rewrite ( context, shape) )
813
+ . collect :: < Option < Vec < _ > > > ( ) ?
814
+ . join ( ", " ) ;
815
+ if result. is_empty ( ) {
816
+ None
817
+ } else {
818
+ Some ( result)
819
+ }
820
+ }
0 commit comments