Skip to content

Commit 98d37d7

Browse files
authored
Merge pull request #2096 from CosmWasm/mergify/bp/release/1.5/pr-2092
Implement add for Uint* more consistently (backport #2092)
2 parents 5215d3b + a39a171 commit 98d37d7

File tree

5 files changed

+118
-62
lines changed

5 files changed

+118
-62
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- cosmwasm-std: Implement `&T + T` and `&T op &T` for `Uint64`, `Uint128`,
12+
`Uint256` and `Uint512`; improve panic message for `Uint64::add` and
13+
`Uint512::add` ([#2092])
14+
15+
[#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092
16+
917
### Fixed
1018

1119
- cosmwasm-std: Correctly deallocate vectors that were turned into a `Region`

packages/std/src/math/uint128.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,14 @@ impl Add<Uint128> for Uint128 {
372372
type Output = Self;
373373

374374
fn add(self, rhs: Self) -> Self {
375-
Uint128(
375+
Self(
376376
self.u128()
377377
.checked_add(rhs.u128())
378378
.expect("attempt to add with overflow"),
379379
)
380380
}
381381
}
382-
383-
impl<'a> Add<&'a Uint128> for Uint128 {
384-
type Output = Self;
385-
386-
fn add(self, rhs: &'a Uint128) -> Self {
387-
self + *rhs
388-
}
389-
}
382+
forward_ref_binop!(impl Add, add for Uint128, Uint128);
390383

391384
impl Sub<Uint128> for Uint128 {
392385
type Output = Self;
@@ -811,10 +804,6 @@ mod tests {
811804
let a = Uint128(12345);
812805
let b = Uint128(23456);
813806

814-
// test + with owned and reference right hand side
815-
assert_eq!(a + b, Uint128(35801));
816-
assert_eq!(a + &b, Uint128(35801));
817-
818807
// test - with owned and reference right hand side
819808
assert_eq!(b - a, Uint128(11111));
820809
assert_eq!(b - &a, Uint128(11111));
@@ -844,11 +833,32 @@ mod tests {
844833
}
845834

846835
#[test]
847-
#[should_panic]
836+
#[allow(clippy::op_ref)]
837+
fn uint128_add_works() {
838+
assert_eq!(
839+
Uint128::from(2u32) + Uint128::from(1u32),
840+
Uint128::from(3u32)
841+
);
842+
assert_eq!(
843+
Uint128::from(2u32) + Uint128::from(0u32),
844+
Uint128::from(2u32)
845+
);
846+
847+
// works for refs
848+
let a = Uint128::from(10u32);
849+
let b = Uint128::from(3u32);
850+
let expected = Uint128::from(13u32);
851+
assert_eq!(a + b, expected);
852+
assert_eq!(a + &b, expected);
853+
assert_eq!(&a + b, expected);
854+
assert_eq!(&a + &b, expected);
855+
}
856+
857+
#[test]
858+
#[should_panic(expected = "attempt to add with overflow")]
848859
fn uint128_add_overflow_panics() {
849-
// almost_max is 2^128 - 10
850-
let almost_max = Uint128(340282366920938463463374607431768211446);
851-
let _ = almost_max + Uint128(12);
860+
let max = Uint128::MAX;
861+
let _ = max + Uint128(12);
852862
}
853863

854864
#[test]

packages/std/src/math/uint256.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,7 @@ impl Add<Uint256> for Uint256 {
447447
)
448448
}
449449
}
450-
451-
impl<'a> Add<&'a Uint256> for Uint256 {
452-
type Output = Self;
453-
454-
fn add(self, rhs: &'a Uint256) -> Self {
455-
self + *rhs
456-
}
457-
}
450+
forward_ref_binop!(impl Add, add for Uint256, Uint256);
458451

459452
impl Sub<Uint256> for Uint256 {
460453
type Output = Self;
@@ -1328,10 +1321,6 @@ mod tests {
13281321
let a = Uint256::from(12345u32);
13291322
let b = Uint256::from(23456u32);
13301323

1331-
// test + with owned and reference right hand side
1332-
assert_eq!(a + b, Uint256::from(35801u32));
1333-
assert_eq!(a + &b, Uint256::from(35801u32));
1334-
13351324
// test - with owned and reference right hand side
13361325
assert_eq!(b - a, Uint256::from(11111u32));
13371326
assert_eq!(b - &a, Uint256::from(11111u32));
@@ -1361,7 +1350,29 @@ mod tests {
13611350
}
13621351

13631352
#[test]
1364-
#[should_panic]
1353+
#[allow(clippy::op_ref)]
1354+
fn uint256_add_works() {
1355+
assert_eq!(
1356+
Uint256::from(2u32) + Uint256::from(1u32),
1357+
Uint256::from(3u32)
1358+
);
1359+
assert_eq!(
1360+
Uint256::from(2u32) + Uint256::from(0u32),
1361+
Uint256::from(2u32)
1362+
);
1363+
1364+
// works for refs
1365+
let a = Uint256::from(10u32);
1366+
let b = Uint256::from(3u32);
1367+
let expected = Uint256::from(13u32);
1368+
assert_eq!(a + b, expected);
1369+
assert_eq!(a + &b, expected);
1370+
assert_eq!(&a + b, expected);
1371+
assert_eq!(&a + &b, expected);
1372+
}
1373+
1374+
#[test]
1375+
#[should_panic(expected = "attempt to add with overflow")]
13651376
fn uint256_add_overflow_panics() {
13661377
let max = Uint256::new([255u8; 32]);
13671378
let _ = max + Uint256::from(12u32);

packages/std/src/math/uint512.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,14 @@ impl Add<Uint512> for Uint512 {
424424
type Output = Self;
425425

426426
fn add(self, rhs: Self) -> Self {
427-
Uint512(self.0.checked_add(rhs.0).unwrap())
428-
}
429-
}
430-
431-
impl<'a> Add<&'a Uint512> for Uint512 {
432-
type Output = Self;
433-
434-
fn add(self, rhs: &'a Uint512) -> Self {
435-
Uint512(self.0.checked_add(rhs.0).unwrap())
427+
Self(
428+
self.0
429+
.checked_add(rhs.0)
430+
.expect("attempt to add with overflow"),
431+
)
436432
}
437433
}
434+
forward_ref_binop!(impl Add, add for Uint512, Uint512);
438435

439436
impl Sub<Uint512> for Uint512 {
440437
type Output = Self;
@@ -1034,14 +1031,6 @@ mod tests {
10341031
let a = Uint512::from(12345u32);
10351032
let b = Uint512::from(23456u32);
10361033

1037-
// test + with owned and reference right hand side
1038-
assert_eq!(a + b, Uint512::from(35801u32));
1039-
assert_eq!(a + &b, Uint512::from(35801u32));
1040-
1041-
// test - with owned and reference right hand side
1042-
assert_eq!(b - a, Uint512::from(11111u32));
1043-
assert_eq!(b - &a, Uint512::from(11111u32));
1044-
10451034
// test += with owned and reference right hand side
10461035
let mut c = Uint512::from(300000u32);
10471036
c += b;
@@ -1067,9 +1056,31 @@ mod tests {
10671056
}
10681057

10691058
#[test]
1070-
#[should_panic]
1059+
#[allow(clippy::op_ref)]
1060+
fn uint512_add_works() {
1061+
assert_eq!(
1062+
Uint512::from(2u32) + Uint512::from(1u32),
1063+
Uint512::from(3u32)
1064+
);
1065+
assert_eq!(
1066+
Uint512::from(2u32) + Uint512::from(0u32),
1067+
Uint512::from(2u32)
1068+
);
1069+
1070+
// works for refs
1071+
let a = Uint512::from(10u32);
1072+
let b = Uint512::from(3u32);
1073+
let expected = Uint512::from(13u32);
1074+
assert_eq!(a + b, expected);
1075+
assert_eq!(a + &b, expected);
1076+
assert_eq!(&a + b, expected);
1077+
assert_eq!(&a + &b, expected);
1078+
}
1079+
1080+
#[test]
1081+
#[should_panic(expected = "attempt to add with overflow")]
10711082
fn uint512_add_overflow_panics() {
1072-
let max = Uint512::new([255u8; 64]);
1083+
let max = Uint512::MAX;
10731084
let _ = max + Uint512::from(12u32);
10741085
}
10751086

packages/std/src/math/uint64.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,14 @@ impl Add<Uint64> for Uint64 {
345345
type Output = Self;
346346

347347
fn add(self, rhs: Self) -> Self {
348-
Uint64(self.u64().checked_add(rhs.u64()).unwrap())
349-
}
350-
}
351-
352-
impl<'a> Add<&'a Uint64> for Uint64 {
353-
type Output = Self;
354-
355-
fn add(self, rhs: &'a Uint64) -> Self {
356-
Uint64(self.u64().checked_add(rhs.u64()).unwrap())
348+
Self(
349+
self.u64()
350+
.checked_add(rhs.u64())
351+
.expect("attempt to add with overflow"),
352+
)
357353
}
358354
}
355+
forward_ref_binop!(impl Add, add for Uint64, Uint64);
359356

360357
impl Sub<Uint64> for Uint64 {
361358
type Output = Self;
@@ -732,10 +729,6 @@ mod tests {
732729
let a = Uint64(12345);
733730
let b = Uint64(23456);
734731

735-
// test + with owned and reference right hand side
736-
assert_eq!(a + b, Uint64(35801));
737-
assert_eq!(a + &b, Uint64(35801));
738-
739732
// test - with owned and reference right hand side
740733
assert_eq!((b.checked_sub(a)).unwrap(), Uint64(11111));
741734

@@ -755,6 +748,29 @@ mod tests {
755748
assert_eq!((operand1, operand2), (a.to_string(), b.to_string()));
756749
}
757750

751+
#[test]
752+
#[allow(clippy::op_ref)]
753+
fn uint64_add_works() {
754+
assert_eq!(Uint64::from(2u32) + Uint64::from(1u32), Uint64::from(3u32));
755+
assert_eq!(Uint64::from(2u32) + Uint64::from(0u32), Uint64::from(2u32));
756+
757+
// works for refs
758+
let a = Uint64::from(10u32);
759+
let b = Uint64::from(3u32);
760+
let expected = Uint64::from(13u32);
761+
assert_eq!(a + b, expected);
762+
assert_eq!(a + &b, expected);
763+
assert_eq!(&a + b, expected);
764+
assert_eq!(&a + &b, expected);
765+
}
766+
767+
#[test]
768+
#[should_panic(expected = "attempt to add with overflow")]
769+
fn uint64_add_overflow_panics() {
770+
let max = Uint64::MAX;
771+
let _ = max + Uint64(12);
772+
}
773+
758774
#[test]
759775
#[allow(clippy::op_ref)]
760776
fn uint64_sub_works() {

0 commit comments

Comments
 (0)