@@ -1543,11 +1543,11 @@ pub trait PrettyPrinter<'tcx>:
1543
1543
}
1544
1544
1545
1545
// HACK(eddyb) boxed to avoid moving around a large struct by-value.
1546
- pub struct FmtPrinter < ' a , ' tcx , F > ( Box < FmtPrinterData < ' a , ' tcx , F > > ) ;
1546
+ pub struct FmtPrinter < ' a , ' tcx > ( Box < FmtPrinterData < ' a , ' tcx > > ) ;
1547
1547
1548
- pub struct FmtPrinterData < ' a , ' tcx , F > {
1548
+ pub struct FmtPrinterData < ' a , ' tcx > {
1549
1549
tcx : TyCtxt < ' tcx > ,
1550
- fmt : F ,
1550
+ fmt : String ,
1551
1551
1552
1552
empty_path : bool ,
1553
1553
in_value : bool ,
@@ -1564,24 +1564,26 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
1564
1564
pub const_infer_name_resolver : Option < Box < dyn Fn ( ty:: ConstVid < ' tcx > ) -> Option < String > + ' a > > ,
1565
1565
}
1566
1566
1567
- impl < ' a , ' tcx , F > Deref for FmtPrinter < ' a , ' tcx , F > {
1568
- type Target = FmtPrinterData < ' a , ' tcx , F > ;
1567
+ impl < ' a , ' tcx > Deref for FmtPrinter < ' a , ' tcx > {
1568
+ type Target = FmtPrinterData < ' a , ' tcx > ;
1569
1569
fn deref ( & self ) -> & Self :: Target {
1570
1570
& self . 0
1571
1571
}
1572
1572
}
1573
1573
1574
- impl < F > DerefMut for FmtPrinter < ' _ , ' _ , F > {
1574
+ impl DerefMut for FmtPrinter < ' _ , ' _ > {
1575
1575
fn deref_mut ( & mut self ) -> & mut Self :: Target {
1576
1576
& mut self . 0
1577
1577
}
1578
1578
}
1579
1579
1580
- impl < ' a , ' tcx , F > FmtPrinter < ' a , ' tcx , F > {
1581
- pub fn new ( tcx : TyCtxt < ' tcx > , fmt : F , ns : Namespace ) -> Self {
1580
+ impl < ' a , ' tcx > FmtPrinter < ' a , ' tcx > {
1581
+ pub fn new ( tcx : TyCtxt < ' tcx > , ns : Namespace ) -> Self {
1582
1582
FmtPrinter ( Box :: new ( FmtPrinterData {
1583
1583
tcx,
1584
- fmt,
1584
+ // Estimated reasonable capacity to allocate upfront based on a few
1585
+ // benchmarks.
1586
+ fmt : String :: with_capacity ( 64 ) ,
1585
1587
empty_path : false ,
1586
1588
in_value : ns == Namespace :: ValueNS ,
1587
1589
print_alloc_ids : false ,
@@ -1594,6 +1596,10 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
1594
1596
const_infer_name_resolver : None ,
1595
1597
} ) )
1596
1598
}
1599
+
1600
+ pub fn into_buffer ( self ) -> String {
1601
+ self . 0 . fmt
1602
+ }
1597
1603
}
1598
1604
1599
1605
// HACK(eddyb) get rid of `def_path_str` and/or pass `Namespace` explicitly always
@@ -1625,19 +1631,18 @@ impl<'t> TyCtxt<'t> {
1625
1631
pub fn def_path_str_with_substs ( self , def_id : DefId , substs : & ' t [ GenericArg < ' t > ] ) -> String {
1626
1632
let ns = guess_def_namespace ( self , def_id) ;
1627
1633
debug ! ( "def_path_str: def_id={:?}, ns={:?}" , def_id, ns) ;
1628
- let mut s = String :: new ( ) ;
1629
- let _ = FmtPrinter :: new ( self , & mut s, ns) . print_def_path ( def_id, substs) ;
1630
- s
1634
+ FmtPrinter :: new ( self , ns) . print_def_path ( def_id, substs) . unwrap ( ) . into_buffer ( )
1631
1635
}
1632
1636
}
1633
1637
1634
- impl < F : fmt:: Write > fmt :: Write for FmtPrinter < ' _ , ' _ , F > {
1638
+ impl fmt:: Write for FmtPrinter < ' _ , ' _ > {
1635
1639
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
1636
- self . fmt . write_str ( s)
1640
+ self . fmt . push_str ( s) ;
1641
+ Ok ( ( ) )
1637
1642
}
1638
1643
}
1639
1644
1640
- impl < ' tcx , F : fmt :: Write > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1645
+ impl < ' tcx > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx > {
1641
1646
type Error = fmt:: Error ;
1642
1647
1643
1648
type Path = Self ;
@@ -1845,7 +1850,7 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
1845
1850
}
1846
1851
}
1847
1852
1848
- impl < ' tcx , F : fmt :: Write > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1853
+ impl < ' tcx > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx > {
1849
1854
fn ty_infer_name ( & self , id : ty:: TyVid ) -> Option < String > {
1850
1855
self . 0 . ty_infer_name_resolver . as_ref ( ) . and_then ( |func| func ( id) )
1851
1856
}
@@ -1981,7 +1986,7 @@ impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
1981
1986
}
1982
1987
1983
1988
// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
1984
- impl < F : fmt :: Write > FmtPrinter < ' _ , ' _ , F > {
1989
+ impl FmtPrinter < ' _ , ' _ > {
1985
1990
pub fn pretty_print_region ( mut self , region : ty:: Region < ' _ > ) -> Result < Self , fmt:: Error > {
1986
1991
define_scoped_cx ! ( self ) ;
1987
1992
@@ -2115,7 +2120,7 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
2115
2120
2116
2121
// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
2117
2122
// `region_index` and `used_region_names`.
2118
- impl < ' tcx , F : fmt :: Write > FmtPrinter < ' _ , ' tcx , F > {
2123
+ impl < ' tcx > FmtPrinter < ' _ , ' tcx > {
2119
2124
pub fn name_all_regions < T > (
2120
2125
mut self ,
2121
2126
value : & ty:: Binder < ' tcx , T > ,
@@ -2367,9 +2372,10 @@ macro_rules! forward_display_to_print {
2367
2372
$( #[ allow( unused_lifetimes) ] impl <' tcx> fmt:: Display for $ty {
2368
2373
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
2369
2374
ty:: tls:: with( |tcx| {
2370
- tcx. lift( * self )
2375
+ let cx = tcx. lift( * self )
2371
2376
. expect( "could not lift for printing" )
2372
- . print( FmtPrinter :: new( tcx, f, Namespace :: TypeNS ) ) ?;
2377
+ . print( FmtPrinter :: new( tcx, Namespace :: TypeNS ) ) ?;
2378
+ f. write_str( & cx. into_buffer( ) ) ?;
2373
2379
Ok ( ( ) )
2374
2380
} )
2375
2381
}
@@ -2400,8 +2406,7 @@ macro_rules! define_print_and_forward_display {
2400
2406
impl < ' tcx > fmt:: Display for ty:: Region < ' tcx > {
2401
2407
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2402
2408
ty:: tls:: with ( |tcx| {
2403
- self . print ( FmtPrinter :: new ( tcx, f, Namespace :: TypeNS ) ) ?;
2404
- Ok ( ( ) )
2409
+ f. write_str ( & self . print ( FmtPrinter :: new ( tcx, Namespace :: TypeNS ) ) ?. into_buffer ( ) )
2405
2410
} )
2406
2411
}
2407
2412
}
0 commit comments