@@ -34,7 +34,7 @@ mod parse;
3434extern crate serde;
3535
3636use std:: fmt:: { Debug , Display , Formatter , Result } ;
37- use std:: ops:: { Add , Mul } ;
37+ use std:: ops:: { Add , AddAssign , Mul , MulAssign } ;
3838
3939/// byte size for 1 byte
4040pub const B : u64 = 1 ;
@@ -219,14 +219,6 @@ impl Debug for ByteSize {
219219
220220macro_rules! commutative_op {
221221 ( $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-
230222 impl Add <ByteSize > for $t {
231223 type Output = ByteSize ;
232224 #[ inline( always) ]
@@ -235,14 +227,6 @@ macro_rules! commutative_op {
235227 }
236228 }
237229
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-
246230 impl Mul <ByteSize > for $t {
247231 type Output = ByteSize ;
248232 #[ inline( always) ]
@@ -267,25 +251,71 @@ impl Add<ByteSize> for ByteSize {
267251 }
268252}
269253
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+
270295#[ cfg( test) ]
271296mod tests {
272297 use super :: * ;
273298
274299 #[ test]
275300 fn test_arithmetic_op ( ) {
276- let x = ByteSize :: mb ( 1 ) ;
301+ let mut x = ByteSize :: mb ( 1 ) ;
277302 let y = ByteSize :: kb ( 100 ) ;
278303
279304 assert_eq ! ( ( x + y) . as_u64( ) , 1_100_000u64 ) ;
280305
281306 assert_eq ! ( ( x + ( 100 * 1000 ) as u64 ) . as_u64( ) , 1_100_000 ) ;
282307
283308 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 ) ;
284314 }
285315
286316 #[ test]
287317 fn test_arithmetic_primitives ( ) {
288- let x = ByteSize :: mb ( 1 ) ;
318+ let mut x = ByteSize :: mb ( 1 ) ;
289319
290320 assert_eq ! ( ( x + MB as u64 ) . as_u64( ) , 2_000_000 ) ;
291321
@@ -294,6 +324,12 @@ mod tests {
294324 assert_eq ! ( ( x + KB as u16 ) . as_u64( ) , 1_001_000 ) ;
295325
296326 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 ) ;
297333 }
298334
299335 #[ test]
0 commit comments