8
8
//! not be used external to this module.
9
9
10
10
use std:: borrow:: Cow ;
11
- use std:: cell:: Cell ;
12
11
use std:: cmp:: Ordering ;
13
12
use std:: fmt:: { self , Display , Write } ;
14
13
use std:: iter:: { self , once} ;
@@ -146,36 +145,19 @@ impl Buffer {
146
145
}
147
146
}
148
147
149
- pub ( crate ) fn comma_sep < T : Display > (
150
- items : impl Iterator < Item = T > ,
151
- space_after_comma : bool ,
152
- ) -> impl Display {
153
- let items = Cell :: new ( Some ( items) ) ;
154
- fmt:: from_fn ( move |f| {
155
- for ( i, item) in items. take ( ) . unwrap ( ) . enumerate ( ) {
156
- if i != 0 {
157
- write ! ( f, ",{}" , if space_after_comma { " " } else { "" } ) ?;
158
- }
159
- item. fmt ( f) ?;
160
- }
161
- Ok ( ( ) )
162
- } )
163
- }
164
-
165
148
pub ( crate ) fn print_generic_bounds < ' a , ' tcx : ' a > (
166
149
bounds : & ' a [ clean:: GenericBound ] ,
167
150
cx : & ' a Context < ' tcx > ,
168
151
) -> impl Display + ' a + Captures < ' tcx > {
169
152
fmt:: from_fn ( move |f| {
170
153
let mut bounds_dup = FxHashSet :: default ( ) ;
171
154
172
- for ( i, bound) in bounds. iter ( ) . filter ( |b| bounds_dup. insert ( * b) ) . enumerate ( ) {
173
- if i > 0 {
174
- f. write_str ( " + " ) ?;
175
- }
176
- bound. print ( cx) . fmt ( f) ?;
177
- }
178
- Ok ( ( ) )
155
+ bounds
156
+ . iter ( )
157
+ . filter ( |b| bounds_dup. insert ( * b) )
158
+ . map ( |bound| bound. print ( cx) )
159
+ . format ( " + " )
160
+ . fmt ( f)
179
161
} )
180
162
}
181
163
@@ -190,12 +172,7 @@ impl clean::GenericParamDef {
190
172
191
173
if !outlives. is_empty ( ) {
192
174
f. write_str ( ": " ) ?;
193
- for ( i, lt) in outlives. iter ( ) . enumerate ( ) {
194
- if i != 0 {
195
- f. write_str ( " + " ) ?;
196
- }
197
- write ! ( f, "{}" , lt. print( ) ) ?;
198
- }
175
+ write ! ( f, "{}" , outlives. iter( ) . map( |lt| lt. print( ) ) . format( " + " ) ) ?;
199
176
}
200
177
201
178
Ok ( ( ) )
@@ -246,9 +223,9 @@ impl clean::Generics {
246
223
}
247
224
248
225
if f. alternate ( ) {
249
- write ! ( f, "<{:#}>" , comma_sep ( real_params. map( |g| g. print( cx) ) , true ) )
226
+ write ! ( f, "<{:#}>" , real_params. map( |g| g. print( cx) ) . format ( ", " ) )
250
227
} else {
251
- write ! ( f, "<{}>" , comma_sep ( real_params. map( |g| g. print( cx) ) , true ) )
228
+ write ! ( f, "<{}>" , real_params. map( |g| g. print( cx) ) . format ( ", " ) )
252
229
}
253
230
} )
254
231
}
@@ -318,7 +295,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
318
295
return Ok ( ( ) ) ;
319
296
}
320
297
321
- let where_preds = comma_sep ( where_predicates, false ) ;
298
+ let where_preds = where_predicates. format ( "," ) ;
322
299
let clause = if f. alternate ( ) {
323
300
if ending == Ending :: Newline {
324
301
format ! ( " where{where_preds}," )
@@ -415,12 +392,7 @@ impl clean::GenericBound {
415
392
} else {
416
393
f. write_str ( "use<" ) ?;
417
394
}
418
- for ( i, arg) in args. iter ( ) . enumerate ( ) {
419
- if i > 0 {
420
- write ! ( f, ", " ) ?;
421
- }
422
- arg. fmt ( f) ?;
423
- }
395
+ args. iter ( ) . format ( ", " ) . fmt ( f) ?;
424
396
if f. alternate ( ) { f. write_str ( ">" ) } else { f. write_str ( ">" ) }
425
397
}
426
398
} )
@@ -524,11 +496,7 @@ pub(crate) enum HrefError {
524
496
// Panics if `syms` is empty.
525
497
pub ( crate ) fn join_with_double_colon ( syms : & [ Symbol ] ) -> String {
526
498
let mut s = String :: with_capacity ( estimate_item_path_byte_length ( syms. len ( ) ) ) ;
527
- s. push_str ( syms[ 0 ] . as_str ( ) ) ;
528
- for sym in & syms[ 1 ..] {
529
- s. push_str ( "::" ) ;
530
- s. push_str ( sym. as_str ( ) ) ;
531
- }
499
+ write ! ( s, "{}" , syms. iter( ) . format( "::" ) ) . unwrap ( ) ;
532
500
s
533
501
}
534
502
@@ -572,20 +540,20 @@ fn generate_macro_def_id_path(
572
540
}
573
541
574
542
if let Some ( last) = path. last_mut ( ) {
575
- * last = Symbol :: intern ( & format ! ( "macro.{}.html" , last . as_str ( ) ) ) ;
543
+ * last = Symbol :: intern ( & format ! ( "macro.{last }.html" ) ) ;
576
544
}
577
545
578
546
let url = match cache. extern_locations [ & def_id. krate ] {
579
547
ExternalLocation :: Remote ( ref s) => {
580
548
// `ExternalLocation::Remote` always end with a `/`.
581
- format ! ( "{s}{path}" , path = path. iter( ) . map ( |p| p . as_str ( ) ) . join ( "/" ) )
549
+ format ! ( "{s}{path}" , path = path. iter( ) . format ( "/" ) )
582
550
}
583
551
ExternalLocation :: Local => {
584
552
// `root_path` always end with a `/`.
585
553
format ! (
586
554
"{root_path}{path}" ,
587
555
root_path = root_path. unwrap_or( "" ) ,
588
- path = path. iter( ) . map ( |p| p . as_str ( ) ) . join ( "/" )
556
+ path = path. iter( ) . format ( "/" )
589
557
)
590
558
}
591
559
ExternalLocation :: Unknown => {
@@ -682,9 +650,8 @@ fn make_href(
682
650
url_parts. push ( "index.html" ) ;
683
651
}
684
652
_ => {
685
- let prefix = shortty. as_str ( ) ;
686
653
let last = fqp. last ( ) . unwrap ( ) ;
687
- url_parts. push_fmt ( format_args ! ( "{prefix }.{last}.html" ) ) ;
654
+ url_parts. push_fmt ( format_args ! ( "{shortty }.{last}.html" ) ) ;
688
655
}
689
656
}
690
657
Ok ( ( url_parts. finish ( ) , shortty, fqp. to_vec ( ) ) )
@@ -950,12 +917,7 @@ fn tybounds<'a, 'tcx: 'a>(
950
917
cx : & ' a Context < ' tcx > ,
951
918
) -> impl Display + ' a + Captures < ' tcx > {
952
919
fmt:: from_fn ( move |f| {
953
- for ( i, bound) in bounds. iter ( ) . enumerate ( ) {
954
- if i > 0 {
955
- write ! ( f, " + " ) ?;
956
- }
957
- bound. print ( cx) . fmt ( f) ?;
958
- }
920
+ bounds. iter ( ) . map ( |bound| bound. print ( cx) ) . format ( " + " ) . fmt ( f) ?;
959
921
if let Some ( lt) = lt {
960
922
// We don't need to check `alternate` since we can be certain that
961
923
// the lifetime doesn't contain any characters which need escaping.
@@ -974,7 +936,7 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
974
936
if !params. is_empty ( ) {
975
937
f. write_str ( keyword) ?;
976
938
f. write_str ( if f. alternate ( ) { "<" } else { "<" } ) ?;
977
- comma_sep ( params. iter ( ) . map ( |lt| lt. print ( cx) ) , true ) . fmt ( f) ?;
939
+ params. iter ( ) . map ( |lt| lt. print ( cx) ) . format ( ", " ) . fmt ( f) ?;
978
940
f. write_str ( if f. alternate ( ) { "> " } else { "> " } ) ?;
979
941
}
980
942
Ok ( ( ) )
@@ -1025,9 +987,7 @@ fn fmt_type(
1025
987
clean:: Primitive ( clean:: PrimitiveType :: Never ) => {
1026
988
primitive_link ( f, PrimitiveType :: Never , format_args ! ( "!" ) , cx)
1027
989
}
1028
- clean:: Primitive ( prim) => {
1029
- primitive_link ( f, prim, format_args ! ( "{}" , prim. as_sym( ) . as_str( ) ) , cx)
1030
- }
990
+ clean:: Primitive ( prim) => primitive_link ( f, prim, format_args ! ( "{}" , prim. as_sym( ) ) , cx) ,
1031
991
clean:: BareFunction ( ref decl) => {
1032
992
print_higher_ranked_params_with_space ( & decl. generic_params , cx, "for" ) . fmt ( f) ?;
1033
993
decl. safety . print_with_space ( ) . fmt ( f) ?;
@@ -1067,18 +1027,13 @@ fn fmt_type(
1067
1027
primitive_link (
1068
1028
f,
1069
1029
PrimitiveType :: Tuple ,
1070
- format_args ! ( "({})" , generic_names. iter( ) . map ( |s| s . as_str ( ) ) . join ( ", " ) ) ,
1030
+ format_args ! ( "({})" , generic_names. iter( ) . format ( ", " ) ) ,
1071
1031
cx,
1072
1032
)
1073
1033
} else {
1074
- write ! ( f, "(" ) ?;
1075
- for ( i, item) in many. iter ( ) . enumerate ( ) {
1076
- if i != 0 {
1077
- write ! ( f, ", " ) ?;
1078
- }
1079
- item. print ( cx) . fmt ( f) ?;
1080
- }
1081
- write ! ( f, ")" )
1034
+ f. write_str ( "(" ) ?;
1035
+ many. iter ( ) . map ( |item| item. print ( cx) ) . format ( ", " ) . fmt ( f) ?;
1036
+ f. write_str ( ")" )
1082
1037
}
1083
1038
}
1084
1039
} ,
@@ -1407,14 +1362,16 @@ impl clean::Arguments {
1407
1362
cx : & ' a Context < ' tcx > ,
1408
1363
) -> impl Display + ' a + Captures < ' tcx > {
1409
1364
fmt:: from_fn ( move |f| {
1410
- for ( i, input) in self . values . iter ( ) . enumerate ( ) {
1411
- write ! ( f, "{}: " , input. name) ?;
1412
- input. type_ . print ( cx) . fmt ( f) ?;
1413
- if i + 1 < self . values . len ( ) {
1414
- write ! ( f, ", " ) ?;
1415
- }
1416
- }
1417
- Ok ( ( ) )
1365
+ self . values
1366
+ . iter ( )
1367
+ . map ( |input| {
1368
+ fmt:: from_fn ( |f| {
1369
+ write ! ( f, "{}: " , input. name) ?;
1370
+ input. type_ . print ( cx) . fmt ( f)
1371
+ } )
1372
+ } )
1373
+ . format ( ", " )
1374
+ . fmt ( f)
1418
1375
} )
1419
1376
}
1420
1377
}
@@ -1714,12 +1671,7 @@ impl clean::ImportSource {
1714
1671
}
1715
1672
let name = self . path . last ( ) ;
1716
1673
if let hir:: def:: Res :: PrimTy ( p) = self . path . res {
1717
- primitive_link (
1718
- f,
1719
- PrimitiveType :: from ( p) ,
1720
- format_args ! ( "{}" , name. as_str( ) ) ,
1721
- cx,
1722
- ) ?;
1674
+ primitive_link ( f, PrimitiveType :: from ( p) , format_args ! ( "{name}" ) , cx) ?;
1723
1675
} else {
1724
1676
f. write_str ( name. as_str ( ) ) ?;
1725
1677
}
0 commit comments