@@ -39,7 +39,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
39
39
use std:: convert:: TryFrom ;
40
40
41
41
use std:: fmt:: { self , Debug , Display , Formatter } ;
42
- use std:: ops:: { Add , AddAssign , Mul , MulAssign } ;
42
+ use std:: ops:: { Add , AddAssign , Mul , MulAssign , Sub , SubAssign } ;
43
43
44
44
/// byte size for 1 byte
45
45
pub const B : u64 = 1 ;
@@ -284,6 +284,43 @@ where
284
284
}
285
285
}
286
286
287
+ impl Sub < ByteSize > for ByteSize {
288
+ type Output = ByteSize ;
289
+
290
+ #[ inline( always) ]
291
+ fn sub ( self , rhs : ByteSize ) -> ByteSize {
292
+ ByteSize ( self . 0 - rhs. 0 )
293
+ }
294
+ }
295
+
296
+ impl SubAssign < ByteSize > for ByteSize {
297
+ #[ inline( always) ]
298
+ fn sub_assign ( & mut self , rhs : ByteSize ) {
299
+ self . 0 -= rhs. 0
300
+ }
301
+ }
302
+
303
+ impl < T > Sub < T > for ByteSize
304
+ where
305
+ T : Into < u64 > ,
306
+ {
307
+ type Output = ByteSize ;
308
+ #[ inline( always) ]
309
+ fn sub ( self , rhs : T ) -> ByteSize {
310
+ ByteSize ( self . 0 - ( rhs. into ( ) ) )
311
+ }
312
+ }
313
+
314
+ impl < T > SubAssign < T > for ByteSize
315
+ where
316
+ T : Into < u64 > ,
317
+ {
318
+ #[ inline( always) ]
319
+ fn sub_assign ( & mut self , rhs : T ) {
320
+ self . 0 -= rhs. into ( ) ;
321
+ }
322
+ }
323
+
287
324
impl < T > Mul < T > for ByteSize
288
325
where
289
326
T : Into < u64 > ,
@@ -380,6 +417,8 @@ mod tests {
380
417
381
418
assert_eq ! ( ( x + y) . as_u64( ) , 1_100_000u64 ) ;
382
419
420
+ assert_eq ! ( ( x - y) . as_u64( ) , 900_000u64 ) ;
421
+
383
422
assert_eq ! ( ( x + ( 100 * 1000 ) as u64 ) . as_u64( ) , 1_100_000 ) ;
384
423
385
424
assert_eq ! ( ( x * 2u64 ) . as_u64( ) , 2_000_000 ) ;
@@ -403,6 +442,14 @@ mod tests {
403
442
404
443
assert_eq ! ( ( x + B as u8 ) . as_u64( ) , 1_000_001 ) ;
405
444
445
+ assert_eq ! ( ( x - MB as u64 ) . as_u64( ) , 0 ) ;
446
+
447
+ assert_eq ! ( ( x - MB as u32 ) . as_u64( ) , 0 ) ;
448
+
449
+ assert_eq ! ( ( x - KB as u32 ) . as_u64( ) , 999_000 ) ;
450
+
451
+ assert_eq ! ( ( x - B as u32 ) . as_u64( ) , 999_999 ) ;
452
+
406
453
x += MB as u64 ;
407
454
x += MB as u32 ;
408
455
x += 10u16 ;
0 commit comments