6
6
//! them in the future to instead emit any format desired.
7
7
8
8
use std:: borrow:: Cow ;
9
+ use std:: cell:: Cell ;
9
10
use std:: fmt;
10
11
11
12
use rustc:: hir:: def_id:: DefId ;
@@ -38,8 +39,6 @@ pub struct AsyncSpace(pub hir::IsAsync);
38
39
pub struct MutableSpace ( pub clean:: Mutability ) ;
39
40
/// Wrapper struct for emitting type parameter bounds.
40
41
pub struct GenericBounds < ' a > ( pub & ' a [ clean:: GenericBound ] ) ;
41
- /// Wrapper struct for emitting a comma-separated list of items
42
- pub struct CommaSep < ' a , T > ( pub & ' a [ T ] ) ;
43
42
pub struct AbiSpace ( pub Abi ) ;
44
43
pub struct DefaultSpace ( pub bool ) ;
45
44
@@ -68,11 +67,6 @@ pub struct WhereClause<'a>{
68
67
pub end_newline : bool ,
69
68
}
70
69
71
- pub struct HRef < ' a > {
72
- did : DefId ,
73
- text : & ' a str ,
74
- }
75
-
76
70
impl < ' a > VisSpace < ' a > {
77
71
pub fn get ( self ) -> & ' a Option < clean:: Visibility > {
78
72
let VisSpace ( v) = self ; v
@@ -91,14 +85,14 @@ impl ConstnessSpace {
91
85
}
92
86
}
93
87
94
- impl < ' a , T : fmt:: Display > fmt:: Display for CommaSep < ' a , T > {
95
- fn fmt ( & self , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
96
- for ( i, item) in self . 0 . iter ( ) . enumerate ( ) {
88
+ fn comma_sep < T : fmt:: Display > ( items : & [ T ] ) -> impl fmt:: Display + ' _ {
89
+ display_fn ( move |f| {
90
+ for ( i, item) in items . iter ( ) . enumerate ( ) {
97
91
if i != 0 { write ! ( f, ", " ) ?; }
98
92
fmt:: Display :: fmt ( item, f) ?;
99
93
}
100
94
Ok ( ( ) )
101
- }
95
+ } )
102
96
}
103
97
104
98
impl < ' a > fmt:: Display for GenericBounds < ' a > {
@@ -165,9 +159,9 @@ impl fmt::Display for clean::Generics {
165
159
return Ok ( ( ) ) ;
166
160
}
167
161
if f. alternate ( ) {
168
- write ! ( f, "<{:#}>" , CommaSep ( & real_params) )
162
+ write ! ( f, "<{:#}>" , comma_sep ( & real_params) )
169
163
} else {
170
- write ! ( f, "<{}>" , CommaSep ( & real_params) )
164
+ write ! ( f, "<{}>" , comma_sep ( & real_params) )
171
165
}
172
166
}
173
167
}
@@ -265,9 +259,9 @@ impl fmt::Display for clean::PolyTrait {
265
259
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
266
260
if !self . generic_params . is_empty ( ) {
267
261
if f. alternate ( ) {
268
- write ! ( f, "for<{:#}> " , CommaSep ( & self . generic_params) ) ?;
262
+ write ! ( f, "for<{:#}> " , comma_sep ( & self . generic_params) ) ?;
269
263
} else {
270
- write ! ( f, "for<{}> " , CommaSep ( & self . generic_params) ) ?;
264
+ write ! ( f, "for<{}> " , comma_sep ( & self . generic_params) ) ?;
271
265
}
272
266
}
273
267
if f. alternate ( ) {
@@ -452,16 +446,15 @@ fn resolved_path(w: &mut fmt::Formatter<'_>, did: DefId, path: &clean::Path,
452
446
write ! ( w, "{}{:#}" , & last. name, last. args) ?;
453
447
} else {
454
448
let path = if use_absolute {
455
- match href ( did) {
456
- Some ( ( _, _, fqp) ) => {
457
- format ! ( "{}::{}" ,
458
- fqp[ ..fqp. len( ) - 1 ] . join( "::" ) ,
459
- HRef :: new( did, fqp. last( ) . unwrap( ) ) )
460
- }
461
- None => HRef :: new ( did, & last. name ) . to_string ( ) ,
449
+ if let Some ( ( _, _, fqp) ) = href ( did) {
450
+ format ! ( "{}::{}" ,
451
+ fqp[ ..fqp. len( ) - 1 ] . join( "::" ) ,
452
+ anchor( did, fqp. last( ) . unwrap( ) ) )
453
+ } else {
454
+ last. name . to_string ( )
462
455
}
463
456
} else {
464
- HRef :: new ( did, & last. name ) . to_string ( )
457
+ anchor ( did, & last. name ) . to_string ( )
465
458
} ;
466
459
write ! ( w, "{}{}" , path, last. args) ?;
467
460
}
@@ -513,35 +506,30 @@ fn primitive_link(f: &mut fmt::Formatter<'_>,
513
506
}
514
507
515
508
/// Helper to render type parameters
516
- fn tybounds ( w : & mut fmt:: Formatter < ' _ > ,
517
- param_names : & Option < Vec < clean:: GenericBound > > ) -> fmt:: Result {
518
- match * param_names {
519
- Some ( ref params) => {
520
- for param in params {
521
- write ! ( w, " + " ) ?;
522
- fmt:: Display :: fmt ( param, w) ?;
509
+ fn tybounds ( param_names : & Option < Vec < clean:: GenericBound > > ) -> impl fmt:: Display + ' _ {
510
+ display_fn ( move |f| {
511
+ match * param_names {
512
+ Some ( ref params) => {
513
+ for param in params {
514
+ write ! ( f, " + " ) ?;
515
+ fmt:: Display :: fmt ( param, f) ?;
516
+ }
517
+ Ok ( ( ) )
523
518
}
524
- Ok ( ( ) )
519
+ None => Ok ( ( ) )
525
520
}
526
- None => Ok ( ( ) )
527
- }
528
- }
529
-
530
- impl < ' a > HRef < ' a > {
531
- pub fn new ( did : DefId , text : & ' a str ) -> HRef < ' a > {
532
- HRef { did : did, text : text }
533
- }
521
+ } )
534
522
}
535
523
536
- impl < ' a > fmt:: Display for HRef < ' a > {
537
- fn fmt ( & self , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
538
- if let Some ( ( url, short_ty, fqp) ) = href ( self . did ) {
524
+ pub fn anchor ( did : DefId , text : & str ) -> impl fmt:: Display + ' _ {
525
+ display_fn ( move |f| {
526
+ if let Some ( ( url, short_ty, fqp) ) = href ( did) {
539
527
write ! ( f, r#"<a class="{}" href="{}" title="{} {}">{}</a>"# ,
540
- short_ty, url, short_ty, fqp. join( "::" ) , self . text)
528
+ short_ty, url, short_ty, fqp. join( "::" ) , text)
541
529
} else {
542
- write ! ( f, "{}" , self . text)
530
+ write ! ( f, "{}" , text)
543
531
}
544
- }
532
+ } )
545
533
}
546
534
547
535
fn fmt_type ( t : & clean:: Type , f : & mut fmt:: Formatter < ' _ > , use_absolute : bool ) -> fmt:: Result {
@@ -555,7 +543,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
555
543
}
556
544
// Paths like `T::Output` and `Self::Output` should be rendered with all segments.
557
545
resolved_path ( f, did, path, is_generic, use_absolute) ?;
558
- tybounds ( f , param_names )
546
+ fmt :: Display :: fmt ( & tybounds ( param_names ) , f )
559
547
}
560
548
clean:: Infer => write ! ( f, "_" ) ,
561
549
clean:: Primitive ( prim) => primitive_link ( f, prim, prim. as_str ( ) ) ,
@@ -564,12 +552,12 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
564
552
write ! ( f, "{}{:#}fn{:#}{:#}" ,
565
553
UnsafetySpace ( decl. unsafety) ,
566
554
AbiSpace ( decl. abi) ,
567
- CommaSep ( & decl. generic_params) ,
555
+ comma_sep ( & decl. generic_params) ,
568
556
decl. decl)
569
557
} else {
570
558
write ! ( f, "{}{}" , UnsafetySpace ( decl. unsafety) , AbiSpace ( decl. abi) ) ?;
571
559
primitive_link ( f, PrimitiveType :: Fn , "fn" ) ?;
572
- write ! ( f, "{}{}" , CommaSep ( & decl. generic_params) , decl. decl)
560
+ write ! ( f, "{}{}" , comma_sep ( & decl. generic_params) , decl. decl)
573
561
}
574
562
}
575
563
clean:: Tuple ( ref typs) => {
@@ -583,7 +571,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter<'_>, use_absolute: bool) ->
583
571
}
584
572
many => {
585
573
primitive_link ( f, PrimitiveType :: Tuple , "(" ) ?;
586
- fmt:: Display :: fmt ( & CommaSep ( many) , f) ?;
574
+ fmt:: Display :: fmt ( & comma_sep ( many) , f) ?;
587
575
primitive_link ( f, PrimitiveType :: Tuple , ")" )
588
576
}
589
577
}
@@ -1063,3 +1051,19 @@ impl fmt::Display for DefaultSpace {
1063
1051
}
1064
1052
}
1065
1053
}
1054
+
1055
+ crate fn display_fn (
1056
+ f : impl FnOnce ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result ,
1057
+ ) -> impl fmt:: Display {
1058
+ WithFormatter ( Cell :: new ( Some ( f) ) )
1059
+ }
1060
+
1061
+ struct WithFormatter < F > ( Cell < Option < F > > ) ;
1062
+
1063
+ impl < F > fmt:: Display for WithFormatter < F >
1064
+ where F : FnOnce ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result ,
1065
+ {
1066
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1067
+ ( self . 0 . take ( ) ) . unwrap ( ) ( f)
1068
+ }
1069
+ }
0 commit comments