@@ -27,8 +27,8 @@ use rewrite::{Rewrite, RewriteContext};
27
27
use shape:: Shape ;
28
28
use spanned:: Spanned ;
29
29
use utils:: {
30
- colon_spaces, extra_offset, first_line_width, format_abi, format_mutability, last_line_width ,
31
- mk_sp, rewrite_ident,
30
+ colon_spaces, extra_offset, first_line_width, format_abi, format_mutability,
31
+ last_line_extendable , last_line_width , mk_sp, rewrite_ident,
32
32
} ;
33
33
34
34
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
@@ -733,15 +733,28 @@ fn rewrite_bare_fn(
733
733
Some ( result)
734
734
}
735
735
736
- fn join_bounds < T > (
736
+ fn is_generic_bounds_in_order ( generic_bounds : & [ ast:: GenericBound ] ) -> bool {
737
+ let is_trait = |b : & ast:: GenericBound | match b {
738
+ ast:: GenericBound :: Outlives ( ..) => false ,
739
+ ast:: GenericBound :: Trait ( ..) => true ,
740
+ } ;
741
+ let is_lifetime = |b : & ast:: GenericBound | !is_trait ( b) ;
742
+ let last_trait_index = generic_bounds. iter ( ) . rposition ( is_trait) ;
743
+ let first_lifetime_index = generic_bounds. iter ( ) . position ( is_lifetime) ;
744
+ match ( last_trait_index, first_lifetime_index) {
745
+ ( Some ( last_trait_index) , Some ( first_lifetime_index) ) => {
746
+ last_trait_index < first_lifetime_index
747
+ }
748
+ _ => true ,
749
+ }
750
+ }
751
+
752
+ fn join_bounds (
737
753
context : & RewriteContext ,
738
754
shape : Shape ,
739
- items : & [ T ] ,
755
+ items : & [ ast :: GenericBound ] ,
740
756
need_indent : bool ,
741
- ) -> Option < String >
742
- where
743
- T : Rewrite ,
744
- {
757
+ ) -> Option < String > {
745
758
// Try to join types in a single line
746
759
let joiner = match context. config . type_punctuation_density ( ) {
747
760
TypeDensity :: Compressed => "+" ,
@@ -752,7 +765,7 @@ where
752
765
. map ( |item| item. rewrite ( context, shape) )
753
766
. collect :: < Option < Vec < _ > > > ( ) ?;
754
767
let result = type_strs. join ( joiner) ;
755
- if items. len ( ) = = 1 || ( !result. contains ( '\n' ) && result. len ( ) <= shape. width ) {
768
+ if items. len ( ) < = 1 || ( !result. contains ( '\n' ) && result. len ( ) <= shape. width ) {
756
769
return Some ( result) ;
757
770
}
758
771
@@ -769,8 +782,26 @@ where
769
782
( type_strs, shape. indent )
770
783
} ;
771
784
772
- let joiner = format ! ( "{}+ " , offset. to_string_with_newline( context. config) ) ;
773
- Some ( type_strs. join ( & joiner) )
785
+ let is_bound_extendable = |s : & str , b : & ast:: GenericBound | match b {
786
+ ast:: GenericBound :: Outlives ( ..) => true ,
787
+ ast:: GenericBound :: Trait ( ..) => last_line_extendable ( s) ,
788
+ } ;
789
+ let mut result = String :: with_capacity ( 128 ) ;
790
+ result. push_str ( & type_strs[ 0 ] ) ;
791
+ let mut can_be_put_on_the_same_line = is_bound_extendable ( & result, & items[ 0 ] ) ;
792
+ let generic_bounds_in_order = is_generic_bounds_in_order ( items) ;
793
+ for ( bound, bound_str) in items[ 1 ..] . iter ( ) . zip ( type_strs[ 1 ..] . iter ( ) ) {
794
+ if generic_bounds_in_order && can_be_put_on_the_same_line {
795
+ result. push_str ( joiner) ;
796
+ } else {
797
+ result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
798
+ result. push_str ( "+ " ) ;
799
+ }
800
+ result. push_str ( bound_str) ;
801
+ can_be_put_on_the_same_line = is_bound_extendable ( bound_str, bound) ;
802
+ }
803
+
804
+ Some ( result)
774
805
}
775
806
776
807
pub fn can_be_overflowed_type ( context : & RewriteContext , ty : & ast:: Ty , len : usize ) -> bool {
0 commit comments