@@ -588,7 +588,7 @@ pub fn write_allocations<'tcx>(
588
588
todo. push ( id) ;
589
589
}
590
590
}
591
- write_allocation ( tcx , alloc , w )
591
+ write ! ( w , "{}" , display_allocation ( tcx , alloc ) )
592
592
} ;
593
593
write ! ( w, "\n {}" , id) ?;
594
594
match tcx. get_global_alloc ( id) {
@@ -640,24 +640,36 @@ pub fn write_allocations<'tcx>(
640
640
/// After the hex dump, an ascii dump follows, replacing all unprintable characters (control
641
641
/// characters or characters whose value is larger than 127) with a `.`
642
642
/// This also prints relocations adequately.
643
- pub fn write_allocation < Tag : Copy + Debug , Extra > (
643
+ pub fn display_allocation < Tag : Copy + Debug , Extra > (
644
644
tcx : TyCtxt < ' tcx > ,
645
- alloc : & Allocation < Tag , Extra > ,
646
- w : & mut dyn Write ,
647
- ) -> io:: Result < ( ) > {
648
- write ! ( w, "size: {}, align: {})" , alloc. size. bytes( ) , alloc. align. bytes( ) ) ?;
649
- if alloc. size == Size :: ZERO {
650
- // We are done.
651
- return write ! ( w, " {{}}" ) ;
645
+ alloc : & ' a Allocation < Tag , Extra > ,
646
+ ) -> RenderAllocation < ' a , ' tcx , Tag , Extra > {
647
+ RenderAllocation { tcx, alloc }
648
+ }
649
+
650
+ #[ doc( hidden) ]
651
+ pub struct RenderAllocation < ' a , ' tcx , Tag , Extra > {
652
+ tcx : TyCtxt < ' tcx > ,
653
+ alloc : & ' a Allocation < Tag , Extra > ,
654
+ }
655
+
656
+ impl < Tag : Copy + Debug , Extra > std:: fmt:: Display for RenderAllocation < ' a , ' tcx , Tag , Extra > {
657
+ fn fmt ( & self , w : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
658
+ let RenderAllocation { tcx, alloc } = * self ;
659
+ write ! ( w, "size: {}, align: {})" , alloc. size. bytes( ) , alloc. align. bytes( ) ) ?;
660
+ if alloc. size == Size :: ZERO {
661
+ // We are done.
662
+ return write ! ( w, " {{}}" ) ;
663
+ }
664
+ // Write allocation bytes.
665
+ writeln ! ( w, " {{" ) ?;
666
+ write_allocation_bytes ( tcx, alloc, w, " " ) ?;
667
+ write ! ( w, "}}" ) ?;
668
+ Ok ( ( ) )
652
669
}
653
- // Write allocation bytes.
654
- writeln ! ( w, " {{" ) ?;
655
- write_allocation_bytes ( tcx, alloc, w, " " ) ?;
656
- write ! ( w, "}}" ) ?;
657
- Ok ( ( ) )
658
670
}
659
671
660
- fn write_allocation_endline ( w : & mut dyn Write , ascii : & str ) -> io :: Result < ( ) > {
672
+ fn write_allocation_endline ( w : & mut dyn std :: fmt :: Write , ascii : & str ) -> std :: fmt :: Result {
661
673
for _ in 0 ..( BYTES_PER_LINE - ascii. chars ( ) . count ( ) ) {
662
674
write ! ( w, " " ) ?;
663
675
}
@@ -669,12 +681,12 @@ const BYTES_PER_LINE: usize = 16;
669
681
670
682
/// Prints the line start address and returns the new line start address.
671
683
fn write_allocation_newline (
672
- w : & mut dyn Write ,
684
+ w : & mut dyn std :: fmt :: Write ,
673
685
mut line_start : Size ,
674
686
ascii : & str ,
675
687
pos_width : usize ,
676
688
prefix : & str ,
677
- ) -> io :: Result < Size > {
689
+ ) -> Result < Size , std :: fmt :: Error > {
678
690
write_allocation_endline ( w, ascii) ?;
679
691
line_start += Size :: from_bytes ( BYTES_PER_LINE ) ;
680
692
write ! ( w, "{}0x{:02$x} │ " , prefix, line_start. bytes( ) , pos_width) ?;
@@ -687,9 +699,9 @@ fn write_allocation_newline(
687
699
fn write_allocation_bytes < Tag : Copy + Debug , Extra > (
688
700
tcx : TyCtxt < ' tcx > ,
689
701
alloc : & Allocation < Tag , Extra > ,
690
- w : & mut dyn Write ,
702
+ w : & mut dyn std :: fmt :: Write ,
691
703
prefix : & str ,
692
- ) -> io :: Result < ( ) > {
704
+ ) -> std :: fmt :: Result {
693
705
let num_lines = alloc. size . bytes_usize ( ) . saturating_sub ( BYTES_PER_LINE ) ;
694
706
// Number of chars needed to represent all line numbers.
695
707
let pos_width = format ! ( "{:x}" , alloc. size. bytes( ) ) . len ( ) ;
0 commit comments