@@ -174,31 +174,38 @@ impl<'a> Rewrite for SegmentParam<'a> {
174
174
SegmentParam :: Const ( const_) => const_. rewrite ( context, shape) ,
175
175
SegmentParam :: LifeTime ( lt) => lt. rewrite ( context, shape) ,
176
176
SegmentParam :: Type ( ty) => ty. rewrite ( context, shape) ,
177
- SegmentParam :: Binding ( assoc_ty_constraint) => {
178
- let mut result = match assoc_ty_constraint. kind {
179
- ast:: AssocTyConstraintKind :: Bound { .. } => {
180
- format ! ( "{}: " , rewrite_ident( context, assoc_ty_constraint. ident) )
181
- }
182
- ast:: AssocTyConstraintKind :: Equality { .. } => {
183
- match context. config . type_punctuation_density ( ) {
184
- TypeDensity :: Wide => {
185
- format ! ( "{} = " , rewrite_ident( context, assoc_ty_constraint. ident) )
186
- }
187
- TypeDensity :: Compressed => {
188
- format ! ( "{}=" , rewrite_ident( context, assoc_ty_constraint. ident) )
189
- }
190
- }
191
- }
192
- } ;
177
+ SegmentParam :: Binding ( atc) => atc. rewrite ( context, shape) ,
178
+ }
179
+ }
180
+ }
193
181
194
- let budget = shape. width . checked_sub ( result. len ( ) ) ?;
195
- let rewrite = assoc_ty_constraint
196
- . kind
197
- . rewrite ( context, Shape :: legacy ( budget, shape. indent + result. len ( ) ) ) ?;
198
- result. push_str ( & rewrite) ;
199
- Some ( result)
200
- }
182
+ impl Rewrite for ast:: AssocTyConstraint {
183
+ fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
184
+ use ast:: AssocTyConstraintKind :: { Bound , Equality } ;
185
+
186
+ let mut result = String :: with_capacity ( 128 ) ;
187
+ result. push_str ( rewrite_ident ( context, self . ident ) ) ;
188
+
189
+ if let Some ( ref gen_args) = self . gen_args {
190
+ let budget = shape. width . checked_sub ( result. len ( ) ) ?;
191
+ let shape = Shape :: legacy ( budget, shape. indent + result. len ( ) ) ;
192
+ let gen_str = rewrite_generic_args ( gen_args, context, shape, gen_args. span ( ) ) ?;
193
+ result. push_str ( & gen_str) ;
201
194
}
195
+
196
+ let infix = match ( & self . kind , context. config . type_punctuation_density ( ) ) {
197
+ ( Bound { .. } , _) => ": " ,
198
+ ( Equality { .. } , TypeDensity :: Wide ) => " = " ,
199
+ ( Equality { .. } , TypeDensity :: Compressed ) => "=" ,
200
+ } ;
201
+ result. push_str ( infix) ;
202
+
203
+ let budget = shape. width . checked_sub ( result. len ( ) ) ?;
204
+ let shape = Shape :: legacy ( budget, shape. indent + result. len ( ) ) ;
205
+ let rewrite = self . kind . rewrite ( context, shape) ?;
206
+ result. push_str ( & rewrite) ;
207
+
208
+ Some ( result)
202
209
}
203
210
}
204
211
@@ -240,21 +247,9 @@ fn rewrite_segment(
240
247
} ;
241
248
242
249
if let Some ( ref args) = segment. args {
250
+ let generics_str = rewrite_generic_args ( args, context, shape, mk_sp ( * span_lo, span_hi) ) ?;
243
251
match * * args {
244
252
ast:: GenericArgs :: AngleBracketed ( ref data) if !data. args . is_empty ( ) => {
245
- let param_list = data
246
- . args
247
- . iter ( )
248
- . map ( |x| match x {
249
- ast:: AngleBracketedArg :: Arg ( generic_arg) => {
250
- SegmentParam :: from_generic_arg ( generic_arg)
251
- }
252
- ast:: AngleBracketedArg :: Constraint ( constraint) => {
253
- SegmentParam :: Binding ( constraint)
254
- }
255
- } )
256
- . collect :: < Vec < _ > > ( ) ;
257
-
258
253
// HACK: squeeze out the span between the identifier and the parameters.
259
254
// The hack is requried so that we don't remove the separator inside macro calls.
260
255
// This does not work in the presence of comment, hoping that people are
@@ -270,33 +265,14 @@ fn rewrite_segment(
270
265
} ;
271
266
result. push_str ( separator) ;
272
267
273
- let generics_str = overflow:: rewrite_with_angle_brackets (
274
- context,
275
- "" ,
276
- param_list. iter ( ) ,
277
- shape,
278
- mk_sp ( * span_lo, span_hi) ,
279
- ) ?;
280
-
281
268
// Update position of last bracket.
282
269
* span_lo = context
283
270
. snippet_provider
284
271
. span_after ( mk_sp ( * span_lo, span_hi) , "<" ) ;
285
-
286
- result. push_str ( & generics_str)
287
- }
288
- ast:: GenericArgs :: Parenthesized ( ref data) => {
289
- result. push_str ( & format_function_type (
290
- data. inputs . iter ( ) . map ( |x| & * * x) ,
291
- & data. output ,
292
- false ,
293
- data. span ,
294
- context,
295
- shape,
296
- ) ?) ;
297
272
}
298
273
_ => ( ) ,
299
274
}
275
+ result. push_str ( & generics_str)
300
276
}
301
277
302
278
Some ( result)
@@ -489,6 +465,41 @@ impl Rewrite for ast::GenericArg {
489
465
}
490
466
}
491
467
468
+ fn rewrite_generic_args (
469
+ gen_args : & ast:: GenericArgs ,
470
+ context : & RewriteContext < ' _ > ,
471
+ shape : Shape ,
472
+ span : Span ,
473
+ ) -> Option < String > {
474
+ match gen_args {
475
+ ast:: GenericArgs :: AngleBracketed ( ref data) if !data. args . is_empty ( ) => {
476
+ let args = data
477
+ . args
478
+ . iter ( )
479
+ . map ( |x| match x {
480
+ ast:: AngleBracketedArg :: Arg ( generic_arg) => {
481
+ SegmentParam :: from_generic_arg ( generic_arg)
482
+ }
483
+ ast:: AngleBracketedArg :: Constraint ( constraint) => {
484
+ SegmentParam :: Binding ( constraint)
485
+ }
486
+ } )
487
+ . collect :: < Vec < _ > > ( ) ;
488
+
489
+ overflow:: rewrite_with_angle_brackets ( context, "" , args. iter ( ) , shape, span)
490
+ }
491
+ ast:: GenericArgs :: Parenthesized ( ref data) => format_function_type (
492
+ data. inputs . iter ( ) . map ( |x| & * * x) ,
493
+ & data. output ,
494
+ false ,
495
+ data. span ,
496
+ context,
497
+ shape,
498
+ ) ,
499
+ _ => Some ( "" . to_owned ( ) ) ,
500
+ }
501
+ }
502
+
492
503
fn rewrite_bounded_lifetime (
493
504
lt : & ast:: Lifetime ,
494
505
bounds : & [ ast:: GenericBound ] ,
0 commit comments