Skip to content

Commit

Permalink
Add Uint{64,128,256,512}::panicking_add
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Apr 10, 2024
1 parent 242cc1f commit a74aba0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ and this project adheres to
`Uint512::add` ([#2092])
- cosmwasm-std: Add `{CosmosMsg,SubMsg,Response}::change_custom` to change the
custom message type ([#2099])
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the
`Sub` implementation but `const`.
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub`
which are like the `Add`/`Sub` implementations but `const`.
- cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and
document panicking behaviour.

Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ impl Uint128 {
Self(self.0.saturating_pow(exp))
}

/// This is the same as [`Uint128::add`] but const.
///
/// Panics on overflow.
#[must_use = "this returns the result of the operation, without modifying the original"]
pub const fn panicking_add(self, other: Self) -> Self {
match self.0.checked_add(other.u128()) {
None => panic!("attempt to add with overflow"),
Some(sum) => Self(sum),
}
}

/// This is the same as [`Uint128::sub`] but const.
///
/// Panics on overflow.
Expand Down Expand Up @@ -383,11 +394,7 @@ impl Add<Uint128> for Uint128 {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self(
self.u128()
.checked_add(rhs.u128())
.expect("attempt to add with overflow"),
)
self.panicking_add(rhs)
}
}
forward_ref_binop!(impl Add, add for Uint128, Uint128);
Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ impl Uint256 {
Self(self.0.saturating_pow(exp))
}

/// This is the same as [`Uint256::add`] but const.
///
/// Panics on overflow.
#[must_use = "this returns the result of the operation, without modifying the original"]
pub const fn panicking_add(self, other: Self) -> Self {
match self.0.checked_add(other.0) {
None => panic!("attempt to add with overflow"),
Some(sum) => Self(sum),
}
}

/// This is the same as [`Uint256::sub`] but const.
///
/// Panics on overflow.
Expand Down Expand Up @@ -453,11 +464,7 @@ impl Add<Uint256> for Uint256 {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self(
self.0
.checked_add(rhs.0)
.expect("attempt to add with overflow"),
)
self.panicking_add(rhs)
}
}
forward_ref_binop!(impl Add, add for Uint256, Uint256);
Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ impl Uint512 {
Self(self.0.saturating_pow(exp))
}

/// This is the same as [`Uint512::add`] but const.
///
/// Panics on overflow.
#[must_use = "this returns the result of the operation, without modifying the original"]
pub const fn panicking_add(self, other: Self) -> Self {
match self.0.checked_add(other.0) {
None => panic!("attempt to add with overflow"),
Some(sum) => Self(sum),
}
}

/// This is the same as [`Uint512::sub`] but const.
///
/// Panics on overflow.
Expand Down Expand Up @@ -432,11 +443,7 @@ impl Add<Uint512> for Uint512 {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self(
self.0
.checked_add(rhs.0)
.expect("attempt to add with overflow"),
)
self.panicking_add(rhs)
}
}
forward_ref_binop!(impl Add, add for Uint512, Uint512);
Expand Down
17 changes: 12 additions & 5 deletions packages/core/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ impl Uint64 {
Self(self.0.saturating_pow(exp))
}

/// This is the same as [`Uint64::add`] but const.
///
/// Panics on overflow.
#[must_use = "this returns the result of the operation, without modifying the original"]
pub const fn panicking_add(self, other: Self) -> Self {
match self.0.checked_add(other.u64()) {
None => panic!("attempt to add with overflow"),
Some(sum) => Self(sum),
}
}

/// This is the same as [`Uint64::sub`] but const.
///
/// Panics on overflow.
Expand Down Expand Up @@ -356,11 +367,7 @@ impl Add<Uint64> for Uint64 {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Self(
self.u64()
.checked_add(rhs.u64())
.expect("attempt to add with overflow"),
)
self.panicking_add(rhs)
}
}
forward_ref_binop!(impl Add, add for Uint64, Uint64);
Expand Down

0 comments on commit a74aba0

Please sign in to comment.