Skip to content

Commit a74aba0

Browse files
committed
Add Uint{64,128,256,512}::panicking_add
1 parent 242cc1f commit a74aba0

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ and this project adheres to
2323
`Uint512::add` ([#2092])
2424
- cosmwasm-std: Add `{CosmosMsg,SubMsg,Response}::change_custom` to change the
2525
custom message type ([#2099])
26-
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the
27-
`Sub` implementation but `const`.
26+
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub`
27+
which are like the `Add`/`Sub` implementations but `const`.
2828
- cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and
2929
document panicking behaviour.
3030

packages/core/src/math/uint128.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ impl Uint128 {
263263
Self(self.0.saturating_pow(exp))
264264
}
265265

266+
/// This is the same as [`Uint128::add`] but const.
267+
///
268+
/// Panics on overflow.
269+
#[must_use = "this returns the result of the operation, without modifying the original"]
270+
pub const fn panicking_add(self, other: Self) -> Self {
271+
match self.0.checked_add(other.u128()) {
272+
None => panic!("attempt to add with overflow"),
273+
Some(sum) => Self(sum),
274+
}
275+
}
276+
266277
/// This is the same as [`Uint128::sub`] but const.
267278
///
268279
/// Panics on overflow.
@@ -383,11 +394,7 @@ impl Add<Uint128> for Uint128 {
383394
type Output = Self;
384395

385396
fn add(self, rhs: Self) -> Self {
386-
Self(
387-
self.u128()
388-
.checked_add(rhs.u128())
389-
.expect("attempt to add with overflow"),
390-
)
397+
self.panicking_add(rhs)
391398
}
392399
}
393400
forward_ref_binop!(impl Add, add for Uint128, Uint128);

packages/core/src/math/uint256.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,17 @@ impl Uint256 {
335335
Self(self.0.saturating_pow(exp))
336336
}
337337

338+
/// This is the same as [`Uint256::add`] but const.
339+
///
340+
/// Panics on overflow.
341+
#[must_use = "this returns the result of the operation, without modifying the original"]
342+
pub const fn panicking_add(self, other: Self) -> Self {
343+
match self.0.checked_add(other.0) {
344+
None => panic!("attempt to add with overflow"),
345+
Some(sum) => Self(sum),
346+
}
347+
}
348+
338349
/// This is the same as [`Uint256::sub`] but const.
339350
///
340351
/// Panics on overflow.
@@ -453,11 +464,7 @@ impl Add<Uint256> for Uint256 {
453464
type Output = Self;
454465

455466
fn add(self, rhs: Self) -> Self {
456-
Self(
457-
self.0
458-
.checked_add(rhs.0)
459-
.expect("attempt to add with overflow"),
460-
)
467+
self.panicking_add(rhs)
461468
}
462469
}
463470
forward_ref_binop!(impl Add, add for Uint256, Uint256);

packages/core/src/math/uint512.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ impl Uint512 {
298298
Self(self.0.saturating_pow(exp))
299299
}
300300

301+
/// This is the same as [`Uint512::add`] but const.
302+
///
303+
/// Panics on overflow.
304+
#[must_use = "this returns the result of the operation, without modifying the original"]
305+
pub const fn panicking_add(self, other: Self) -> Self {
306+
match self.0.checked_add(other.0) {
307+
None => panic!("attempt to add with overflow"),
308+
Some(sum) => Self(sum),
309+
}
310+
}
311+
301312
/// This is the same as [`Uint512::sub`] but const.
302313
///
303314
/// Panics on overflow.
@@ -432,11 +443,7 @@ impl Add<Uint512> for Uint512 {
432443
type Output = Self;
433444

434445
fn add(self, rhs: Self) -> Self {
435-
Self(
436-
self.0
437-
.checked_add(rhs.0)
438-
.expect("attempt to add with overflow"),
439-
)
446+
self.panicking_add(rhs)
440447
}
441448
}
442449
forward_ref_binop!(impl Add, add for Uint512, Uint512);

packages/core/src/math/uint64.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ impl Uint64 {
257257
Self(self.0.saturating_pow(exp))
258258
}
259259

260+
/// This is the same as [`Uint64::add`] but const.
261+
///
262+
/// Panics on overflow.
263+
#[must_use = "this returns the result of the operation, without modifying the original"]
264+
pub const fn panicking_add(self, other: Self) -> Self {
265+
match self.0.checked_add(other.u64()) {
266+
None => panic!("attempt to add with overflow"),
267+
Some(sum) => Self(sum),
268+
}
269+
}
270+
260271
/// This is the same as [`Uint64::sub`] but const.
261272
///
262273
/// Panics on overflow.
@@ -356,11 +367,7 @@ impl Add<Uint64> for Uint64 {
356367
type Output = Self;
357368

358369
fn add(self, rhs: Self) -> Self {
359-
Self(
360-
self.u64()
361-
.checked_add(rhs.u64())
362-
.expect("attempt to add with overflow"),
363-
)
370+
self.panicking_add(rhs)
364371
}
365372
}
366373
forward_ref_binop!(impl Add, add for Uint64, Uint64);

0 commit comments

Comments
 (0)