@@ -150,8 +150,9 @@ pub(crate) fn comma_sep<T: Display>(
150
150
items : impl Iterator < Item = T > ,
151
151
space_after_comma : bool ,
152
152
) -> impl Display {
153
- display_fn ( move |f| {
154
- for ( i, item) in items. enumerate ( ) {
153
+ let items = Cell :: new ( Some ( items) ) ;
154
+ fmt:: from_fn ( move |f| {
155
+ for ( i, item) in items. take ( ) . unwrap ( ) . enumerate ( ) {
155
156
if i != 0 {
156
157
write ! ( f, ",{}" , if space_after_comma { " " } else { "" } ) ?;
157
158
}
@@ -165,7 +166,7 @@ pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
165
166
bounds : & ' a [ clean:: GenericBound ] ,
166
167
cx : & ' a Context < ' tcx > ,
167
168
) -> impl Display + ' a + Captures < ' tcx > {
168
- display_fn ( move |f| {
169
+ fmt :: from_fn ( move |f| {
169
170
let mut bounds_dup = FxHashSet :: default ( ) ;
170
171
171
172
for ( i, bound) in bounds. iter ( ) . filter ( |b| bounds_dup. insert ( * b) ) . enumerate ( ) {
@@ -183,7 +184,7 @@ impl clean::GenericParamDef {
183
184
& ' a self ,
184
185
cx : & ' a Context < ' tcx > ,
185
186
) -> impl Display + ' a + Captures < ' tcx > {
186
- display_fn ( move |f| match & self . kind {
187
+ fmt :: from_fn ( move |f| match & self . kind {
187
188
clean:: GenericParamDefKind :: Lifetime { outlives } => {
188
189
write ! ( f, "{}" , self . name) ?;
189
190
@@ -238,7 +239,7 @@ impl clean::Generics {
238
239
& ' a self ,
239
240
cx : & ' a Context < ' tcx > ,
240
241
) -> impl Display + ' a + Captures < ' tcx > {
241
- display_fn ( move |f| {
242
+ fmt :: from_fn ( move |f| {
242
243
let mut real_params = self . params . iter ( ) . filter ( |p| !p. is_synthetic_param ( ) ) . peekable ( ) ;
243
244
if real_params. peek ( ) . is_none ( ) {
244
245
return Ok ( ( ) ) ;
@@ -268,12 +269,12 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
268
269
indent : usize ,
269
270
ending : Ending ,
270
271
) -> impl Display + ' a + Captures < ' tcx > {
271
- display_fn ( move |f| {
272
+ fmt :: from_fn ( move |f| {
272
273
let mut where_predicates = gens
273
274
. where_predicates
274
275
. iter ( )
275
276
. map ( |pred| {
276
- display_fn ( move |f| {
277
+ fmt :: from_fn ( move |f| {
277
278
if f. alternate ( ) {
278
279
f. write_str ( " " ) ?;
279
280
} else {
@@ -376,17 +377,15 @@ impl clean::Lifetime {
376
377
impl clean:: ConstantKind {
377
378
pub ( crate ) fn print ( & self , tcx : TyCtxt < ' _ > ) -> impl Display + ' _ {
378
379
let expr = self . expr ( tcx) ;
379
- display_fn (
380
- move |f| {
381
- if f. alternate ( ) { f. write_str ( & expr) } else { write ! ( f, "{}" , Escape ( & expr) ) }
382
- } ,
383
- )
380
+ fmt:: from_fn ( move |f| {
381
+ if f. alternate ( ) { f. write_str ( & expr) } else { write ! ( f, "{}" , Escape ( & expr) ) }
382
+ } )
384
383
}
385
384
}
386
385
387
386
impl clean:: PolyTrait {
388
387
fn print < ' a , ' tcx : ' a > ( & ' a self , cx : & ' a Context < ' tcx > ) -> impl Display + ' a + Captures < ' tcx > {
389
- display_fn ( move |f| {
388
+ fmt :: from_fn ( move |f| {
390
389
print_higher_ranked_params_with_space ( & self . generic_params , cx, "for" ) . fmt ( f) ?;
391
390
self . trait_ . print ( cx) . fmt ( f)
392
391
} )
@@ -398,7 +397,7 @@ impl clean::GenericBound {
398
397
& ' a self ,
399
398
cx : & ' a Context < ' tcx > ,
400
399
) -> impl Display + ' a + Captures < ' tcx > {
401
- display_fn ( move |f| match self {
400
+ fmt :: from_fn ( move |f| match self {
402
401
clean:: GenericBound :: Outlives ( lt) => write ! ( f, "{}" , lt. print( ) ) ,
403
402
clean:: GenericBound :: TraitBound ( ty, modifiers) => {
404
403
// `const` and `~const` trait bounds are experimental; don't render them.
@@ -430,7 +429,7 @@ impl clean::GenericBound {
430
429
431
430
impl clean:: GenericArgs {
432
431
fn print < ' a , ' tcx : ' a > ( & ' a self , cx : & ' a Context < ' tcx > ) -> impl Display + ' a + Captures < ' tcx > {
433
- display_fn ( move |f| {
432
+ fmt :: from_fn ( move |f| {
434
433
match self {
435
434
clean:: GenericArgs :: AngleBracketed { args, constraints } => {
436
435
if !args. is_empty ( ) || !constraints. is_empty ( ) {
@@ -950,7 +949,7 @@ fn tybounds<'a, 'tcx: 'a>(
950
949
lt : & ' a Option < clean:: Lifetime > ,
951
950
cx : & ' a Context < ' tcx > ,
952
951
) -> impl Display + ' a + Captures < ' tcx > {
953
- display_fn ( move |f| {
952
+ fmt :: from_fn ( move |f| {
954
953
for ( i, bound) in bounds. iter ( ) . enumerate ( ) {
955
954
if i > 0 {
956
955
write ! ( f, " + " ) ?;
@@ -971,7 +970,7 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
971
970
cx : & ' a Context < ' tcx > ,
972
971
keyword : & ' static str ,
973
972
) -> impl Display + ' a + Captures < ' tcx > {
974
- display_fn ( move |f| {
973
+ fmt :: from_fn ( move |f| {
975
974
if !params. is_empty ( ) {
976
975
f. write_str ( keyword) ?;
977
976
f. write_str ( if f. alternate ( ) { "<" } else { "<" } ) ?;
@@ -982,13 +981,13 @@ fn print_higher_ranked_params_with_space<'a, 'tcx: 'a>(
982
981
} )
983
982
}
984
983
985
- pub ( crate ) fn anchor < ' a , ' cx : ' a > (
984
+ pub ( crate ) fn anchor < ' a : ' cx , ' cx > (
986
985
did : DefId ,
987
986
text : Symbol ,
988
- cx : & ' cx Context < ' _ > ,
989
- ) -> impl Display + ' a {
990
- let parts = href ( did , cx ) ;
991
- display_fn ( move |f| {
987
+ cx : & ' cx Context < ' a > ,
988
+ ) -> impl Display + Captures < ' a > + ' cx {
989
+ fmt :: from_fn ( move |f| {
990
+ let parts = href ( did , cx ) ;
992
991
if let Ok ( ( url, short_ty, fqp) ) = parts {
993
992
write ! (
994
993
f,
@@ -1150,7 +1149,7 @@ fn fmt_type(
1150
1149
}
1151
1150
}
1152
1151
clean:: BorrowedRef { lifetime : ref l, mutability, type_ : ref ty } => {
1153
- let lt = display_fn ( |f| match l {
1152
+ let lt = fmt :: from_fn ( |f| match l {
1154
1153
Some ( l) => write ! ( f, "{} " , l. print( ) ) ,
1155
1154
_ => Ok ( ( ) ) ,
1156
1155
} ) ;
@@ -1270,7 +1269,7 @@ impl clean::Type {
1270
1269
& ' a self ,
1271
1270
cx : & ' a Context < ' tcx > ,
1272
1271
) -> impl Display + ' b + Captures < ' tcx > {
1273
- display_fn ( move |f| fmt_type ( self , f, false , cx) )
1272
+ fmt :: from_fn ( move |f| fmt_type ( self , f, false , cx) )
1274
1273
}
1275
1274
}
1276
1275
@@ -1279,7 +1278,7 @@ impl clean::Path {
1279
1278
& ' a self ,
1280
1279
cx : & ' a Context < ' tcx > ,
1281
1280
) -> impl Display + ' b + Captures < ' tcx > {
1282
- display_fn ( move |f| resolved_path ( f, self . def_id ( ) , self , false , false , cx) )
1281
+ fmt :: from_fn ( move |f| resolved_path ( f, self . def_id ( ) , self , false , false , cx) )
1283
1282
}
1284
1283
}
1285
1284
@@ -1289,7 +1288,7 @@ impl clean::Impl {
1289
1288
use_absolute : bool ,
1290
1289
cx : & ' a Context < ' tcx > ,
1291
1290
) -> impl Display + ' a + Captures < ' tcx > {
1292
- display_fn ( move |f| {
1291
+ fmt :: from_fn ( move |f| {
1293
1292
f. write_str ( "impl" ) ?;
1294
1293
self . generics . print ( cx) . fmt ( f) ?;
1295
1294
f. write_str ( " " ) ?;
@@ -1407,7 +1406,7 @@ impl clean::Arguments {
1407
1406
& ' a self ,
1408
1407
cx : & ' a Context < ' tcx > ,
1409
1408
) -> impl Display + ' a + Captures < ' tcx > {
1410
- display_fn ( move |f| {
1409
+ fmt :: from_fn ( move |f| {
1411
1410
for ( i, input) in self . values . iter ( ) . enumerate ( ) {
1412
1411
write ! ( f, "{}: " , input. name) ?;
1413
1412
input. type_ . print ( cx) . fmt ( f) ?;
@@ -1447,7 +1446,7 @@ impl clean::FnDecl {
1447
1446
& ' a self ,
1448
1447
cx : & ' a Context < ' tcx > ,
1449
1448
) -> impl Display + ' b + Captures < ' tcx > {
1450
- display_fn ( move |f| {
1449
+ fmt :: from_fn ( move |f| {
1451
1450
let ellipsis = if self . c_variadic { ", ..." } else { "" } ;
1452
1451
if f. alternate ( ) {
1453
1452
write ! (
@@ -1481,10 +1480,10 @@ impl clean::FnDecl {
1481
1480
indent : usize ,
1482
1481
cx : & ' a Context < ' tcx > ,
1483
1482
) -> impl Display + ' a + Captures < ' tcx > {
1484
- display_fn ( move |f| {
1483
+ fmt :: from_fn ( move |f| {
1485
1484
// First, generate the text form of the declaration, with no line wrapping, and count the bytes.
1486
1485
let mut counter = WriteCounter ( 0 ) ;
1487
- write ! ( & mut counter, "{:#}" , display_fn ( |f| { self . inner_full_print( None , f, cx) } ) )
1486
+ write ! ( & mut counter, "{:#}" , fmt :: from_fn ( |f| { self . inner_full_print( None , f, cx) } ) )
1488
1487
. unwrap ( ) ;
1489
1488
// If the text form was over 80 characters wide, we will line-wrap our output.
1490
1489
let line_wrapping_indent =
@@ -1566,7 +1565,7 @@ impl clean::FnDecl {
1566
1565
& ' a self ,
1567
1566
cx : & ' a Context < ' tcx > ,
1568
1567
) -> impl Display + ' a + Captures < ' tcx > {
1569
- display_fn ( move |f| match & self . output {
1568
+ fmt :: from_fn ( move |f| match & self . output {
1570
1569
clean:: Tuple ( tys) if tys. is_empty ( ) => Ok ( ( ) ) ,
1571
1570
ty if f. alternate ( ) => {
1572
1571
write ! ( f, " -> {:#}" , ty. print( cx) )
@@ -1618,7 +1617,7 @@ pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
1618
1617
} ;
1619
1618
1620
1619
let is_doc_hidden = item. is_doc_hidden ( ) ;
1621
- display_fn ( move |f| {
1620
+ fmt :: from_fn ( move |f| {
1622
1621
if is_doc_hidden {
1623
1622
f. write_str ( "#[doc(hidden)] " ) ?;
1624
1623
}
@@ -1692,7 +1691,7 @@ impl clean::Import {
1692
1691
& ' a self ,
1693
1692
cx : & ' a Context < ' tcx > ,
1694
1693
) -> impl Display + ' a + Captures < ' tcx > {
1695
- display_fn ( move |f| match self . kind {
1694
+ fmt :: from_fn ( move |f| match self . kind {
1696
1695
clean:: ImportKind :: Simple ( name) => {
1697
1696
if name == self . source . path . last ( ) {
1698
1697
write ! ( f, "use {};" , self . source. print( cx) )
@@ -1716,7 +1715,7 @@ impl clean::ImportSource {
1716
1715
& ' a self ,
1717
1716
cx : & ' a Context < ' tcx > ,
1718
1717
) -> impl Display + ' a + Captures < ' tcx > {
1719
- display_fn ( move |f| match self . did {
1718
+ fmt :: from_fn ( move |f| match self . did {
1720
1719
Some ( did) => resolved_path ( f, did, & self . path , true , false , cx) ,
1721
1720
_ => {
1722
1721
for seg in & self . path . segments [ ..self . path . segments . len ( ) - 1 ] {
@@ -1744,7 +1743,7 @@ impl clean::AssocItemConstraint {
1744
1743
& ' a self ,
1745
1744
cx : & ' a Context < ' tcx > ,
1746
1745
) -> impl Display + ' a + Captures < ' tcx > {
1747
- display_fn ( move |f| {
1746
+ fmt :: from_fn ( move |f| {
1748
1747
f. write_str ( self . assoc . name . as_str ( ) ) ?;
1749
1748
self . assoc . args . print ( cx) . fmt ( f) ?;
1750
1749
match self . kind {
@@ -1765,7 +1764,7 @@ impl clean::AssocItemConstraint {
1765
1764
}
1766
1765
1767
1766
pub ( crate ) fn print_abi_with_space ( abi : ExternAbi ) -> impl Display {
1768
- display_fn ( move |f| {
1767
+ fmt :: from_fn ( move |f| {
1769
1768
let quot = if f. alternate ( ) { "\" " } else { """ } ;
1770
1769
match abi {
1771
1770
ExternAbi :: Rust => Ok ( ( ) ) ,
@@ -1783,7 +1782,7 @@ impl clean::GenericArg {
1783
1782
& ' a self ,
1784
1783
cx : & ' a Context < ' tcx > ,
1785
1784
) -> impl Display + ' a + Captures < ' tcx > {
1786
- display_fn ( move |f| match self {
1785
+ fmt :: from_fn ( move |f| match self {
1787
1786
clean:: GenericArg :: Lifetime ( lt) => lt. print ( ) . fmt ( f) ,
1788
1787
clean:: GenericArg :: Type ( ty) => ty. print ( cx) . fmt ( f) ,
1789
1788
clean:: GenericArg :: Const ( ct) => ct. print ( cx. tcx ( ) ) . fmt ( f) ,
@@ -1797,24 +1796,9 @@ impl clean::Term {
1797
1796
& ' a self ,
1798
1797
cx : & ' a Context < ' tcx > ,
1799
1798
) -> impl Display + ' a + Captures < ' tcx > {
1800
- display_fn ( move |f| match self {
1799
+ fmt :: from_fn ( move |f| match self {
1801
1800
clean:: Term :: Type ( ty) => ty. print ( cx) . fmt ( f) ,
1802
1801
clean:: Term :: Constant ( ct) => ct. print ( cx. tcx ( ) ) . fmt ( f) ,
1803
1802
} )
1804
1803
}
1805
1804
}
1806
-
1807
- pub ( crate ) fn display_fn ( f : impl FnOnce ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result ) -> impl Display {
1808
- struct WithFormatter < F > ( Cell < Option < F > > ) ;
1809
-
1810
- impl < F > Display for WithFormatter < F >
1811
- where
1812
- F : FnOnce ( & mut fmt:: Formatter < ' _ > ) -> fmt:: Result ,
1813
- {
1814
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1815
- ( self . 0 . take ( ) ) . unwrap ( ) ( f)
1816
- }
1817
- }
1818
-
1819
- WithFormatter ( Cell :: new ( Some ( f) ) )
1820
- }
0 commit comments