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} ;
15
14
16
- use itertools:: Itertools ;
17
15
use rustc_abi:: ExternAbi ;
18
16
use rustc_attr_parsing:: { ConstStability , StabilityLevel , StableSince } ;
19
17
use rustc_data_structures:: captures:: Captures ;
@@ -35,6 +33,7 @@ use crate::formats::cache::Cache;
35
33
use crate :: formats:: item_type:: ItemType ;
36
34
use crate :: html:: escape:: { Escape , EscapeBodyText } ;
37
35
use crate :: html:: render:: Context ;
36
+ use crate :: join:: Joined as _;
38
37
use crate :: passes:: collect_intra_doc_links:: UrlFragment ;
39
38
40
39
pub ( crate ) trait Print {
@@ -146,36 +145,18 @@ 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 ( move |b| bounds_dup. insert ( * b) )
158
+ . map ( |bound| bound. print ( cx) )
159
+ . joined ( " + " , f)
179
160
} )
180
161
}
181
162
@@ -190,12 +171,7 @@ impl clean::GenericParamDef {
190
171
191
172
if !outlives. is_empty ( ) {
192
173
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
- }
174
+ outlives. iter ( ) . map ( |lt| lt. print ( ) ) . joined ( " + " , f) ?; // FIXME: keep fmt options?
199
175
}
200
176
201
177
Ok ( ( ) )
@@ -245,10 +221,12 @@ impl clean::Generics {
245
221
return Ok ( ( ) ) ;
246
222
}
247
223
224
+ let real_params =
225
+ fmt:: from_fn ( |f| real_params. clone ( ) . map ( |g| g. print ( cx) ) . joined ( ", " , f) ) ;
248
226
if f. alternate ( ) {
249
- write ! ( f, "<{:#}>" , comma_sep ( real_params. map ( |g| g . print ( cx ) ) , true ) )
227
+ write ! ( f, "<{:#}>" , real_params)
250
228
} else {
251
- write ! ( f, "<{}>" , comma_sep ( real_params. map ( |g| g . print ( cx ) ) , true ) )
229
+ write ! ( f, "<{}>" , real_params)
252
230
}
253
231
} )
254
232
}
@@ -270,10 +248,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
270
248
ending : Ending ,
271
249
) -> impl Display + ' a + Captures < ' tcx > {
272
250
fmt:: from_fn ( move |f| {
273
- let mut where_predicates = gens
274
- . where_predicates
275
- . iter ( )
276
- . map ( |pred| {
251
+ if gens. where_predicates . is_empty ( ) {
252
+ return Ok ( ( ) ) ;
253
+ }
254
+
255
+ let where_predicates = || {
256
+ gens. where_predicates . iter ( ) . map ( |pred| {
277
257
fmt:: from_fn ( move |f| {
278
258
if f. alternate ( ) {
279
259
f. write_str ( " " ) ?;
@@ -312,13 +292,9 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
312
292
}
313
293
} )
314
294
} )
315
- . peekable ( ) ;
316
-
317
- if where_predicates. peek ( ) . is_none ( ) {
318
- return Ok ( ( ) ) ;
319
- }
295
+ } ;
320
296
321
- let where_preds = comma_sep ( where_predicates, false ) ;
297
+ let where_preds = fmt :: from_fn ( |f| where_predicates ( ) . joined ( "," , f ) ) ;
322
298
let clause = if f. alternate ( ) {
323
299
if ending == Ending :: Newline {
324
300
format ! ( " where{where_preds}," )
@@ -415,12 +391,7 @@ impl clean::GenericBound {
415
391
} else {
416
392
f. write_str ( "use<" ) ?;
417
393
}
418
- for ( i, arg) in args. iter ( ) . enumerate ( ) {
419
- if i > 0 {
420
- write ! ( f, ", " ) ?;
421
- }
422
- arg. fmt ( f) ?;
423
- }
394
+ args. iter ( ) . joined ( ", " , f) ?;
424
395
if f. alternate ( ) { f. write_str ( ">" ) } else { f. write_str ( ">" ) }
425
396
}
426
397
} )
@@ -524,11 +495,7 @@ pub(crate) enum HrefError {
524
495
// Panics if `syms` is empty.
525
496
pub ( crate ) fn join_with_double_colon ( syms : & [ Symbol ] ) -> String {
526
497
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
- }
498
+ write ! ( s, "{}" , fmt:: from_fn( |f| syms. iter( ) . joined( "::" , f) ) ) . unwrap ( ) ;
532
499
s
533
500
}
534
501
@@ -572,20 +539,20 @@ fn generate_macro_def_id_path(
572
539
}
573
540
574
541
if let Some ( last) = path. last_mut ( ) {
575
- * last = Symbol :: intern ( & format ! ( "macro.{}.html" , last . as_str ( ) ) ) ;
542
+ * last = Symbol :: intern ( & format ! ( "macro.{last }.html" ) ) ;
576
543
}
577
544
578
545
let url = match cache. extern_locations [ & def_id. krate ] {
579
546
ExternalLocation :: Remote ( ref s) => {
580
547
// `ExternalLocation::Remote` always end with a `/`.
581
- format ! ( "{s}{path}" , path = path . iter ( ) . map ( |p| p . as_str ( ) ) . join ( "/" ) )
548
+ format ! ( "{s}{path}" , path = fmt :: from_fn ( |f| path . iter ( ) . joined ( "/" , f ) ) )
582
549
}
583
550
ExternalLocation :: Local => {
584
551
// `root_path` always end with a `/`.
585
552
format ! (
586
553
"{root_path}{path}" ,
587
554
root_path = root_path. unwrap_or( "" ) ,
588
- path = path . iter ( ) . map ( |p| p . as_str ( ) ) . join ( "/" )
555
+ path = fmt :: from_fn ( |f| path . iter ( ) . joined ( "/" , f ) )
589
556
)
590
557
}
591
558
ExternalLocation :: Unknown => {
@@ -682,9 +649,8 @@ fn make_href(
682
649
url_parts. push ( "index.html" ) ;
683
650
}
684
651
_ => {
685
- let prefix = shortty. as_str ( ) ;
686
652
let last = fqp. last ( ) . unwrap ( ) ;
687
- url_parts. push_fmt ( format_args ! ( "{prefix }.{last}.html" ) ) ;
653
+ url_parts. push_fmt ( format_args ! ( "{shortty }.{last}.html" ) ) ;
688
654
}
689
655
}
690
656
Ok ( ( url_parts. finish ( ) , shortty, fqp. to_vec ( ) ) )
@@ -950,12 +916,7 @@ fn tybounds<'a, 'tcx: 'a>(
950
916
cx : & ' a Context < ' tcx > ,
951
917
) -> impl Display + ' a + Captures < ' tcx > {
952
918
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
- }
919
+ bounds. iter ( ) . map ( |bound| bound. print ( cx) ) . joined ( " + " , f) ?;
959
920
if let Some ( lt) = lt {
960
921
// We don't need to check `alternate` since we can be certain that
961
922
// the lifetime doesn't contain any characters which need escaping.
@@ -974,7 +935,7 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
974
935
if !params. is_empty ( ) {
975
936
f. write_str ( keyword) ?;
976
937
f. write_str ( if f. alternate ( ) { "<" } else { "<" } ) ?;
977
- comma_sep ( params. iter ( ) . map ( |lt| lt. print ( cx) ) , true ) . fmt ( f) ?;
938
+ params. iter ( ) . map ( |lt| lt. print ( cx) ) . joined ( ", " , f) ?;
978
939
f. write_str ( if f. alternate ( ) { "> " } else { "> " } ) ?;
979
940
}
980
941
Ok ( ( ) )
@@ -1025,9 +986,7 @@ fn fmt_type(
1025
986
clean:: Primitive ( clean:: PrimitiveType :: Never ) => {
1026
987
primitive_link ( f, PrimitiveType :: Never , format_args ! ( "!" ) , cx)
1027
988
}
1028
- clean:: Primitive ( prim) => {
1029
- primitive_link ( f, prim, format_args ! ( "{}" , prim. as_sym( ) . as_str( ) ) , cx)
1030
- }
989
+ clean:: Primitive ( prim) => primitive_link ( f, prim, format_args ! ( "{}" , prim. as_sym( ) ) , cx) ,
1031
990
clean:: BareFunction ( ref decl) => {
1032
991
print_higher_ranked_params_with_space ( & decl. generic_params , cx, "for" ) . fmt ( f) ?;
1033
992
decl. safety . print_with_space ( ) . fmt ( f) ?;
@@ -1067,18 +1026,16 @@ fn fmt_type(
1067
1026
primitive_link (
1068
1027
f,
1069
1028
PrimitiveType :: Tuple ,
1070
- format_args ! ( "({})" , generic_names. iter( ) . map( |s| s. as_str( ) ) . join( ", " ) ) ,
1029
+ format_args ! (
1030
+ "({})" ,
1031
+ fmt:: from_fn( |f| generic_names. iter( ) . joined( ", " , f) )
1032
+ ) ,
1071
1033
cx,
1072
1034
)
1073
1035
} 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, ")" )
1036
+ f. write_str ( "(" ) ?;
1037
+ many. iter ( ) . map ( |item| item. print ( cx) ) . joined ( ", " , f) ?;
1038
+ f. write_str ( ")" )
1082
1039
}
1083
1040
}
1084
1041
} ,
@@ -1407,14 +1364,15 @@ impl clean::Arguments {
1407
1364
cx : & ' a Context < ' tcx > ,
1408
1365
) -> impl Display + ' a + Captures < ' tcx > {
1409
1366
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 ( ( ) )
1367
+ self . values
1368
+ . iter ( )
1369
+ . map ( |input| {
1370
+ fmt:: from_fn ( |f| {
1371
+ write ! ( f, "{}: " , input. name) ?;
1372
+ input. type_ . print ( cx) . fmt ( f)
1373
+ } )
1374
+ } )
1375
+ . joined ( ", " , f)
1418
1376
} )
1419
1377
}
1420
1378
}
@@ -1723,12 +1681,7 @@ impl clean::ImportSource {
1723
1681
}
1724
1682
let name = self . path . last ( ) ;
1725
1683
if let hir:: def:: Res :: PrimTy ( p) = self . path . res {
1726
- primitive_link (
1727
- f,
1728
- PrimitiveType :: from ( p) ,
1729
- format_args ! ( "{}" , name. as_str( ) ) ,
1730
- cx,
1731
- ) ?;
1684
+ primitive_link ( f, PrimitiveType :: from ( p) , format_args ! ( "{name}" ) , cx) ?;
1732
1685
} else {
1733
1686
f. write_str ( name. as_str ( ) ) ?;
1734
1687
}
0 commit comments