@@ -168,11 +168,15 @@ impl<'a> Spanned for SegmentParam<'a> {
168
168
169
169
impl < ' a > Rewrite for SegmentParam < ' a > {
170
170
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
171
+ self . rewrite_result ( context, shape) . ok ( )
172
+ }
173
+
174
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
171
175
match * self {
172
- SegmentParam :: Const ( const_) => const_. rewrite ( context, shape) ,
173
- SegmentParam :: LifeTime ( lt) => lt. rewrite ( context, shape) ,
174
- SegmentParam :: Type ( ty) => ty. rewrite ( context, shape) ,
175
- SegmentParam :: Binding ( atc) => atc. rewrite ( context, shape) ,
176
+ SegmentParam :: Const ( const_) => const_. rewrite_result ( context, shape) ,
177
+ SegmentParam :: LifeTime ( lt) => lt. rewrite_result ( context, shape) ,
178
+ SegmentParam :: Type ( ty) => ty. rewrite_result ( context, shape) ,
179
+ SegmentParam :: Binding ( atc) => atc. rewrite_result ( context, shape) ,
176
180
}
177
181
}
178
182
}
@@ -542,7 +546,7 @@ fn rewrite_bounded_lifetime(
542
546
"{}{}{}" ,
543
547
result,
544
548
colon,
545
- join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) ?
549
+ join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) . ok ( ) ?
546
550
) ;
547
551
Some ( result)
548
552
}
@@ -605,8 +609,12 @@ impl Rewrite for ast::GenericBound {
605
609
606
610
impl Rewrite for ast:: GenericBounds {
607
611
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
612
+ self . rewrite_result ( context, shape) . ok ( )
613
+ }
614
+
615
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
608
616
if self . is_empty ( ) {
609
- return Some ( String :: new ( ) ) ;
617
+ return Ok ( String :: new ( ) ) ;
610
618
}
611
619
612
620
join_bounds ( context, shape, self , true )
@@ -615,8 +623,15 @@ impl Rewrite for ast::GenericBounds {
615
623
616
624
impl Rewrite for ast:: GenericParam {
617
625
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
626
+ self . rewrite_result ( context, shape) . ok ( )
627
+ }
628
+
629
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
618
630
// FIXME: If there are more than one attributes, this will force multiline.
619
- let mut result = self . attrs . rewrite ( context, shape) . unwrap_or ( String :: new ( ) ) ;
631
+ let mut result = self
632
+ . attrs
633
+ . rewrite_result ( context, shape)
634
+ . unwrap_or ( String :: new ( ) ) ;
620
635
let has_attrs = !result. is_empty ( ) ;
621
636
622
637
let mut param = String :: with_capacity ( 128 ) ;
@@ -630,15 +645,19 @@ impl Rewrite for ast::GenericParam {
630
645
param. push_str ( "const " ) ;
631
646
param. push_str ( rewrite_ident ( context, self . ident ) ) ;
632
647
param. push_str ( ": " ) ;
633
- param. push_str ( & ty. rewrite ( context, shape) ?) ;
648
+ param. push_str ( & ty. rewrite_result ( context, shape) ?) ;
634
649
if let Some ( default) = default {
635
650
let eq_str = match context. config . type_punctuation_density ( ) {
636
651
TypeDensity :: Compressed => "=" ,
637
652
TypeDensity :: Wide => " = " ,
638
653
} ;
639
654
param. push_str ( eq_str) ;
640
- let budget = shape. width . checked_sub ( param. len ( ) ) ?;
641
- let rewrite = default. rewrite ( context, Shape :: legacy ( budget, shape. indent ) ) ?;
655
+ let budget = shape
656
+ . width
657
+ . checked_sub ( param. len ( ) )
658
+ . max_width_error ( shape. width , self . span ( ) ) ?;
659
+ let rewrite =
660
+ default. rewrite_result ( context, Shape :: legacy ( budget, shape. indent ) ) ?;
642
661
param. push_str ( & rewrite) ;
643
662
}
644
663
kw_span. lo ( )
@@ -649,7 +668,7 @@ impl Rewrite for ast::GenericParam {
649
668
650
669
if !self . bounds . is_empty ( ) {
651
670
param. push_str ( type_bound_colon ( context) ) ;
652
- param. push_str ( & self . bounds . rewrite ( context, shape) ?)
671
+ param. push_str ( & self . bounds . rewrite_result ( context, shape) ?)
653
672
}
654
673
if let ast:: GenericParamKind :: Type {
655
674
default : Some ( ref def) ,
@@ -660,9 +679,12 @@ impl Rewrite for ast::GenericParam {
660
679
TypeDensity :: Wide => " = " ,
661
680
} ;
662
681
param. push_str ( eq_str) ;
663
- let budget = shape. width . checked_sub ( param. len ( ) ) ?;
682
+ let budget = shape
683
+ . width
684
+ . checked_sub ( param. len ( ) )
685
+ . max_width_error ( shape. width , self . span ( ) ) ?;
664
686
let rewrite =
665
- def. rewrite ( context, Shape :: legacy ( budget, shape. indent + param. len ( ) ) ) ?;
687
+ def. rewrite_result ( context, Shape :: legacy ( budget, shape. indent + param. len ( ) ) ) ?;
666
688
param. push_str ( & rewrite) ;
667
689
}
668
690
@@ -676,7 +698,8 @@ impl Rewrite for ast::GenericParam {
676
698
mk_sp ( last_attr. span . hi ( ) , param_start) ,
677
699
shape,
678
700
!last_attr. is_doc_comment ( ) ,
679
- ) ?;
701
+ )
702
+ . unknown_error ( ) ?;
680
703
} else {
681
704
// When rewriting generic params, an extra newline should be put
682
705
// if the attributes end with a doc comment
@@ -688,7 +711,7 @@ impl Rewrite for ast::GenericParam {
688
711
result. push_str ( & param) ;
689
712
}
690
713
691
- Some ( result)
714
+ Ok ( result)
692
715
}
693
716
}
694
717
@@ -924,7 +947,7 @@ impl Rewrite for ast::Ty {
924
947
let rw = if context. config . version ( ) == Version :: One {
925
948
it. rewrite_result ( context, shape)
926
949
} else {
927
- join_bounds ( context, shape, it, false ) . unknown_error ( )
950
+ join_bounds ( context, shape, it, false )
928
951
} ;
929
952
rw. map ( |it_str| {
930
953
let space = if it_str. is_empty ( ) { "" } else { " " } ;
@@ -1024,7 +1047,7 @@ fn join_bounds(
1024
1047
shape : Shape ,
1025
1048
items : & [ ast:: GenericBound ] ,
1026
1049
need_indent : bool ,
1027
- ) -> Option < String > {
1050
+ ) -> RewriteResult {
1028
1051
join_bounds_inner ( context, shape, items, need_indent, false )
1029
1052
}
1030
1053
@@ -1034,7 +1057,7 @@ fn join_bounds_inner(
1034
1057
items : & [ ast:: GenericBound ] ,
1035
1058
need_indent : bool ,
1036
1059
force_newline : bool ,
1037
- ) -> Option < String > {
1060
+ ) -> RewriteResult {
1038
1061
debug_assert ! ( !items. is_empty( ) ) ;
1039
1062
1040
1063
let generic_bounds_in_order = is_generic_bounds_in_order ( items) ;
@@ -1129,16 +1152,17 @@ fn join_bounds_inner(
1129
1152
} ;
1130
1153
1131
1154
let ( extendable, trailing_str) = if i == 0 {
1132
- let bound_str = item. rewrite ( context, shape) ?;
1155
+ let bound_str = item. rewrite_result ( context, shape) ?;
1133
1156
( is_bound_extendable ( & bound_str, item) , bound_str)
1134
1157
} else {
1135
- let bound_str = & item. rewrite ( context, shape) ?;
1158
+ let bound_str = & item. rewrite_result ( context, shape) ?;
1136
1159
match leading_span {
1137
1160
Some ( ls) if has_leading_comment => (
1138
1161
is_bound_extendable ( bound_str, item) ,
1139
1162
combine_strs_with_missing_comments (
1140
1163
context, joiner, bound_str, ls, shape, true ,
1141
- ) ?,
1164
+ )
1165
+ . unknown_error ( ) ?,
1142
1166
) ,
1143
1167
_ => (
1144
1168
is_bound_extendable ( bound_str, item) ,
@@ -1155,8 +1179,9 @@ fn join_bounds_inner(
1155
1179
shape,
1156
1180
true ,
1157
1181
)
1182
+ . unknown_error ( )
1158
1183
. map ( |v| ( v, trailing_span, extendable) ) ,
1159
- _ => Some ( ( strs + & trailing_str, trailing_span, extendable) ) ,
1184
+ _ => Ok ( ( strs + & trailing_str, trailing_span, extendable) ) ,
1160
1185
}
1161
1186
} ,
1162
1187
) ?;
@@ -1181,7 +1206,7 @@ fn join_bounds_inner(
1181
1206
if retry_with_force_newline {
1182
1207
join_bounds_inner ( context, shape, items, need_indent, true )
1183
1208
} else {
1184
- Some ( result. 0 )
1209
+ Ok ( result. 0 )
1185
1210
}
1186
1211
}
1187
1212
0 commit comments