Skip to content

Commit a4a872d

Browse files
committed
Rename math functions to strict_add/strict_sub
1 parent c7da5e3 commit a4a872d

File tree

6 files changed

+122
-57
lines changed

6 files changed

+122
-57
lines changed

CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ 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_add` and `::panicking_sub`
27-
which are like the `Add`/`Sub` implementations but `const`. ([#2098])
26+
- cosmwasm-std: Add `Uint{64,128,256,512}::strict_add` and `::strict_sub` which
27+
are like the `Add`/`Sub` implementations but `const`. ([#2098], [#2107])
2828
- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use
29-
`Uint64::panicking_add`/`::panicking_sub` and document overflows. ([#2098])
29+
`Uint64::strict_add`/`::strict_sub` and document overflows. ([#2098], [#2107])
3030

3131
[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
3232
[#2057]: https://github.com/CosmWasm/cosmwasm/pull/2057
@@ -36,6 +36,7 @@ and this project adheres to
3636
[#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092
3737
[#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098
3838
[#2099]: https://github.com/CosmWasm/cosmwasm/pull/2099
39+
[#2107]: https://github.com/CosmWasm/cosmwasm/pull/2107
3940

4041
### Changed
4142

packages/core/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/core/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),
@@ -464,7 +464,7 @@ impl Add<Uint256> for Uint256 {
464464
type Output = Self;
465465

466466
fn add(self, rhs: Self) -> Self {
467-
self.panicking_add(rhs)
467+
self.strict_add(rhs)
468468
}
469469
}
470470
forward_ref_binop!(impl Add, add for Uint256, Uint256);
@@ -473,7 +473,7 @@ impl Sub<Uint256> for Uint256 {
473473
type Output = Self;
474474

475475
fn sub(self, rhs: Self) -> Self {
476-
self.panicking_sub(rhs)
476+
self.strict_sub(rhs)
477477
}
478478
}
479479
forward_ref_binop!(impl Sub, sub for Uint256, Uint256);
@@ -1740,18 +1740,34 @@ mod tests {
17401740
}
17411741

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

17491765
#[test]
17501766
#[should_panic(expected = "attempt to subtract with overflow")]
1751-
fn uint256_panicking_sub_panics_on_overflow() {
1767+
fn uint256_strict_sub_panics_on_overflow() {
17521768
let a = Uint256::ZERO;
17531769
let b = Uint256::ONE;
1754-
let _diff = a.panicking_sub(b);
1770+
let _ = a.strict_sub(b);
17551771
}
17561772

17571773
#[test]

packages/core/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/core/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);
@@ -1105,18 +1105,34 @@ mod tests {
11051105
}
11061106

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

11141130
#[test]
11151131
#[should_panic(expected = "attempt to subtract with overflow")]
1116-
fn uint64_panicking_sub_panics_on_overflow() {
1132+
fn uint64_strict_sub_panics_on_overflow() {
11171133
let a = Uint64::ZERO;
11181134
let b = Uint64::ONE;
1119-
let _diff = a.panicking_sub(b);
1135+
let _ = a.strict_sub(b);
11201136
}
11211137

11221138
#[test]

packages/std/src/timestamp.rs

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

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

141141
/// Returns nanoseconds since epoch

0 commit comments

Comments
 (0)