Skip to content

Commit ff0fab5

Browse files
authored
Merge pull request #936 from CosmWasm/impl-sub-for-uint128
Implement Sub and SubAssign for Uint128 (closes #858)
2 parents 76a23fa + 5edb2c0 commit ff0fab5

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- cosmwasm-std: Implement `Sub` and `SubAssign` for `Uint128`
12+
913
### Removed
1014

1115
- cosmwasm-std: Make `Uint128` inner field private ([#905])

packages/std/src/math/uint128.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ impl<'a> ops::Add<&'a Uint128> for Uint128 {
206206
}
207207
}
208208

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+
209225
impl ops::AddAssign<Uint128> for Uint128 {
210226
fn add_assign(&mut self, rhs: Uint128) {
211227
self.0 = self.0.checked_add(rhs.u128()).unwrap();
@@ -218,6 +234,18 @@ impl<'a> ops::AddAssign<&'a Uint128> for Uint128 {
218234
}
219235
}
220236

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+
221249
impl Uint128 {
222250
/// Returns `self * numerator / denominator`
223251
pub fn multiply_ratio<A: Into<u128>, B: Into<u128>>(
@@ -397,7 +425,8 @@ mod tests {
397425
assert_eq!(a + &b, Uint128(35801));
398426

399427
// 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));
401430

402431
// test += with owned and reference right hand side
403432
let mut c = Uint128(300000);
@@ -407,6 +436,14 @@ mod tests {
407436
d += &b;
408437
assert_eq!(d, Uint128(323456));
409438

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+
410447
// error result on underflow (- would produce negative result)
411448
let underflow_result = a.checked_sub(b);
412449
let OverflowError {
@@ -417,12 +454,18 @@ mod tests {
417454

418455
#[test]
419456
#[should_panic]
420-
fn uint128_math_overflow_panics() {
457+
fn uint128_add_overflow_panics() {
421458
// almost_max is 2^128 - 10
422459
let almost_max = Uint128(340282366920938463463374607431768211446);
423460
let _ = almost_max + Uint128(12);
424461
}
425462

463+
#[test]
464+
#[should_panic]
465+
fn uint128_sub_overflow_panics() {
466+
let _ = Uint128(1) - Uint128(2);
467+
}
468+
426469
#[test]
427470
fn uint128_multiply_ratio_works() {
428471
let base = Uint128(500);

0 commit comments

Comments
 (0)