@@ -48,28 +48,26 @@ mod serde;
48
48
use std:: fmt:: { self , Debug , Display , Formatter } ;
49
49
use std:: ops:: { Add , AddAssign , Mul , MulAssign , Sub , SubAssign } ;
50
50
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.
54
52
pub const KB : u64 = 1_000 ;
55
- /// bytes size for 1 megabyte
53
+ /// Number of bytes in 1 megabyte.
56
54
pub const MB : u64 = 1_000_000 ;
57
- /// bytes size for 1 gigabyte
55
+ /// Number of bytes in 1 gigabyte.
58
56
pub const GB : u64 = 1_000_000_000 ;
59
- /// bytes size for 1 terabyte
57
+ /// Number of bytes in 1 terabyte.
60
58
pub const TB : u64 = 1_000_000_000_000 ;
61
- /// bytes size for 1 petabyte
59
+ /// Number of bytes in 1 petabyte.
62
60
pub const PB : u64 = 1_000_000_000_000_000 ;
63
61
64
- /// bytes size for 1 kibibyte
62
+ /// Number of bytes in 1 kibibyte.
65
63
pub const KIB : u64 = 1_024 ;
66
- /// bytes size for 1 mebibyte
64
+ /// Number of bytes in 1 mebibyte.
67
65
pub const MIB : u64 = 1_048_576 ;
68
- /// bytes size for 1 gibibyte
66
+ /// Number of bytes in 1 gibibyte.
69
67
pub const GIB : u64 = 1_073_741_824 ;
70
- /// bytes size for 1 tebibyte
68
+ /// Number of bytes in 1 tebibyte.
71
69
pub const TIB : u64 = 1_099_511_627_776 ;
72
- /// bytes size for 1 pebibyte
70
+ /// Number of bytes in 1 pebibyte.
73
71
pub const PIB : u64 = 1_125_899_906_842_624 ;
74
72
75
73
/// IEC (binary) units.
@@ -88,119 +86,150 @@ const LN_KIB: f64 = 6.931_471_805_599_453;
88
86
/// `ln(1000) ~= 6.908`
89
87
const LN_KB : f64 = 6.907_755_278_982_137 ;
90
88
89
+ /// Formatting style.
91
90
#[ derive( Debug , Clone , Default ) ]
92
91
pub enum Format {
92
+ /// IEC (binary) representation.
93
+ ///
94
+ /// E.g., "1.0 MiB"
93
95
#[ default]
94
96
IEC ,
97
+
98
+ /// SI (decimal) representation.
99
+ ///
100
+ /// E.g., "1.02 MB"
95
101
SI ,
96
102
}
97
103
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 {
99
106
size. into ( ) * KB
100
107
}
101
108
109
+ /// Converts a quantity of kibibytes to bytes.
102
110
pub fn kib < V : Into < u64 > > ( size : V ) -> u64 {
103
111
size. into ( ) * KIB
104
112
}
105
113
114
+ /// Converts a quantity of megabytes to bytes.
106
115
pub fn mb < V : Into < u64 > > ( size : V ) -> u64 {
107
116
size. into ( ) * MB
108
117
}
109
118
119
+ /// Converts a quantity of mebibytes to bytes.
110
120
pub fn mib < V : Into < u64 > > ( size : V ) -> u64 {
111
121
size. into ( ) * MIB
112
122
}
113
123
124
+ /// Converts a quantity of gigabytes to bytes.
114
125
pub fn gb < V : Into < u64 > > ( size : V ) -> u64 {
115
126
size. into ( ) * GB
116
127
}
117
128
129
+ /// Converts a quantity of gibibytes to bytes.
118
130
pub fn gib < V : Into < u64 > > ( size : V ) -> u64 {
119
131
size. into ( ) * GIB
120
132
}
121
133
134
+ /// Converts a quantity of terabytes to bytes.
122
135
pub fn tb < V : Into < u64 > > ( size : V ) -> u64 {
123
136
size. into ( ) * TB
124
137
}
125
138
139
+ /// Converts a quantity of tebibytes to bytes.
126
140
pub fn tib < V : Into < u64 > > ( size : V ) -> u64 {
127
141
size. into ( ) * TIB
128
142
}
129
143
144
+ /// Converts a quantity of petabytes to bytes.
130
145
pub fn pb < V : Into < u64 > > ( size : V ) -> u64 {
131
146
size. into ( ) * PB
132
147
}
133
148
149
+ /// Converts a quantity of pebibytes to bytes.
134
150
pub fn pib < V : Into < u64 > > ( size : V ) -> u64 {
135
151
size. into ( ) * PIB
136
152
}
137
153
138
- /// Byte size representation
154
+ /// Byte size representation.
139
155
#[ derive( Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Default ) ]
140
156
pub struct ByteSize ( pub u64 ) ;
141
157
142
158
impl ByteSize {
159
+ /// Constructs a byte size wrapper from a quantity of bytes.
143
160
#[ inline( always) ]
144
161
pub const fn b ( size : u64 ) -> ByteSize {
145
162
ByteSize ( size)
146
163
}
147
164
165
+ /// Constructs a byte size wrapper from a quantity of kilobytes.
148
166
#[ inline( always) ]
149
167
pub const fn kb ( size : u64 ) -> ByteSize {
150
168
ByteSize ( size * KB )
151
169
}
152
170
171
+ /// Constructs a byte size wrapper from a quantity of kibibytes.
153
172
#[ inline( always) ]
154
173
pub const fn kib ( size : u64 ) -> ByteSize {
155
174
ByteSize ( size * KIB )
156
175
}
157
176
177
+ /// Constructs a byte size wrapper from a quantity of megabytes.
158
178
#[ inline( always) ]
159
179
pub const fn mb ( size : u64 ) -> ByteSize {
160
180
ByteSize ( size * MB )
161
181
}
162
182
183
+ /// Constructs a byte size wrapper from a quantity of mebibytes.
163
184
#[ inline( always) ]
164
185
pub const fn mib ( size : u64 ) -> ByteSize {
165
186
ByteSize ( size * MIB )
166
187
}
167
188
189
+ /// Constructs a byte size wrapper from a quantity of gigabytes.
168
190
#[ inline( always) ]
169
191
pub const fn gb ( size : u64 ) -> ByteSize {
170
192
ByteSize ( size * GB )
171
193
}
172
194
195
+ /// Constructs a byte size wrapper from a quantity of gibibytes.
173
196
#[ inline( always) ]
174
197
pub const fn gib ( size : u64 ) -> ByteSize {
175
198
ByteSize ( size * GIB )
176
199
}
177
200
201
+ /// Constructs a byte size wrapper from a quantity of terabytes.
178
202
#[ inline( always) ]
179
203
pub const fn tb ( size : u64 ) -> ByteSize {
180
204
ByteSize ( size * TB )
181
205
}
182
206
207
+ /// Constructs a byte size wrapper from a quantity of tebibytes.
183
208
#[ inline( always) ]
184
209
pub const fn tib ( size : u64 ) -> ByteSize {
185
210
ByteSize ( size * TIB )
186
211
}
187
212
213
+ /// Constructs a byte size wrapper from a quantity of petabytes.
188
214
#[ inline( always) ]
189
215
pub const fn pb ( size : u64 ) -> ByteSize {
190
216
ByteSize ( size * PB )
191
217
}
192
218
219
+ /// Constructs a byte size wrapper from a quantity of pebibytes.
193
220
#[ inline( always) ]
194
221
pub const fn pib ( size : u64 ) -> ByteSize {
195
222
ByteSize ( size * PIB )
196
223
}
197
224
225
+ /// Returns byte count.
198
226
#[ inline( always) ]
199
227
pub const fn as_u64 ( & self ) -> u64 {
200
228
self . 0
201
229
}
202
230
}
203
231
232
+ /// Constructs human-readable string representation of `bytes` with given `format` style.
204
233
pub fn to_string_format ( bytes : u64 , format : Format ) -> String {
205
234
let unit = match format {
206
235
Format :: IEC => KIB ,
@@ -430,21 +459,12 @@ mod tests {
430
459
let mut x = ByteSize :: mb ( 1 ) ;
431
460
432
461
assert_eq ! ( ( x + MB as u64 ) . as_u64( ) , 2_000_000 ) ;
433
-
434
462
assert_eq ! ( ( x + MB as u32 ) . as_u64( ) , 2_000_000 ) ;
435
-
436
463
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
-
440
464
assert_eq ! ( ( x - MB as u64 ) . as_u64( ) , 0 ) ;
441
-
442
465
assert_eq ! ( ( x - MB as u32 ) . as_u64( ) , 0 ) ;
443
-
444
466
assert_eq ! ( ( x - KB as u32 ) . as_u64( ) , 999_000 ) ;
445
467
446
- assert_eq ! ( ( x - B as u32 ) . as_u64( ) , 999_999 ) ;
447
-
448
468
x += MB as u64 ;
449
469
x += MB as u32 ;
450
470
x += 10u16 ;
0 commit comments