@@ -48,28 +48,26 @@ mod serde;
4848use std:: fmt:: { self , Debug , Display , Formatter } ;
4949use std:: ops:: { Add , AddAssign , Mul , MulAssign , Sub , SubAssign } ;
5050
51- /// byte size for 1 byte
52- pub const B : u64 = 1 ;
53- /// bytes size for 1 kilobyte
51+ /// Number of bytes in 1 kilobyte.
5452pub const KB : u64 = 1_000 ;
55- /// bytes size for 1 megabyte
53+ /// Number of bytes in 1 megabyte.
5654pub const MB : u64 = 1_000_000 ;
57- /// bytes size for 1 gigabyte
55+ /// Number of bytes in 1 gigabyte.
5856pub const GB : u64 = 1_000_000_000 ;
59- /// bytes size for 1 terabyte
57+ /// Number of bytes in 1 terabyte.
6058pub const TB : u64 = 1_000_000_000_000 ;
61- /// bytes size for 1 petabyte
59+ /// Number of bytes in 1 petabyte.
6260pub const PB : u64 = 1_000_000_000_000_000 ;
6361
64- /// bytes size for 1 kibibyte
62+ /// Number of bytes in 1 kibibyte.
6563pub const KIB : u64 = 1_024 ;
66- /// bytes size for 1 mebibyte
64+ /// Number of bytes in 1 mebibyte.
6765pub const MIB : u64 = 1_048_576 ;
68- /// bytes size for 1 gibibyte
66+ /// Number of bytes in 1 gibibyte.
6967pub const GIB : u64 = 1_073_741_824 ;
70- /// bytes size for 1 tebibyte
68+ /// Number of bytes in 1 tebibyte.
7169pub const TIB : u64 = 1_099_511_627_776 ;
72- /// bytes size for 1 pebibyte
70+ /// Number of bytes in 1 pebibyte.
7371pub const PIB : u64 = 1_125_899_906_842_624 ;
7472
7573/// IEC (binary) units.
@@ -88,119 +86,150 @@ const LN_KIB: f64 = 6.931_471_805_599_453;
8886/// `ln(1000) ~= 6.908`
8987const LN_KB : f64 = 6.907_755_278_982_137 ;
9088
89+ /// Formatting style.
9190#[ derive( Debug , Clone , Default ) ]
9291pub enum Format {
92+ /// IEC (binary) representation.
93+ ///
94+ /// E.g., "1.0 MiB"
9395 #[ default]
9496 IEC ,
97+
98+ /// SI (decimal) representation.
99+ ///
100+ /// E.g., "1.02 MB"
95101 SI ,
96102}
97103
98- pub fn kb < V : Into < u64 > > ( size : V ) -> u64 {
104+ /// Converts a quantity of kilobytes to bytes.
105+ pub fn kb ( size : impl Into < u64 > ) -> u64 {
99106 size. into ( ) * KB
100107}
101108
109+ /// Converts a quantity of kibibytes to bytes.
102110pub fn kib < V : Into < u64 > > ( size : V ) -> u64 {
103111 size. into ( ) * KIB
104112}
105113
114+ /// Converts a quantity of megabytes to bytes.
106115pub fn mb < V : Into < u64 > > ( size : V ) -> u64 {
107116 size. into ( ) * MB
108117}
109118
119+ /// Converts a quantity of mebibytes to bytes.
110120pub fn mib < V : Into < u64 > > ( size : V ) -> u64 {
111121 size. into ( ) * MIB
112122}
113123
124+ /// Converts a quantity of gigabytes to bytes.
114125pub fn gb < V : Into < u64 > > ( size : V ) -> u64 {
115126 size. into ( ) * GB
116127}
117128
129+ /// Converts a quantity of gibibytes to bytes.
118130pub fn gib < V : Into < u64 > > ( size : V ) -> u64 {
119131 size. into ( ) * GIB
120132}
121133
134+ /// Converts a quantity of terabytes to bytes.
122135pub fn tb < V : Into < u64 > > ( size : V ) -> u64 {
123136 size. into ( ) * TB
124137}
125138
139+ /// Converts a quantity of tebibytes to bytes.
126140pub fn tib < V : Into < u64 > > ( size : V ) -> u64 {
127141 size. into ( ) * TIB
128142}
129143
144+ /// Converts a quantity of petabytes to bytes.
130145pub fn pb < V : Into < u64 > > ( size : V ) -> u64 {
131146 size. into ( ) * PB
132147}
133148
149+ /// Converts a quantity of pebibytes to bytes.
134150pub fn pib < V : Into < u64 > > ( size : V ) -> u64 {
135151 size. into ( ) * PIB
136152}
137153
138- /// Byte size representation
154+ /// Byte size representation.
139155#[ derive( Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Default ) ]
140156pub struct ByteSize ( pub u64 ) ;
141157
142158impl ByteSize {
159+ /// Constructs a byte size wrapper from a quantity of bytes.
143160 #[ inline( always) ]
144161 pub const fn b ( size : u64 ) -> ByteSize {
145162 ByteSize ( size)
146163 }
147164
165+ /// Constructs a byte size wrapper from a quantity of kilobytes.
148166 #[ inline( always) ]
149167 pub const fn kb ( size : u64 ) -> ByteSize {
150168 ByteSize ( size * KB )
151169 }
152170
171+ /// Constructs a byte size wrapper from a quantity of kibibytes.
153172 #[ inline( always) ]
154173 pub const fn kib ( size : u64 ) -> ByteSize {
155174 ByteSize ( size * KIB )
156175 }
157176
177+ /// Constructs a byte size wrapper from a quantity of megabytes.
158178 #[ inline( always) ]
159179 pub const fn mb ( size : u64 ) -> ByteSize {
160180 ByteSize ( size * MB )
161181 }
162182
183+ /// Constructs a byte size wrapper from a quantity of mebibytes.
163184 #[ inline( always) ]
164185 pub const fn mib ( size : u64 ) -> ByteSize {
165186 ByteSize ( size * MIB )
166187 }
167188
189+ /// Constructs a byte size wrapper from a quantity of gigabytes.
168190 #[ inline( always) ]
169191 pub const fn gb ( size : u64 ) -> ByteSize {
170192 ByteSize ( size * GB )
171193 }
172194
195+ /// Constructs a byte size wrapper from a quantity of gibibytes.
173196 #[ inline( always) ]
174197 pub const fn gib ( size : u64 ) -> ByteSize {
175198 ByteSize ( size * GIB )
176199 }
177200
201+ /// Constructs a byte size wrapper from a quantity of terabytes.
178202 #[ inline( always) ]
179203 pub const fn tb ( size : u64 ) -> ByteSize {
180204 ByteSize ( size * TB )
181205 }
182206
207+ /// Constructs a byte size wrapper from a quantity of tebibytes.
183208 #[ inline( always) ]
184209 pub const fn tib ( size : u64 ) -> ByteSize {
185210 ByteSize ( size * TIB )
186211 }
187212
213+ /// Constructs a byte size wrapper from a quantity of petabytes.
188214 #[ inline( always) ]
189215 pub const fn pb ( size : u64 ) -> ByteSize {
190216 ByteSize ( size * PB )
191217 }
192218
219+ /// Constructs a byte size wrapper from a quantity of pebibytes.
193220 #[ inline( always) ]
194221 pub const fn pib ( size : u64 ) -> ByteSize {
195222 ByteSize ( size * PIB )
196223 }
197224
225+ /// Returns byte count.
198226 #[ inline( always) ]
199227 pub const fn as_u64 ( & self ) -> u64 {
200228 self . 0
201229 }
202230}
203231
232+ /// Constructs human-readable string representation of `bytes` with given `format` style.
204233pub fn to_string_format ( bytes : u64 , format : Format ) -> String {
205234 let unit = match format {
206235 Format :: IEC => KIB ,
@@ -430,21 +459,12 @@ mod tests {
430459 let mut x = ByteSize :: mb ( 1 ) ;
431460
432461 assert_eq ! ( ( x + MB as u64 ) . as_u64( ) , 2_000_000 ) ;
433-
434462 assert_eq ! ( ( x + MB as u32 ) . as_u64( ) , 2_000_000 ) ;
435-
436463 assert_eq ! ( ( x + KB as u16 ) . as_u64( ) , 1_001_000 ) ;
437-
438- assert_eq ! ( ( x + B as u8 ) . as_u64( ) , 1_000_001 ) ;
439-
440464 assert_eq ! ( ( x - MB as u64 ) . as_u64( ) , 0 ) ;
441-
442465 assert_eq ! ( ( x - MB as u32 ) . as_u64( ) , 0 ) ;
443-
444466 assert_eq ! ( ( x - KB as u32 ) . as_u64( ) , 999_000 ) ;
445467
446- assert_eq ! ( ( x - B as u32 ) . as_u64( ) , 999_999 ) ;
447-
448468 x += MB as u64 ;
449469 x += MB as u32 ;
450470 x += 10u16 ;
0 commit comments