@@ -72,7 +72,7 @@ impl fmt::Debug for AllocationReport {
7272 } else {
7373 "--"
7474 } ;
75- write ! ( f, "{name:?}: {}" , fmt_bytes ( self . size) )
75+ write ! ( f, "{name:?}: {}" , FmtBytes ( self . size) )
7676 }
7777}
7878
@@ -89,8 +89,8 @@ impl fmt::Debug for AllocatorReport {
8989 "summary" ,
9090 & std:: format_args!(
9191 "{} / {}" ,
92- fmt_bytes ( self . total_allocated_bytes) ,
93- fmt_bytes ( self . total_reserved_bytes)
92+ FmtBytes ( self . total_allocated_bytes) ,
93+ FmtBytes ( self . total_reserved_bytes)
9494 ) ,
9595 )
9696 . field ( "blocks" , & self . blocks . len ( ) )
@@ -145,18 +145,20 @@ pub(crate) trait SubAllocator: SubAllocatorBase + fmt::Debug + Sync + Send {
145145 }
146146}
147147
148- pub ( crate ) fn fmt_bytes ( mut amount : u64 ) -> String {
149- const SUFFIX : [ & str ; 5 ] = [ "B" , "KB" , "MB" , "GB" , "TB" ] ;
148+ pub struct FmtBytes ( pub u64 ) ;
150149
151- let mut idx = 0 ;
152- let mut print_amount = amount as f64 ;
153- loop {
154- if amount < 1024 {
155- return format ! ( "{:.2} {}" , print_amount, SUFFIX [ idx] ) ;
150+ impl fmt:: Display for FmtBytes {
151+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
152+ const SUFFIX : [ & str ; 5 ] = [ "B" , "KB" , "MB" , "GB" , "TB" ] ;
153+ let mut idx = 0 ;
154+ let mut amount = self . 0 as f64 ;
155+ loop {
156+ if amount < 1024.0 || idx >= SUFFIX . len ( ) - 1 {
157+ return write ! ( f, "{:.2} {}" , amount, SUFFIX [ idx] ) ;
158+ }
159+
160+ amount /= 1024.0 ;
161+ idx += 1 ;
156162 }
157-
158- print_amount = amount as f64 / 1024.0 ;
159- amount /= 1024 ;
160- idx += 1 ;
161163 }
162164}
0 commit comments