@@ -126,11 +126,14 @@ impl Buffer {
126
126
}
127
127
}
128
128
129
- fn comma_sep < T : fmt:: Display > ( items : impl Iterator < Item = T > ) -> impl fmt:: Display {
129
+ fn comma_sep < T : fmt:: Display > (
130
+ items : impl Iterator < Item = T > ,
131
+ space_after_comma : bool ,
132
+ ) -> impl fmt:: Display {
130
133
display_fn ( move |f| {
131
134
for ( i, item) in items. enumerate ( ) {
132
135
if i != 0 {
133
- write ! ( f, ", " ) ?;
136
+ write ! ( f, ",{}" , if space_after_comma { " " } else { "" } ) ?;
134
137
}
135
138
fmt:: Display :: fmt ( & item, f) ?;
136
139
}
@@ -231,9 +234,9 @@ impl clean::Generics {
231
234
}
232
235
233
236
if f. alternate ( ) {
234
- write ! ( f, "<{:#}>" , comma_sep( real_params. map( |g| g. print( cx) ) ) )
237
+ write ! ( f, "<{:#}>" , comma_sep( real_params. map( |g| g. print( cx) ) , true ) )
235
238
} else {
236
- write ! ( f, "<{}>" , comma_sep( real_params. map( |g| g. print( cx) ) ) )
239
+ write ! ( f, "<{}>" , comma_sep( real_params. map( |g| g. print( cx) ) , true ) )
237
240
}
238
241
} )
239
242
}
@@ -249,10 +252,80 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
249
252
end_newline : bool ,
250
253
) -> impl fmt:: Display + ' a + Captures < ' tcx > {
251
254
display_fn ( move |f| {
252
- if gens. where_predicates . is_empty ( ) {
255
+ let mut where_predicates = gens. where_predicates . iter ( ) . filter ( |pred| {
256
+ !matches ! ( pred, clean:: WherePredicate :: BoundPredicate { bounds, .. } if bounds. is_empty( ) )
257
+ } ) . map ( |pred| {
258
+ display_fn ( move |f| {
259
+ if f. alternate ( ) {
260
+ f. write_str ( " " ) ?;
261
+ } else {
262
+ f. write_str ( "<br>" ) ?;
263
+ }
264
+
265
+ match pred {
266
+ clean:: WherePredicate :: BoundPredicate { ty, bounds, bound_params } => {
267
+ let bounds = bounds;
268
+ let for_prefix = match bound_params. len ( ) {
269
+ 0 => String :: new ( ) ,
270
+ _ if f. alternate ( ) => {
271
+ format ! (
272
+ "for<{:#}> " ,
273
+ comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) , true )
274
+ )
275
+ }
276
+ _ => format ! (
277
+ "for<{}> " ,
278
+ comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) , true )
279
+ ) ,
280
+ } ;
281
+
282
+ if f. alternate ( ) {
283
+ write ! (
284
+ f,
285
+ "{}{:#}: {:#}" ,
286
+ for_prefix,
287
+ ty. print( cx) ,
288
+ print_generic_bounds( bounds, cx)
289
+ )
290
+ } else {
291
+ write ! (
292
+ f,
293
+ "{}{}: {}" ,
294
+ for_prefix,
295
+ ty. print( cx) ,
296
+ print_generic_bounds( bounds, cx)
297
+ )
298
+ }
299
+ }
300
+ clean:: WherePredicate :: RegionPredicate { lifetime, bounds } => {
301
+ write ! (
302
+ f,
303
+ "{}: {}" ,
304
+ lifetime. print( ) ,
305
+ bounds
306
+ . iter( )
307
+ . map( |b| b. print( cx) . to_string( ) )
308
+ . collect:: <Vec <_>>( )
309
+ . join( " + " )
310
+ )
311
+ }
312
+ clean:: WherePredicate :: EqPredicate { lhs, rhs } => {
313
+ if f. alternate ( ) {
314
+ write ! ( f, "{:#} == {:#}" , lhs. print( cx) , rhs. print( cx) , )
315
+ } else {
316
+ write ! ( f, "{} == {}" , lhs. print( cx) , rhs. print( cx) , )
317
+ }
318
+ }
319
+ }
320
+ } )
321
+ } ) . peekable ( ) ;
322
+
323
+ if where_predicates. peek ( ) . is_none ( ) {
253
324
return Ok ( ( ) ) ;
254
325
}
326
+
255
327
let mut clause = String :: new ( ) ;
328
+
256
329
if f. alternate ( ) {
257
330
clause. push_str ( " where" ) ;
258
331
} else {
@@ -263,82 +336,7 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
263
336
}
264
337
}
265
338
266
- #[ derive( Clone , Copy ) ]
267
- enum Print < ' a > {
268
- Predicate ( & ' a clean:: WherePredicate ) ,
269
- Comma ,
270
- }
271
-
272
- for pred in gens. where_predicates . iter ( ) . filter ( |pred| {
273
- !matches ! ( pred, clean:: WherePredicate :: BoundPredicate { bounds, .. } if bounds. is_empty( ) )
274
- } ) . map ( Print :: Predicate ) . intersperse ( Print :: Comma ) {
275
- let pred = match pred {
276
- Print :: Predicate ( pred) => pred,
277
- Print :: Comma => {
278
- clause. push ( ',' ) ;
279
- continue ;
280
- }
281
- } ;
282
-
283
- if f. alternate ( ) {
284
- clause. push ( ' ' ) ;
285
- } else {
286
- clause. push_str ( "<br>" ) ;
287
- }
288
-
289
- match pred {
290
- clean:: WherePredicate :: BoundPredicate { ty, bounds, bound_params } => {
291
- let bounds = bounds;
292
- let for_prefix = match bound_params. len ( ) {
293
- 0 => String :: new ( ) ,
294
- _ if f. alternate ( ) => {
295
- format ! (
296
- "for<{:#}> " ,
297
- comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) )
298
- )
299
- }
300
- _ => format ! (
301
- "for<{}> " ,
302
- comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) )
303
- ) ,
304
- } ;
305
-
306
- if f. alternate ( ) {
307
- clause. push_str ( & format ! (
308
- "{}{:#}: {:#}" ,
309
- for_prefix,
310
- ty. print( cx) ,
311
- print_generic_bounds( bounds, cx)
312
- ) ) ;
313
- } else {
314
- clause. push_str ( & format ! (
315
- "{}{}: {}" ,
316
- for_prefix,
317
- ty. print( cx) ,
318
- print_generic_bounds( bounds, cx)
319
- ) ) ;
320
- }
321
- }
322
- clean:: WherePredicate :: RegionPredicate { lifetime, bounds } => {
323
- clause. push_str ( & format ! (
324
- "{}: {}" ,
325
- lifetime. print( ) ,
326
- bounds
327
- . iter( )
328
- . map( |b| b. print( cx) . to_string( ) )
329
- . collect:: <Vec <_>>( )
330
- . join( " + " )
331
- ) ) ;
332
- }
333
- clean:: WherePredicate :: EqPredicate { lhs, rhs } => {
334
- if f. alternate ( ) {
335
- clause. push_str ( & format ! ( "{:#} == {:#}" , lhs. print( cx) , rhs. print( cx) , ) ) ;
336
- } else {
337
- clause. push_str ( & format ! ( "{} == {}" , lhs. print( cx) , rhs. print( cx) , ) ) ;
338
- }
339
- }
340
- }
341
- }
339
+ clause. push_str ( & comma_sep ( where_predicates, false ) . to_string ( ) ) ;
342
340
343
341
if end_newline {
344
342
clause. push ( ',' ) ;
@@ -391,13 +389,13 @@ impl clean::PolyTrait {
391
389
write ! (
392
390
f,
393
391
"for<{:#}> " ,
394
- comma_sep( self . generic_params. iter( ) . map( |g| g. print( cx) ) )
392
+ comma_sep( self . generic_params. iter( ) . map( |g| g. print( cx) ) , true )
395
393
) ?;
396
394
} else {
397
395
write ! (
398
396
f,
399
397
"for<{}> " ,
400
- comma_sep( self . generic_params. iter( ) . map( |g| g. print( cx) ) )
398
+ comma_sep( self . generic_params. iter( ) . map( |g| g. print( cx) ) , true )
401
399
) ?;
402
400
}
403
401
}
@@ -1108,7 +1106,7 @@ impl clean::BareFunctionDecl {
1108
1106
write ! (
1109
1107
f,
1110
1108
"for<{}> " ,
1111
- comma_sep( self . generic_params. iter( ) . map( |g| g. print( cx) ) )
1109
+ comma_sep( self . generic_params. iter( ) . map( |g| g. print( cx) ) , true )
1112
1110
)
1113
1111
} else {
1114
1112
Ok ( ( ) )
0 commit comments