@@ -1543,11 +1543,11 @@ pub trait PrettyPrinter<'tcx>:
15431543}
15441544
15451545// 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 > > ) ;
15471547
1548- pub struct FmtPrinterData < ' a , ' tcx , F > {
1548+ pub struct FmtPrinterData < ' a , ' tcx > {
15491549 tcx : TyCtxt < ' tcx > ,
1550- fmt : F ,
1550+ fmt : String ,
15511551
15521552 empty_path : bool ,
15531553 in_value : bool ,
@@ -1564,24 +1564,26 @@ pub struct FmtPrinterData<'a, 'tcx, F> {
15641564 pub const_infer_name_resolver : Option < Box < dyn Fn ( ty:: ConstVid < ' tcx > ) -> Option < String > + ' a > > ,
15651565}
15661566
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 > ;
15691569 fn deref ( & self ) -> & Self :: Target {
15701570 & self . 0
15711571 }
15721572}
15731573
1574- impl < F > DerefMut for FmtPrinter < ' _ , ' _ , F > {
1574+ impl DerefMut for FmtPrinter < ' _ , ' _ > {
15751575 fn deref_mut ( & mut self ) -> & mut Self :: Target {
15761576 & mut self . 0
15771577 }
15781578}
15791579
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 {
15821582 FmtPrinter ( Box :: new ( FmtPrinterData {
15831583 tcx,
1584- fmt,
1584+ // Estimated reasonable capacity to allocate upfront based on a few
1585+ // benchmarks.
1586+ fmt : String :: with_capacity ( 64 ) ,
15851587 empty_path : false ,
15861588 in_value : ns == Namespace :: ValueNS ,
15871589 print_alloc_ids : false ,
@@ -1594,6 +1596,10 @@ impl<'a, 'tcx, F> FmtPrinter<'a, 'tcx, F> {
15941596 const_infer_name_resolver : None ,
15951597 } ) )
15961598 }
1599+
1600+ pub fn into_buffer ( self ) -> String {
1601+ self . 0 . fmt
1602+ }
15971603}
15981604
15991605// HACK(eddyb) get rid of `def_path_str` and/or pass `Namespace` explicitly always
@@ -1625,19 +1631,18 @@ impl<'t> TyCtxt<'t> {
16251631 pub fn def_path_str_with_substs ( self , def_id : DefId , substs : & ' t [ GenericArg < ' t > ] ) -> String {
16261632 let ns = guess_def_namespace ( self , def_id) ;
16271633 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 ( )
16311635 }
16321636}
16331637
1634- impl < F : fmt:: Write > fmt :: Write for FmtPrinter < ' _ , ' _ , F > {
1638+ impl fmt:: Write for FmtPrinter < ' _ , ' _ > {
16351639 fn write_str ( & mut self , s : & str ) -> fmt:: Result {
1636- self . fmt . write_str ( s)
1640+ self . fmt . push_str ( s) ;
1641+ Ok ( ( ) )
16371642 }
16381643}
16391644
1640- impl < ' tcx , F : fmt :: Write > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1645+ impl < ' tcx > Printer < ' tcx > for FmtPrinter < ' _ , ' tcx > {
16411646 type Error = fmt:: Error ;
16421647
16431648 type Path = Self ;
@@ -1845,7 +1850,7 @@ impl<'tcx, F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
18451850 }
18461851}
18471852
1848- impl < ' tcx , F : fmt :: Write > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx , F > {
1853+ impl < ' tcx > PrettyPrinter < ' tcx > for FmtPrinter < ' _ , ' tcx > {
18491854 fn ty_infer_name ( & self , id : ty:: TyVid ) -> Option < String > {
18501855 self . 0 . ty_infer_name_resolver . as_ref ( ) . and_then ( |func| func ( id) )
18511856 }
@@ -1981,7 +1986,7 @@ impl<'tcx, F: fmt::Write> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx, F> {
19811986}
19821987
19831988// HACK(eddyb) limited to `FmtPrinter` because of `region_highlight_mode`.
1984- impl < F : fmt :: Write > FmtPrinter < ' _ , ' _ , F > {
1989+ impl FmtPrinter < ' _ , ' _ > {
19851990 pub fn pretty_print_region ( mut self , region : ty:: Region < ' _ > ) -> Result < Self , fmt:: Error > {
19861991 define_scoped_cx ! ( self ) ;
19871992
@@ -2115,7 +2120,7 @@ impl<'a, 'tcx> ty::TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
21152120
21162121// HACK(eddyb) limited to `FmtPrinter` because of `binder_depth`,
21172122// `region_index` and `used_region_names`.
2118- impl < ' tcx , F : fmt :: Write > FmtPrinter < ' _ , ' tcx , F > {
2123+ impl < ' tcx > FmtPrinter < ' _ , ' tcx > {
21192124 pub fn name_all_regions < T > (
21202125 mut self ,
21212126 value : & ty:: Binder < ' tcx , T > ,
@@ -2367,9 +2372,10 @@ macro_rules! forward_display_to_print {
23672372 $( #[ allow( unused_lifetimes) ] impl <' tcx> fmt:: Display for $ty {
23682373 fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
23692374 ty:: tls:: with( |tcx| {
2370- tcx. lift( * self )
2375+ let cx = tcx. lift( * self )
23712376 . 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( ) ) ?;
23732379 Ok ( ( ) )
23742380 } )
23752381 }
@@ -2400,8 +2406,7 @@ macro_rules! define_print_and_forward_display {
24002406impl < ' tcx > fmt:: Display for ty:: Region < ' tcx > {
24012407 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
24022408 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 ( ) )
24052410 } )
24062411 }
24072412}
0 commit comments