11use std:: fmt:: { self , Display } ;
22
3+ /// Human readable size representation
34#[ derive( Copy , Clone , Debug ) ]
4- pub enum Unit {
5- B ,
6- IO ,
7- }
8-
9- /// Human readable size (some units)
10- pub ( crate ) enum Size {
11- B ( usize ) ,
12- IO ( usize ) ,
13- }
5+ pub ( crate ) struct Size ( usize ) ;
146
157impl Size {
16- pub ( crate ) fn new ( size : usize , unit : Unit ) -> Self {
17- match unit {
18- Unit :: B => Self :: B ( size) ,
19- Unit :: IO => Self :: IO ( size) ,
20- }
8+ pub ( crate ) fn new ( size : usize ) -> Self {
9+ Self ( size)
2110 }
2211}
2312
2413impl Display for Size {
2514 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
26- match self {
27- Size :: B ( size) => {
28- const KI : f64 = 1024.0 ;
29- const MI : f64 = KI * KI ;
30- const GI : f64 = KI * KI * KI ;
31- let size = * size as f64 ;
32-
33- let suffix: String = "" . into ( ) ;
34-
35- if size >= GI {
36- write ! ( f, "{:5.1} GiB{}" , size / GI , suffix)
37- } else if size >= MI {
38- write ! ( f, "{:5.1} MiB{}" , size / MI , suffix)
39- } else if size >= KI {
40- write ! ( f, "{:5.1} KiB{}" , size / KI , suffix)
41- } else {
42- write ! ( f, "{size:3.0} B{suffix}" )
43- }
44- }
45- Size :: IO ( size) => {
46- const K : f64 = 1000.0 ;
47- const M : f64 = K * K ;
48- const G : f64 = K * K * K ;
49- let size = * size as f64 ;
50-
51- let suffix: String = "IO-ops" . into ( ) ;
52-
53- if size >= G {
54- write ! ( f, "{:5.1} giga-{}" , size / G , suffix)
55- } else if size >= M {
56- write ! ( f, "{:5.1} mega-{}" , size / M , suffix)
57- } else if size >= K {
58- write ! ( f, "{:5.1} kilo-{}" , size / K , suffix)
59- } else {
60- write ! ( f, "{size:3.0} {suffix}" )
61- }
62- }
15+ const KI : f64 = 1024.0 ;
16+ const MI : f64 = KI * KI ;
17+ const GI : f64 = KI * KI * KI ;
18+ let size = self . 0 as f64 ;
19+
20+ let suffix: String = "" . into ( ) ;
21+
22+ if size >= GI {
23+ write ! ( f, "{:5.1} GiB{}" , size / GI , suffix)
24+ } else if size >= MI {
25+ write ! ( f, "{:5.1} MiB{}" , size / MI , suffix)
26+ } else if size >= KI {
27+ write ! ( f, "{:5.1} KiB{}" , size / KI , suffix)
28+ } else {
29+ write ! ( f, "{size:3.0} B{suffix}" )
6330 }
6431 }
6532}
@@ -68,33 +35,12 @@ impl Display for Size {
6835mod tests {
6936 #[ test]
7037 fn unit_formatter_test ( ) {
71- use crate :: utils:: units:: { Size , Unit } ;
38+ use crate :: utils:: units:: Size ;
7239
7340 // Test Bytes
74- assert_eq ! ( format!( "{}" , Size :: new( 1 , Unit :: B ) ) , " 1 B" ) ;
75- assert_eq ! ( format!( "{}" , Size :: new( 1024 , Unit :: B ) ) , " 1.0 KiB" ) ;
76- assert_eq ! (
77- format!( "{}" , Size :: new( 1024usize . pow( 2 ) , Unit :: B ) ) ,
78- " 1.0 MiB"
79- ) ;
80- assert_eq ! (
81- format!( "{}" , Size :: new( 1024usize . pow( 3 ) , Unit :: B ) ) ,
82- " 1.0 GiB"
83- ) ;
84-
85- //Test I/O Operations
86- assert_eq ! ( format!( "{}" , Size :: new( 1 , Unit :: IO ) ) , " 1 IO-ops" ) ;
87- assert_eq ! (
88- format!( "{}" , Size :: new( 1000 , Unit :: IO ) ) ,
89- " 1.0 kilo-IO-ops"
90- ) ;
91- assert_eq ! (
92- format!( "{}" , Size :: new( 1000usize . pow( 2 ) , Unit :: IO ) ) ,
93- " 1.0 mega-IO-ops"
94- ) ;
95- assert_eq ! (
96- format!( "{}" , Size :: new( 1000usize . pow( 3 ) , Unit :: IO ) ) ,
97- " 1.0 giga-IO-ops"
98- ) ;
41+ assert_eq ! ( format!( "{}" , Size :: new( 1 ) ) , " 1 B" ) ;
42+ assert_eq ! ( format!( "{}" , Size :: new( 1024 ) ) , " 1.0 KiB" ) ;
43+ assert_eq ! ( format!( "{}" , Size :: new( 1024usize . pow( 2 ) ) ) , " 1.0 MiB" ) ;
44+ assert_eq ! ( format!( "{}" , Size :: new( 1024usize . pow( 3 ) ) ) , " 1.0 GiB" ) ;
9945 }
10046}
0 commit comments