@@ -72,7 +72,7 @@ impl fmt::Debug for AllocationReport {
72
72
} else {
73
73
"--"
74
74
} ;
75
- write ! ( f, "{name:?}: {}" , fmt_bytes ( self . size) )
75
+ write ! ( f, "{name:?}: {}" , FmtBytes ( self . size) )
76
76
}
77
77
}
78
78
@@ -89,8 +89,8 @@ impl fmt::Debug for AllocatorReport {
89
89
"summary" ,
90
90
& std:: format_args!(
91
91
"{} / {}" ,
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)
94
94
) ,
95
95
)
96
96
. field ( "blocks" , & self . blocks . len ( ) )
@@ -145,18 +145,20 @@ pub(crate) trait SubAllocator: SubAllocatorBase + fmt::Debug + Sync + Send {
145
145
}
146
146
}
147
147
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 ) ;
150
149
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 ;
156
162
}
157
-
158
- print_amount = amount as f64 / 1024.0 ;
159
- amount /= 1024 ;
160
- idx += 1 ;
161
163
}
162
164
}
0 commit comments