Skip to content

Commit 33484bb

Browse files
Rename math functions to strict_add/strict_sub (backport #2107) (#2109)
* Rename math functions to strict_add/strict_sub (#2107) (cherry picked from commit 3b59561) # Conflicts: # CHANGELOG.md * Fix CHANGELOG * Add ### Changed section to Unreleased CHANGELOG --------- Co-authored-by: Simon Warta <[email protected]> Co-authored-by: Simon Warta <[email protected]>
1 parent 56edb12 commit 33484bb

File tree

6 files changed

+129
-58
lines changed

6 files changed

+129
-58
lines changed

CHANGELOG.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ and this project adheres to
1111
- cosmwasm-std: Implement `&T + T` and `&T op &T` for `Uint64`, `Uint128`,
1212
`Uint256` and `Uint512`; improve panic message for `Uint64::add` and
1313
`Uint512::add` ([#2092])
14-
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub`
15-
which are like the `Add`/`Sub` implementations but `const`. ([#2098])
16-
- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use
17-
`Uint64::panicking_add`/`::panicking_sub` and document overflows. ([#2098])
14+
- cosmwasm-std: Add `Uint{64,128,256,512}::strict_add` and `::strict_sub` which
15+
are like the `Add`/`Sub` implementations but `const`. ([#2098], [#2107])
1816

1917
[#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092
2018
[#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098
19+
[#2107]: https://github.com/CosmWasm/cosmwasm/pull/2107
20+
21+
### Changed
22+
23+
- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use
24+
`Uint64::strict_add`/`::strict_sub` and document overflows. ([#2098], [#2107])
25+
26+
[#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098
27+
[#2107]: https://github.com/CosmWasm/cosmwasm/pull/2107
2128

2229
## [2.0.1] - 2024-04-03
2330

packages/std/src/math/uint128.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -263,22 +263,22 @@ impl Uint128 {
263263
Self(self.0.saturating_pow(exp))
264264
}
265265

266-
/// This is the same as [`Uint128::add`] but const.
266+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
267267
///
268-
/// Panics on overflow.
268+
/// This is the same as [`Uint128::add`] but const.
269269
#[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()) {
270+
pub const fn strict_add(self, rhs: Self) -> Self {
271+
match self.0.checked_add(rhs.u128()) {
272272
None => panic!("attempt to add with overflow"),
273273
Some(sum) => Self(sum),
274274
}
275275
}
276276

277-
/// This is the same as [`Uint128::sub`] but const.
277+
/// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred.
278278
///
279-
/// Panics on overflow.
279+
/// This is the same as [`Uint128::sub`] but const.
280280
#[must_use = "this returns the result of the operation, without modifying the original"]
281-
pub const fn panicking_sub(self, other: Self) -> Self {
281+
pub const fn strict_sub(self, other: Self) -> Self {
282282
match self.0.checked_sub(other.u128()) {
283283
None => panic!("attempt to subtract with overflow"),
284284
Some(diff) => Self(diff),
@@ -394,7 +394,7 @@ impl Add<Uint128> for Uint128 {
394394
type Output = Self;
395395

396396
fn add(self, rhs: Self) -> Self {
397-
self.panicking_add(rhs)
397+
self.strict_add(rhs)
398398
}
399399
}
400400
forward_ref_binop!(impl Add, add for Uint128, Uint128);
@@ -403,7 +403,7 @@ impl Sub<Uint128> for Uint128 {
403403
type Output = Self;
404404

405405
fn sub(self, rhs: Self) -> Self {
406-
self.panicking_sub(rhs)
406+
self.strict_sub(rhs)
407407
}
408408
}
409409
forward_ref_binop!(impl Sub, sub for Uint128, Uint128);
@@ -1191,18 +1191,34 @@ mod tests {
11911191
}
11921192

11931193
#[test]
1194-
fn uint128_panicking_sub_works() {
1194+
fn uint128_strict_add_works() {
1195+
let a = Uint128::new(5);
1196+
let b = Uint128::new(3);
1197+
assert_eq!(a.strict_add(b), Uint128::new(8));
1198+
assert_eq!(b.strict_add(a), Uint128::new(8));
1199+
}
1200+
1201+
#[test]
1202+
#[should_panic(expected = "attempt to add with overflow")]
1203+
fn uint128_strict_add_panics_on_overflow() {
1204+
let a = Uint128::MAX;
1205+
let b = Uint128::ONE;
1206+
let _ = a.strict_add(b);
1207+
}
1208+
1209+
#[test]
1210+
fn uint128_strict_sub_works() {
11951211
let a = Uint128::new(5);
11961212
let b = Uint128::new(3);
1197-
assert_eq!(a.panicking_sub(b), Uint128::new(2));
1213+
assert_eq!(a.strict_sub(b), Uint128::new(2));
11981214
}
11991215

12001216
#[test]
12011217
#[should_panic(expected = "attempt to subtract with overflow")]
1202-
fn uint128_panicking_sub_panics_on_overflow() {
1218+
fn uint128_strict_sub_panics_on_overflow() {
12031219
let a = Uint128::ZERO;
12041220
let b = Uint128::ONE;
1205-
let _diff = a.panicking_sub(b);
1221+
let _ = a.strict_sub(b);
12061222
}
12071223

12081224
#[test]

packages/std/src/math/uint256.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -335,22 +335,22 @@ impl Uint256 {
335335
Self(self.0.saturating_pow(exp))
336336
}
337337

338-
/// This is the same as [`Uint256::add`] but const.
338+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
339339
///
340-
/// Panics on overflow.
340+
/// This is the same as [`Uint256::add`] but const.
341341
#[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) {
342+
pub const fn strict_add(self, rhs: Self) -> Self {
343+
match self.0.checked_add(rhs.0) {
344344
None => panic!("attempt to add with overflow"),
345345
Some(sum) => Self(sum),
346346
}
347347
}
348348

349-
/// This is the same as [`Uint256::sub`] but const.
349+
/// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred.
350350
///
351-
/// Panics on overflow.
351+
/// This is the same as [`Uint256::sub`] but const.
352352
#[must_use = "this returns the result of the operation, without modifying the original"]
353-
pub const fn panicking_sub(self, other: Self) -> Self {
353+
pub const fn strict_sub(self, other: Self) -> Self {
354354
match self.0.checked_sub(other.0) {
355355
None => panic!("attempt to subtract with overflow"),
356356
Some(diff) => Self(diff),
@@ -462,7 +462,7 @@ impl Add<Uint256> for Uint256 {
462462
type Output = Self;
463463

464464
fn add(self, rhs: Self) -> Self {
465-
self.panicking_add(rhs)
465+
self.strict_add(rhs)
466466
}
467467
}
468468
forward_ref_binop!(impl Add, add for Uint256, Uint256);
@@ -471,7 +471,7 @@ impl Sub<Uint256> for Uint256 {
471471
type Output = Self;
472472

473473
fn sub(self, rhs: Self) -> Self {
474-
self.panicking_sub(rhs)
474+
self.strict_sub(rhs)
475475
}
476476
}
477477
forward_ref_binop!(impl Sub, sub for Uint256, Uint256);
@@ -1738,18 +1738,34 @@ mod tests {
17381738
}
17391739

17401740
#[test]
1741-
fn uint256_panicking_sub_works() {
1741+
fn uint256_strict_add_works() {
1742+
let a = Uint256::from(5u32);
1743+
let b = Uint256::from(3u32);
1744+
assert_eq!(a.strict_add(b), Uint256::from(8u32));
1745+
assert_eq!(b.strict_add(a), Uint256::from(8u32));
1746+
}
1747+
1748+
#[test]
1749+
#[should_panic(expected = "attempt to add with overflow")]
1750+
fn uint256_strict_add_panics_on_overflow() {
1751+
let a = Uint256::MAX;
1752+
let b = Uint256::ONE;
1753+
let _ = a.strict_add(b);
1754+
}
1755+
1756+
#[test]
1757+
fn uint256_strict_sub_works() {
17421758
let a = Uint256::from(5u32);
17431759
let b = Uint256::from(3u32);
1744-
assert_eq!(a.panicking_sub(b), Uint256::from(2u32));
1760+
assert_eq!(a.strict_sub(b), Uint256::from(2u32));
17451761
}
17461762

17471763
#[test]
17481764
#[should_panic(expected = "attempt to subtract with overflow")]
1749-
fn uint256_panicking_sub_panics_on_overflow() {
1765+
fn uint256_strict_sub_panics_on_overflow() {
17501766
let a = Uint256::ZERO;
17511767
let b = Uint256::ONE;
1752-
let _diff = a.panicking_sub(b);
1768+
let _ = a.strict_sub(b);
17531769
}
17541770

17551771
#[test]

packages/std/src/math/uint512.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,22 @@ impl Uint512 {
298298
Self(self.0.saturating_pow(exp))
299299
}
300300

301-
/// This is the same as [`Uint512::add`] but const.
301+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
302302
///
303-
/// Panics on overflow.
303+
/// This is the same as [`Uint512::add`] but const.
304304
#[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) {
305+
pub const fn strict_add(self, rhs: Self) -> Self {
306+
match self.0.checked_add(rhs.0) {
307307
None => panic!("attempt to add with overflow"),
308308
Some(sum) => Self(sum),
309309
}
310310
}
311311

312-
/// This is the same as [`Uint512::sub`] but const.
312+
/// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred.
313313
///
314-
/// Panics on overflow.
314+
/// This is the same as [`Uint512::sub`] but const.
315315
#[must_use = "this returns the result of the operation, without modifying the original"]
316-
pub const fn panicking_sub(self, other: Self) -> Self {
316+
pub const fn strict_sub(self, other: Self) -> Self {
317317
match self.0.checked_sub(other.0) {
318318
None => panic!("attempt to subtract with overflow"),
319319
Some(diff) => Self(diff),
@@ -443,7 +443,7 @@ impl Add<Uint512> for Uint512 {
443443
type Output = Self;
444444

445445
fn add(self, rhs: Self) -> Self {
446-
self.panicking_add(rhs)
446+
self.strict_add(rhs)
447447
}
448448
}
449449
forward_ref_binop!(impl Add, add for Uint512, Uint512);
@@ -452,7 +452,7 @@ impl Sub<Uint512> for Uint512 {
452452
type Output = Self;
453453

454454
fn sub(self, rhs: Self) -> Self {
455-
self.panicking_sub(rhs)
455+
self.strict_sub(rhs)
456456
}
457457
}
458458
forward_ref_binop!(impl Sub, sub for Uint512, Uint512);
@@ -1389,18 +1389,34 @@ mod tests {
13891389
}
13901390

13911391
#[test]
1392-
fn uint512_panicking_sub_works() {
1392+
fn uint512_strict_add_works() {
1393+
let a = Uint512::from(5u32);
1394+
let b = Uint512::from(3u32);
1395+
assert_eq!(a.strict_add(b), Uint512::from(8u32));
1396+
assert_eq!(b.strict_add(a), Uint512::from(8u32));
1397+
}
1398+
1399+
#[test]
1400+
#[should_panic(expected = "attempt to add with overflow")]
1401+
fn uint512_strict_add_panics_on_overflow() {
1402+
let a = Uint512::MAX;
1403+
let b = Uint512::ONE;
1404+
let _ = a.strict_add(b);
1405+
}
1406+
1407+
#[test]
1408+
fn uint512_strict_sub_works() {
13931409
let a = Uint512::from(5u32);
13941410
let b = Uint512::from(3u32);
1395-
assert_eq!(a.panicking_sub(b), Uint512::from(2u32));
1411+
assert_eq!(a.strict_sub(b), Uint512::from(2u32));
13961412
}
13971413

13981414
#[test]
13991415
#[should_panic(expected = "attempt to subtract with overflow")]
1400-
fn uint512_panicking_sub_panics_on_overflow() {
1416+
fn uint512_strict_sub_panics_on_overflow() {
14011417
let a = Uint512::ZERO;
14021418
let b = Uint512::ONE;
1403-
let _diff = a.panicking_sub(b);
1419+
let _ = a.strict_sub(b);
14041420
}
14051421

14061422
#[test]

packages/std/src/math/uint64.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -257,22 +257,22 @@ impl Uint64 {
257257
Self(self.0.saturating_pow(exp))
258258
}
259259

260-
/// This is the same as [`Uint64::add`] but const.
260+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
261261
///
262-
/// Panics on overflow.
262+
/// This is the same as [`Uint64::add`] but const.
263263
#[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()) {
264+
pub const fn strict_add(self, rhs: Self) -> Self {
265+
match self.0.checked_add(rhs.u64()) {
266266
None => panic!("attempt to add with overflow"),
267267
Some(sum) => Self(sum),
268268
}
269269
}
270270

271-
/// This is the same as [`Uint64::sub`] but const.
271+
/// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred.
272272
///
273-
/// Panics on overflow.
273+
/// This is the same as [`Uint64::sub`] but const.
274274
#[must_use = "this returns the result of the operation, without modifying the original"]
275-
pub const fn panicking_sub(self, other: Self) -> Self {
275+
pub const fn strict_sub(self, other: Self) -> Self {
276276
match self.0.checked_sub(other.u64()) {
277277
None => panic!("attempt to subtract with overflow"),
278278
Some(diff) => Self(diff),
@@ -367,7 +367,7 @@ impl Add<Uint64> for Uint64 {
367367
type Output = Self;
368368

369369
fn add(self, rhs: Self) -> Self {
370-
self.panicking_add(rhs)
370+
self.strict_add(rhs)
371371
}
372372
}
373373
forward_ref_binop!(impl Add, add for Uint64, Uint64);
@@ -376,7 +376,7 @@ impl Sub<Uint64> for Uint64 {
376376
type Output = Self;
377377

378378
fn sub(self, rhs: Self) -> Self {
379-
self.panicking_sub(rhs)
379+
self.strict_sub(rhs)
380380
}
381381
}
382382
forward_ref_binop!(impl Sub, sub for Uint64, Uint64);
@@ -1103,18 +1103,34 @@ mod tests {
11031103
}
11041104

11051105
#[test]
1106-
fn uint64_panicking_sub_works() {
1106+
fn uint64_strict_add_works() {
1107+
let a = Uint64::new(5);
1108+
let b = Uint64::new(3);
1109+
assert_eq!(a.strict_add(b), Uint64::new(8));
1110+
assert_eq!(b.strict_add(a), Uint64::new(8));
1111+
}
1112+
1113+
#[test]
1114+
#[should_panic(expected = "attempt to add with overflow")]
1115+
fn uint64_strict_add_panics_on_overflow() {
1116+
let a = Uint64::MAX;
1117+
let b = Uint64::ONE;
1118+
let _ = a.strict_add(b);
1119+
}
1120+
1121+
#[test]
1122+
fn uint64_strict_sub_works() {
11071123
let a = Uint64::new(5);
11081124
let b = Uint64::new(3);
1109-
assert_eq!(a.panicking_sub(b), Uint64::new(2));
1125+
assert_eq!(a.strict_sub(b), Uint64::new(2));
11101126
}
11111127

11121128
#[test]
11131129
#[should_panic(expected = "attempt to subtract with overflow")]
1114-
fn uint64_panicking_sub_panics_on_overflow() {
1130+
fn uint64_strict_sub_panics_on_overflow() {
11151131
let a = Uint64::ZERO;
11161132
let b = Uint64::ONE;
1117-
let _diff = a.panicking_sub(b);
1133+
let _ = a.strict_sub(b);
11181134
}
11191135

11201136
#[test]

packages/std/src/timestamp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl Timestamp {
8686
#[must_use = "this returns the result of the operation, without modifying the original"]
8787
// no #[inline] here as this could be shared with all the callers
8888
pub const fn plus_nanos(&self, addition: u64) -> Timestamp {
89-
let nanos = self.0.panicking_add(Uint64::new(addition));
89+
let nanos = self.0.strict_add(Uint64::new(addition));
9090
Timestamp(nanos)
9191
}
9292

@@ -137,7 +137,7 @@ impl Timestamp {
137137
#[must_use = "this returns the result of the operation, without modifying the original"]
138138
// no #[inline] here as this could be shared with all the callers
139139
pub const fn minus_nanos(&self, subtrahend: u64) -> Timestamp {
140-
Timestamp(self.0.panicking_sub(Uint64::new(subtrahend)))
140+
Timestamp(self.0.strict_sub(Uint64::new(subtrahend)))
141141
}
142142

143143
/// Returns nanoseconds since epoch

0 commit comments

Comments
 (0)