Skip to content

Commit 70a9fc2

Browse files
webmaster128mergify[bot]
authored andcommitted
Rename math functions to strict_add/strict_sub (#2107)
(cherry picked from commit 3b59561) # Conflicts: # CHANGELOG.md
1 parent 695970c commit 70a9fc2

File tree

6 files changed

+151
-55
lines changed

6 files changed

+151
-55
lines changed

CHANGELOG.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,45 @@ 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+
<<<<<<< HEAD
1415
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub`
1516
which are like the `Add`/`Sub` implementations but `const`. ([#2098])
17+
=======
18+
- cosmwasm-std: Add `{CosmosMsg,SubMsg,Response}::change_custom` to change the
19+
custom message type ([#2099])
20+
- cosmwasm-std: Add `Uint{64,128,256,512}::strict_add` and `::strict_sub` which
21+
are like the `Add`/`Sub` implementations but `const`. ([#2098], [#2107])
22+
>>>>>>> 3b59561d5 (Rename math functions to strict_add/strict_sub (#2107))
1623
- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use
17-
`Uint64::panicking_add`/`::panicking_sub` and document overflows. ([#2098])
24+
`Uint64::strict_add`/`::strict_sub` and document overflows. ([#2098], [#2107])
1825

1926
[#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092
2027
[#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098
28+
<<<<<<< HEAD
29+
=======
30+
[#2099]: https://github.com/CosmWasm/cosmwasm/pull/2099
31+
[#2107]: https://github.com/CosmWasm/cosmwasm/pull/2107
32+
33+
### Changed
34+
35+
- cosmwasm-std: Enable `add_event` and `add_events` functions to process types
36+
implementing `Into<Event>` ([#2044])
37+
- cosmwasm-vm: Improve performance of the `Cache::analyze` function ([#2051])
38+
- cosmwasm-derive: Update to `syn` v2 ([#2063])
39+
- cosmwasm-schema-derive: Update to `syn` v2 ([#2063])
40+
- cosmwasm-schema-derive: Improve emitted error messages ([#2063])
41+
- cosmwasm-schema: `#[cw_serde]` now doesn't add `#[serde(deny_unknown_fields)]`
42+
to the expanded code anymore ([#2080])
43+
44+
[#2044]: https://github.com/CosmWasm/cosmwasm/pull/2044
45+
[#2051]: https://github.com/CosmWasm/cosmwasm/pull/2051
46+
[#2059]: https://github.com/CosmWasm/cosmwasm/pull/2059
47+
[#2063]: https://github.com/CosmWasm/cosmwasm/pull/2063
48+
[#2070]: https://github.com/CosmWasm/cosmwasm/pull/2070
49+
[#2080]: https://github.com/CosmWasm/cosmwasm/pull/2080
50+
51+
## [2.0.1] - 2024-04-03
52+
>>>>>>> 3b59561d5 (Rename math functions to strict_add/strict_sub (#2107))
2153
2254
### Fixed
2355

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);
@@ -1193,18 +1193,34 @@ mod tests {
11931193
}
11941194

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

12021218
#[test]
12031219
#[should_panic(expected = "attempt to subtract with overflow")]
1204-
fn uint128_panicking_sub_panics_on_overflow() {
1220+
fn uint128_strict_sub_panics_on_overflow() {
12051221
let a = Uint128::ZERO;
12061222
let b = Uint128::ONE;
1207-
let _diff = a.panicking_sub(b);
1223+
let _ = a.strict_sub(b);
12081224
}
12091225

12101226
#[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);
@@ -1744,18 +1744,34 @@ mod tests {
17441744
}
17451745

17461746
#[test]
1747-
fn uint256_panicking_sub_works() {
1747+
fn uint256_strict_add_works() {
1748+
let a = Uint256::from(5u32);
1749+
let b = Uint256::from(3u32);
1750+
assert_eq!(a.strict_add(b), Uint256::from(8u32));
1751+
assert_eq!(b.strict_add(a), Uint256::from(8u32));
1752+
}
1753+
1754+
#[test]
1755+
#[should_panic(expected = "attempt to add with overflow")]
1756+
fn uint256_strict_add_panics_on_overflow() {
1757+
let a = Uint256::MAX;
1758+
let b = Uint256::ONE;
1759+
let _ = a.strict_add(b);
1760+
}
1761+
1762+
#[test]
1763+
fn uint256_strict_sub_works() {
17481764
let a = Uint256::from(5u32);
17491765
let b = Uint256::from(3u32);
1750-
assert_eq!(a.panicking_sub(b), Uint256::from(2u32));
1766+
assert_eq!(a.strict_sub(b), Uint256::from(2u32));
17511767
}
17521768

17531769
#[test]
17541770
#[should_panic(expected = "attempt to subtract with overflow")]
1755-
fn uint256_panicking_sub_panics_on_overflow() {
1771+
fn uint256_strict_sub_panics_on_overflow() {
17561772
let a = Uint256::ZERO;
17571773
let b = Uint256::ONE;
1758-
let _diff = a.panicking_sub(b);
1774+
let _ = a.strict_sub(b);
17591775
}
17601776

17611777
#[test]

packages/std/src/math/uint512.rs

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

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

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

448448
fn add(self, rhs: Self) -> Self {
449-
self.panicking_add(rhs)
449+
self.strict_add(rhs)
450450
}
451451
}
452452
forward_ref_binop!(impl Add, add for Uint512, Uint512);
@@ -455,7 +455,7 @@ impl Sub<Uint512> for Uint512 {
455455
type Output = Self;
456456

457457
fn sub(self, rhs: Self) -> Self {
458-
self.panicking_sub(rhs)
458+
self.strict_sub(rhs)
459459
}
460460
}
461461
forward_ref_binop!(impl Sub, sub for Uint512, Uint512);
@@ -1398,18 +1398,34 @@ mod tests {
13981398
}
13991399

14001400
#[test]
1401-
fn uint512_panicking_sub_works() {
1401+
fn uint512_strict_add_works() {
1402+
let a = Uint512::from(5u32);
1403+
let b = Uint512::from(3u32);
1404+
assert_eq!(a.strict_add(b), Uint512::from(8u32));
1405+
assert_eq!(b.strict_add(a), Uint512::from(8u32));
1406+
}
1407+
1408+
#[test]
1409+
#[should_panic(expected = "attempt to add with overflow")]
1410+
fn uint512_strict_add_panics_on_overflow() {
1411+
let a = Uint512::MAX;
1412+
let b = Uint512::ONE;
1413+
let _ = a.strict_add(b);
1414+
}
1415+
1416+
#[test]
1417+
fn uint512_strict_sub_works() {
14021418
let a = Uint512::from(5u32);
14031419
let b = Uint512::from(3u32);
1404-
assert_eq!(a.panicking_sub(b), Uint512::from(2u32));
1420+
assert_eq!(a.strict_sub(b), Uint512::from(2u32));
14051421
}
14061422

14071423
#[test]
14081424
#[should_panic(expected = "attempt to subtract with overflow")]
1409-
fn uint512_panicking_sub_panics_on_overflow() {
1425+
fn uint512_strict_sub_panics_on_overflow() {
14101426
let a = Uint512::ZERO;
14111427
let b = Uint512::ONE;
1412-
let _diff = a.panicking_sub(b);
1428+
let _ = a.strict_sub(b);
14131429
}
14141430

14151431
#[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);
@@ -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]

0 commit comments

Comments
 (0)