@@ -34,7 +34,7 @@ mod parse;
34
34
extern crate serde;
35
35
36
36
use std:: fmt:: { Debug , Display , Formatter , Result } ;
37
- use std:: ops:: { Add , Mul } ;
37
+ use std:: ops:: { Add , AddAssign , Mul , MulAssign } ;
38
38
39
39
/// byte size for 1 byte
40
40
pub const B : u64 = 1 ;
@@ -219,14 +219,6 @@ impl Debug for ByteSize {
219
219
220
220
macro_rules! commutative_op {
221
221
( $t: ty) => {
222
- impl Add <$t> for ByteSize {
223
- type Output = ByteSize ;
224
- #[ inline( always) ]
225
- fn add( self , rhs: $t) -> ByteSize {
226
- ByteSize ( self . 0 + ( rhs as u64 ) )
227
- }
228
- }
229
-
230
222
impl Add <ByteSize > for $t {
231
223
type Output = ByteSize ;
232
224
#[ inline( always) ]
@@ -235,14 +227,6 @@ macro_rules! commutative_op {
235
227
}
236
228
}
237
229
238
- impl Mul <$t> for ByteSize {
239
- type Output = ByteSize ;
240
- #[ inline( always) ]
241
- fn mul( self , rhs: $t) -> ByteSize {
242
- ByteSize ( self . 0 * ( rhs as u64 ) )
243
- }
244
- }
245
-
246
230
impl Mul <ByteSize > for $t {
247
231
type Output = ByteSize ;
248
232
#[ inline( always) ]
@@ -267,25 +251,71 @@ impl Add<ByteSize> for ByteSize {
267
251
}
268
252
}
269
253
254
+ impl AddAssign < ByteSize > for ByteSize {
255
+ #[ inline( always) ]
256
+ fn add_assign ( & mut self , rhs : ByteSize ) {
257
+ self . 0 += rhs. 0
258
+ }
259
+ }
260
+
261
+ impl < T > Add < T > for ByteSize
262
+ where T : Into < u64 > {
263
+ type Output = ByteSize ;
264
+ #[ inline( always) ]
265
+ fn add ( self , rhs : T ) -> ByteSize {
266
+ ByteSize ( self . 0 + ( rhs. into ( ) as u64 ) )
267
+ }
268
+ }
269
+
270
+ impl < T > AddAssign < T > for ByteSize
271
+ where T : Into < u64 > {
272
+ #[ inline( always) ]
273
+ fn add_assign ( & mut self , rhs : T ) {
274
+ self . 0 += rhs. into ( ) as u64 ;
275
+ }
276
+ }
277
+
278
+ impl < T > Mul < T > for ByteSize
279
+ where T : Into < u64 > {
280
+ type Output = ByteSize ;
281
+ #[ inline( always) ]
282
+ fn mul ( self , rhs : T ) -> ByteSize {
283
+ ByteSize ( self . 0 * ( rhs. into ( ) as u64 ) )
284
+ }
285
+ }
286
+
287
+ impl < T > MulAssign < T > for ByteSize
288
+ where T : Into < u64 > {
289
+ #[ inline( always) ]
290
+ fn mul_assign ( & mut self , rhs : T ) {
291
+ self . 0 *= rhs. into ( ) as u64 ;
292
+ }
293
+ }
294
+
270
295
#[ cfg( test) ]
271
296
mod tests {
272
297
use super :: * ;
273
298
274
299
#[ test]
275
300
fn test_arithmetic_op ( ) {
276
- let x = ByteSize :: mb ( 1 ) ;
301
+ let mut x = ByteSize :: mb ( 1 ) ;
277
302
let y = ByteSize :: kb ( 100 ) ;
278
303
279
304
assert_eq ! ( ( x + y) . as_u64( ) , 1_100_000u64 ) ;
280
305
281
306
assert_eq ! ( ( x + ( 100 * 1000 ) as u64 ) . as_u64( ) , 1_100_000 ) ;
282
307
283
308
assert_eq ! ( ( x * 2u64 ) . as_u64( ) , 2_000_000 ) ;
309
+
310
+ x += y;
311
+ assert_eq ! ( x. as_u64( ) , 1_100_000 ) ;
312
+ x *= 2u64 ;
313
+ assert_eq ! ( x. as_u64( ) , 2_200_000 ) ;
284
314
}
285
315
286
316
#[ test]
287
317
fn test_arithmetic_primitives ( ) {
288
- let x = ByteSize :: mb ( 1 ) ;
318
+ let mut x = ByteSize :: mb ( 1 ) ;
289
319
290
320
assert_eq ! ( ( x + MB as u64 ) . as_u64( ) , 2_000_000 ) ;
291
321
@@ -294,6 +324,12 @@ mod tests {
294
324
assert_eq ! ( ( x + KB as u16 ) . as_u64( ) , 1_001_000 ) ;
295
325
296
326
assert_eq ! ( ( x + B as u8 ) . as_u64( ) , 1_000_001 ) ;
327
+
328
+ x += MB as u64 ;
329
+ x += MB as u32 ;
330
+ x += 10 as u16 ;
331
+ x += 1 as u8 ;
332
+ assert_eq ! ( x. as_u64( ) , 3_000_011 ) ;
297
333
}
298
334
299
335
#[ test]
0 commit comments