@@ -206,6 +206,22 @@ impl<'a> ops::Add<&'a Uint128> for Uint128 {
206
206
}
207
207
}
208
208
209
+ impl ops:: Sub < Uint128 > for Uint128 {
210
+ type Output = Self ;
211
+
212
+ fn sub ( self , rhs : Self ) -> Self {
213
+ Uint128 ( self . u128 ( ) . checked_sub ( rhs. u128 ( ) ) . unwrap ( ) )
214
+ }
215
+ }
216
+
217
+ impl < ' a > ops:: Sub < & ' a Uint128 > for Uint128 {
218
+ type Output = Self ;
219
+
220
+ fn sub ( self , rhs : & ' a Uint128 ) -> Self {
221
+ Uint128 ( self . u128 ( ) . checked_sub ( rhs. u128 ( ) ) . unwrap ( ) )
222
+ }
223
+ }
224
+
209
225
impl ops:: AddAssign < Uint128 > for Uint128 {
210
226
fn add_assign ( & mut self , rhs : Uint128 ) {
211
227
self . 0 = self . 0 . checked_add ( rhs. u128 ( ) ) . unwrap ( ) ;
@@ -218,6 +234,18 @@ impl<'a> ops::AddAssign<&'a Uint128> for Uint128 {
218
234
}
219
235
}
220
236
237
+ impl ops:: SubAssign < Uint128 > for Uint128 {
238
+ fn sub_assign ( & mut self , rhs : Uint128 ) {
239
+ self . 0 = self . 0 . checked_sub ( rhs. u128 ( ) ) . unwrap ( ) ;
240
+ }
241
+ }
242
+
243
+ impl < ' a > ops:: SubAssign < & ' a Uint128 > for Uint128 {
244
+ fn sub_assign ( & mut self , rhs : & ' a Uint128 ) {
245
+ self . 0 = self . 0 . checked_sub ( rhs. u128 ( ) ) . unwrap ( ) ;
246
+ }
247
+ }
248
+
221
249
impl Uint128 {
222
250
/// Returns `self * numerator / denominator`
223
251
pub fn multiply_ratio < A : Into < u128 > , B : Into < u128 > > (
@@ -397,7 +425,8 @@ mod tests {
397
425
assert_eq ! ( a + & b, Uint128 ( 35801 ) ) ;
398
426
399
427
// test - with owned and reference right hand side
400
- assert_eq ! ( ( b. checked_sub( a) ) . unwrap( ) , Uint128 ( 11111 ) ) ;
428
+ assert_eq ! ( b - a, Uint128 ( 11111 ) ) ;
429
+ assert_eq ! ( b - & a, Uint128 ( 11111 ) ) ;
401
430
402
431
// test += with owned and reference right hand side
403
432
let mut c = Uint128 ( 300000 ) ;
@@ -407,6 +436,14 @@ mod tests {
407
436
d += & b;
408
437
assert_eq ! ( d, Uint128 ( 323456 ) ) ;
409
438
439
+ // test -= with owned and reference right hand side
440
+ let mut c = Uint128 ( 300000 ) ;
441
+ c -= b;
442
+ assert_eq ! ( c, Uint128 ( 276544 ) ) ;
443
+ let mut d = Uint128 ( 300000 ) ;
444
+ d -= & b;
445
+ assert_eq ! ( d, Uint128 ( 276544 ) ) ;
446
+
410
447
// error result on underflow (- would produce negative result)
411
448
let underflow_result = a. checked_sub ( b) ;
412
449
let OverflowError {
@@ -417,12 +454,18 @@ mod tests {
417
454
418
455
#[ test]
419
456
#[ should_panic]
420
- fn uint128_math_overflow_panics ( ) {
457
+ fn uint128_add_overflow_panics ( ) {
421
458
// almost_max is 2^128 - 10
422
459
let almost_max = Uint128 ( 340282366920938463463374607431768211446 ) ;
423
460
let _ = almost_max + Uint128 ( 12 ) ;
424
461
}
425
462
463
+ #[ test]
464
+ #[ should_panic]
465
+ fn uint128_sub_overflow_panics ( ) {
466
+ let _ = Uint128 ( 1 ) - Uint128 ( 2 ) ;
467
+ }
468
+
426
469
#[ test]
427
470
fn uint128_multiply_ratio_works ( ) {
428
471
let base = Uint128 ( 500 ) ;
0 commit comments