diff --git a/CHANGELOG.md b/CHANGELOG.md index e75326db2c..7ebd2b846d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/packages/core/src/math/uint128.rs b/packages/core/src/math/uint128.rs index 18bacd0887..b95c5bafe8 100644 --- a/packages/core/src/math/uint128.rs +++ b/packages/core/src/math/uint128.rs @@ -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. @@ -383,11 +394,7 @@ impl Add 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); diff --git a/packages/core/src/math/uint256.rs b/packages/core/src/math/uint256.rs index 0a8710219b..92fbaf2928 100644 --- a/packages/core/src/math/uint256.rs +++ b/packages/core/src/math/uint256.rs @@ -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. @@ -453,11 +464,7 @@ impl Add 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); diff --git a/packages/core/src/math/uint512.rs b/packages/core/src/math/uint512.rs index 45f1217426..e4640d7b2e 100644 --- a/packages/core/src/math/uint512.rs +++ b/packages/core/src/math/uint512.rs @@ -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. @@ -432,11 +443,7 @@ impl Add 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); diff --git a/packages/core/src/math/uint64.rs b/packages/core/src/math/uint64.rs index 688af264ac..ea684229c5 100644 --- a/packages/core/src/math/uint64.rs +++ b/packages/core/src/math/uint64.rs @@ -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. @@ -356,11 +367,7 @@ impl Add 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);