Skip to content

Commit dc1593d

Browse files
Rename math functions to strict_add/strict_sub (backport #2107) (#2111)
* Rename math functions to strict_add/strict_sub (#2107) (cherry picked from commit 3b59561) # Conflicts: # CHANGELOG.md # packages/std/src/math/uint128.rs # packages/std/src/math/uint256.rs # packages/std/src/math/uint512.rs # packages/std/src/math/uint64.rs * Fix test code from merge * Fix CHANGELOG --------- Co-authored-by: Simon Warta <[email protected]> Co-authored-by: Simon Warta <[email protected]>
1 parent e29dbf7 commit dc1593d

File tree

6 files changed

+125
-58
lines changed

6 files changed

+125
-58
lines changed

CHANGELOG.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@ 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
2120

2221
### Changed
2322

2423
- cosmwasm-vm: Read `Region` from Wasm memory as bytes and convert to `Region`
2524
afterwards ([#2005])
25+
- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use
26+
`Uint64::strict_add`/`::strict_sub` and document overflows. ([#2098], [#2107])
2627

2728
[#2005]: https://github.com/CosmWasm/cosmwasm/pull/2005
29+
[#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098
30+
[#2107]: https://github.com/CosmWasm/cosmwasm/pull/2107
2831

2932
## [1.4.3] - 2024-01-18
3033

packages/std/src/math/uint128.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,22 @@ impl Uint128 {
259259
Self(self.0.saturating_pow(exp))
260260
}
261261

262-
/// This is the same as [`Uint128::add`] but const.
262+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
263263
///
264-
/// Panics on overflow.
264+
/// This is the same as [`Uint128::add`] but const.
265265
#[must_use = "this returns the result of the operation, without modifying the original"]
266-
pub const fn panicking_add(self, other: Self) -> Self {
267-
match self.0.checked_add(other.u128()) {
266+
pub const fn strict_add(self, rhs: Self) -> Self {
267+
match self.0.checked_add(rhs.u128()) {
268268
None => panic!("attempt to add with overflow"),
269269
Some(sum) => Self(sum),
270270
}
271271
}
272272

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

387387
fn add(self, rhs: Self) -> Self {
388-
self.panicking_add(rhs)
388+
self.strict_add(rhs)
389389
}
390390
}
391391
forward_ref_binop!(impl Add, add for Uint128, Uint128);
@@ -394,7 +394,7 @@ impl Sub<Uint128> for Uint128 {
394394
type Output = Self;
395395

396396
fn sub(self, rhs: Self) -> Self {
397-
self.panicking_sub(rhs)
397+
self.strict_sub(rhs)
398398
}
399399
}
400400
forward_ref_binop!(impl Sub, sub for Uint128, Uint128);
@@ -1163,18 +1163,34 @@ mod tests {
11631163
}
11641164

11651165
#[test]
1166-
fn uint128_panicking_sub_works() {
1166+
fn uint128_strict_add_works() {
1167+
let a = Uint128::new(5);
1168+
let b = Uint128::new(3);
1169+
assert_eq!(a.strict_add(b), Uint128::new(8));
1170+
assert_eq!(b.strict_add(a), Uint128::new(8));
1171+
}
1172+
1173+
#[test]
1174+
#[should_panic(expected = "attempt to add with overflow")]
1175+
fn uint128_strict_add_panics_on_overflow() {
1176+
let a = Uint128::MAX;
1177+
let b = Uint128::one();
1178+
let _ = a.strict_add(b);
1179+
}
1180+
1181+
#[test]
1182+
fn uint128_strict_sub_works() {
11671183
let a = Uint128::new(5);
11681184
let b = Uint128::new(3);
1169-
assert_eq!(a.panicking_sub(b), Uint128::new(2));
1185+
assert_eq!(a.strict_sub(b), Uint128::new(2));
11701186
}
11711187

11721188
#[test]
11731189
#[should_panic(expected = "attempt to subtract with overflow")]
1174-
fn uint128_panicking_sub_panics_on_overflow() {
1190+
fn uint128_strict_sub_panics_on_overflow() {
11751191
let a = Uint128::zero();
11761192
let b = Uint128::one();
1177-
let _diff = a.panicking_sub(b);
1193+
let _ = a.strict_sub(b);
11781194
}
11791195

11801196
#[test]

packages/std/src/math/uint256.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,22 @@ impl Uint256 {
329329
Self(self.0.saturating_pow(exp))
330330
}
331331

332-
/// This is the same as [`Uint256::add`] but const.
332+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
333333
///
334-
/// Panics on overflow.
334+
/// This is the same as [`Uint256::add`] but const.
335335
#[must_use = "this returns the result of the operation, without modifying the original"]
336-
pub const fn panicking_add(self, other: Self) -> Self {
337-
match self.0.checked_add(other.0) {
336+
pub const fn strict_add(self, rhs: Self) -> Self {
337+
match self.0.checked_add(rhs.0) {
338338
None => panic!("attempt to add with overflow"),
339339
Some(sum) => Self(sum),
340340
}
341341
}
342342

343-
/// This is the same as [`Uint256::sub`] but const.
343+
/// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred.
344344
///
345-
/// Panics on overflow.
345+
/// This is the same as [`Uint256::sub`] but const.
346346
#[must_use = "this returns the result of the operation, without modifying the original"]
347-
pub const fn panicking_sub(self, other: Self) -> Self {
347+
pub const fn strict_sub(self, other: Self) -> Self {
348348
match self.0.checked_sub(other.0) {
349349
None => panic!("attempt to subtract with overflow"),
350350
Some(diff) => Self(diff),
@@ -450,7 +450,7 @@ impl Add<Uint256> for Uint256 {
450450
type Output = Self;
451451

452452
fn add(self, rhs: Self) -> Self {
453-
self.panicking_add(rhs)
453+
self.strict_add(rhs)
454454
}
455455
}
456456
forward_ref_binop!(impl Add, add for Uint256, Uint256);
@@ -459,7 +459,7 @@ impl Sub<Uint256> for Uint256 {
459459
type Output = Self;
460460

461461
fn sub(self, rhs: Self) -> Self {
462-
self.panicking_sub(rhs)
462+
self.strict_sub(rhs)
463463
}
464464
}
465465
forward_ref_binop!(impl Sub, sub for Uint256, Uint256);
@@ -1705,18 +1705,34 @@ mod tests {
17051705
}
17061706

17071707
#[test]
1708-
fn uint256_panicking_sub_works() {
1708+
fn uint256_strict_add_works() {
1709+
let a = Uint256::from(5u32);
1710+
let b = Uint256::from(3u32);
1711+
assert_eq!(a.strict_add(b), Uint256::from(8u32));
1712+
assert_eq!(b.strict_add(a), Uint256::from(8u32));
1713+
}
1714+
1715+
#[test]
1716+
#[should_panic(expected = "attempt to add with overflow")]
1717+
fn uint256_strict_add_panics_on_overflow() {
1718+
let a = Uint256::MAX;
1719+
let b = Uint256::one();
1720+
let _ = a.strict_add(b);
1721+
}
1722+
1723+
#[test]
1724+
fn uint256_strict_sub_works() {
17091725
let a = Uint256::from(5u32);
17101726
let b = Uint256::from(3u32);
1711-
assert_eq!(a.panicking_sub(b), Uint256::from(2u32));
1727+
assert_eq!(a.strict_sub(b), Uint256::from(2u32));
17121728
}
17131729

17141730
#[test]
17151731
#[should_panic(expected = "attempt to subtract with overflow")]
1716-
fn uint256_panicking_sub_panics_on_overflow() {
1732+
fn uint256_strict_sub_panics_on_overflow() {
17171733
let a = Uint256::zero();
17181734
let b = Uint256::one();
1719-
let _diff = a.panicking_sub(b);
1735+
let _ = a.strict_sub(b);
17201736
}
17211737

17221738
#[test]

packages/std/src/math/uint512.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -294,22 +294,22 @@ impl Uint512 {
294294
Self(self.0.saturating_pow(exp))
295295
}
296296

297-
/// This is the same as [`Uint512::add`] but const.
297+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
298298
///
299-
/// Panics on overflow.
299+
/// This is the same as [`Uint512::add`] but const.
300300
#[must_use = "this returns the result of the operation, without modifying the original"]
301-
pub const fn panicking_add(self, other: Self) -> Self {
302-
match self.0.checked_add(other.0) {
301+
pub const fn strict_add(self, rhs: Self) -> Self {
302+
match self.0.checked_add(rhs.0) {
303303
None => panic!("attempt to add with overflow"),
304304
Some(sum) => Self(sum),
305305
}
306306
}
307307

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

439439
fn add(self, rhs: Self) -> Self {
440-
self.panicking_add(rhs)
440+
self.strict_add(rhs)
441441
}
442442
}
443443
forward_ref_binop!(impl Add, add for Uint512, Uint512);
@@ -446,7 +446,7 @@ impl Sub<Uint512> for Uint512 {
446446
type Output = Self;
447447

448448
fn sub(self, rhs: Self) -> Self {
449-
self.panicking_sub(rhs)
449+
self.strict_sub(rhs)
450450
}
451451
}
452452
forward_ref_binop!(impl Sub, sub for Uint512, Uint512);
@@ -1357,18 +1357,34 @@ mod tests {
13571357
}
13581358

13591359
#[test]
1360-
fn uint512_panicking_sub_works() {
1360+
fn uint512_strict_add_works() {
1361+
let a = Uint512::from(5u32);
1362+
let b = Uint512::from(3u32);
1363+
assert_eq!(a.strict_add(b), Uint512::from(8u32));
1364+
assert_eq!(b.strict_add(a), Uint512::from(8u32));
1365+
}
1366+
1367+
#[test]
1368+
#[should_panic(expected = "attempt to add with overflow")]
1369+
fn uint512_strict_add_panics_on_overflow() {
1370+
let a = Uint512::MAX;
1371+
let b = Uint512::one();
1372+
let _ = a.strict_add(b);
1373+
}
1374+
1375+
#[test]
1376+
fn uint512_strict_sub_works() {
13611377
let a = Uint512::from(5u32);
13621378
let b = Uint512::from(3u32);
1363-
assert_eq!(a.panicking_sub(b), Uint512::from(2u32));
1379+
assert_eq!(a.strict_sub(b), Uint512::from(2u32));
13641380
}
13651381

13661382
#[test]
13671383
#[should_panic(expected = "attempt to subtract with overflow")]
1368-
fn uint512_panicking_sub_panics_on_overflow() {
1384+
fn uint512_strict_sub_panics_on_overflow() {
13691385
let a = Uint512::zero();
13701386
let b = Uint512::one();
1371-
let _diff = a.panicking_sub(b);
1387+
let _ = a.strict_sub(b);
13721388
}
13731389

13741390
#[test]

packages/std/src/math/uint64.rs

+29-13
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,22 @@ impl Uint64 {
252252
Self(self.0.saturating_pow(exp))
253253
}
254254

255-
/// This is the same as [`Uint64::add`] but const.
255+
/// Strict integer addition. Computes `self + rhs`, panicking if overflow occurred.
256256
///
257-
/// Panics on overflow.
257+
/// This is the same as [`Uint64::add`] but const.
258258
#[must_use = "this returns the result of the operation, without modifying the original"]
259-
pub const fn panicking_add(self, other: Self) -> Self {
260-
match self.0.checked_add(other.u64()) {
259+
pub const fn strict_add(self, rhs: Self) -> Self {
260+
match self.0.checked_add(rhs.u64()) {
261261
None => panic!("attempt to add with overflow"),
262262
Some(sum) => Self(sum),
263263
}
264264
}
265265

266-
/// This is the same as [`Uint64::sub`] but const.
266+
/// Strict integer subtraction. Computes `self - rhs`, panicking if overflow occurred.
267267
///
268-
/// Panics on overflow.
268+
/// This is the same as [`Uint64::sub`] but const.
269269
#[must_use = "this returns the result of the operation, without modifying the original"]
270-
pub const fn panicking_sub(self, other: Self) -> Self {
270+
pub const fn strict_sub(self, other: Self) -> Self {
271271
match self.0.checked_sub(other.u64()) {
272272
None => panic!("attempt to subtract with overflow"),
273273
Some(diff) => Self(diff),
@@ -348,7 +348,7 @@ impl Add<Uint64> for Uint64 {
348348
type Output = Self;
349349

350350
fn add(self, rhs: Self) -> Self {
351-
self.panicking_add(rhs)
351+
self.strict_add(rhs)
352352
}
353353
}
354354
forward_ref_binop!(impl Add, add for Uint64, Uint64);
@@ -357,7 +357,7 @@ impl Sub<Uint64> for Uint64 {
357357
type Output = Self;
358358

359359
fn sub(self, rhs: Self) -> Self {
360-
self.panicking_sub(rhs)
360+
self.strict_sub(rhs)
361361
}
362362
}
363363
forward_ref_binop!(impl Sub, sub for Uint64, Uint64);
@@ -1077,18 +1077,34 @@ mod tests {
10771077
}
10781078

10791079
#[test]
1080-
fn uint64_panicking_sub_works() {
1080+
fn uint64_strict_add_works() {
1081+
let a = Uint64::new(5);
1082+
let b = Uint64::new(3);
1083+
assert_eq!(a.strict_add(b), Uint64::new(8));
1084+
assert_eq!(b.strict_add(a), Uint64::new(8));
1085+
}
1086+
1087+
#[test]
1088+
#[should_panic(expected = "attempt to add with overflow")]
1089+
fn uint64_strict_add_panics_on_overflow() {
1090+
let a = Uint64::MAX;
1091+
let b = Uint64::one();
1092+
let _ = a.strict_add(b);
1093+
}
1094+
1095+
#[test]
1096+
fn uint64_strict_sub_works() {
10811097
let a = Uint64::new(5);
10821098
let b = Uint64::new(3);
1083-
assert_eq!(a.panicking_sub(b), Uint64::new(2));
1099+
assert_eq!(a.strict_sub(b), Uint64::new(2));
10841100
}
10851101

10861102
#[test]
10871103
#[should_panic(expected = "attempt to subtract with overflow")]
1088-
fn uint64_panicking_sub_panics_on_overflow() {
1104+
fn uint64_strict_sub_panics_on_overflow() {
10891105
let a = Uint64::zero();
10901106
let b = Uint64::one();
1091-
let _diff = a.panicking_sub(b);
1107+
let _ = a.strict_sub(b);
10921108
}
10931109

10941110
#[test]

packages/std/src/timestamp.rs

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

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

142142
/// Returns nanoseconds since epoch

0 commit comments

Comments
 (0)