Skip to content

Commit 5b5a32d

Browse files
authored
Merge pull request #2095 from CosmWasm/mergify/bp/release/2.0/pr-2092
Implement add for Uint* more consistently (backport #2092)
2 parents 624963c + 608429b commit 5b5a32d

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
## [2.0.1] - 2024-04-03
1018

1119
### Fixed

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));
@@ -842,11 +831,32 @@ mod tests {
842831
}
843832

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

852862
#[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;
@@ -1324,10 +1317,6 @@ mod tests {
13241317
let a = Uint256::from(12345u32);
13251318
let b = Uint256::from(23456u32);
13261319

1327-
// test + with owned and reference right hand side
1328-
assert_eq!(a + b, Uint256::from(35801u32));
1329-
assert_eq!(a + &b, Uint256::from(35801u32));
1330-
13311320
// test - with owned and reference right hand side
13321321
assert_eq!(b - a, Uint256::from(11111u32));
13331322
assert_eq!(b - &a, Uint256::from(11111u32));
@@ -1355,7 +1344,29 @@ mod tests {
13551344
}
13561345

13571346
#[test]
1358-
#[should_panic]
1347+
#[allow(clippy::op_ref)]
1348+
fn uint256_add_works() {
1349+
assert_eq!(
1350+
Uint256::from(2u32) + Uint256::from(1u32),
1351+
Uint256::from(3u32)
1352+
);
1353+
assert_eq!(
1354+
Uint256::from(2u32) + Uint256::from(0u32),
1355+
Uint256::from(2u32)
1356+
);
1357+
1358+
// works for refs
1359+
let a = Uint256::from(10u32);
1360+
let b = Uint256::from(3u32);
1361+
let expected = Uint256::from(13u32);
1362+
assert_eq!(a + b, expected);
1363+
assert_eq!(a + &b, expected);
1364+
assert_eq!(&a + b, expected);
1365+
assert_eq!(&a + &b, expected);
1366+
}
1367+
1368+
#[test]
1369+
#[should_panic(expected = "attempt to add with overflow")]
13591370
fn uint256_add_overflow_panics() {
13601371
let max = Uint256::new([255u8; 32]);
13611372
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
@@ -421,17 +421,14 @@ impl Add<Uint512> for Uint512 {
421421
type Output = Self;
422422

423423
fn add(self, rhs: Self) -> Self {
424-
Uint512(self.0.checked_add(rhs.0).unwrap())
425-
}
426-
}
427-
428-
impl<'a> Add<&'a Uint512> for Uint512 {
429-
type Output = Self;
430-
431-
fn add(self, rhs: &'a Uint512) -> Self {
432-
Uint512(self.0.checked_add(rhs.0).unwrap())
424+
Self(
425+
self.0
426+
.checked_add(rhs.0)
427+
.expect("attempt to add with overflow"),
428+
)
433429
}
434430
}
431+
forward_ref_binop!(impl Add, add for Uint512, Uint512);
435432

436433
impl Sub<Uint512> for Uint512 {
437434
type Output = Self;
@@ -1027,14 +1024,6 @@ mod tests {
10271024
let a = Uint512::from(12345u32);
10281025
let b = Uint512::from(23456u32);
10291026

1030-
// test + with owned and reference right hand side
1031-
assert_eq!(a + b, Uint512::from(35801u32));
1032-
assert_eq!(a + &b, Uint512::from(35801u32));
1033-
1034-
// test - with owned and reference right hand side
1035-
assert_eq!(b - a, Uint512::from(11111u32));
1036-
assert_eq!(b - &a, Uint512::from(11111u32));
1037-
10381027
// test += with owned and reference right hand side
10391028
let mut c = Uint512::from(300000u32);
10401029
c += b;
@@ -1058,9 +1047,31 @@ mod tests {
10581047
}
10591048

10601049
#[test]
1061-
#[should_panic]
1050+
#[allow(clippy::op_ref)]
1051+
fn uint512_add_works() {
1052+
assert_eq!(
1053+
Uint512::from(2u32) + Uint512::from(1u32),
1054+
Uint512::from(3u32)
1055+
);
1056+
assert_eq!(
1057+
Uint512::from(2u32) + Uint512::from(0u32),
1058+
Uint512::from(2u32)
1059+
);
1060+
1061+
// works for refs
1062+
let a = Uint512::from(10u32);
1063+
let b = Uint512::from(3u32);
1064+
let expected = Uint512::from(13u32);
1065+
assert_eq!(a + b, expected);
1066+
assert_eq!(a + &b, expected);
1067+
assert_eq!(&a + b, expected);
1068+
assert_eq!(&a + &b, expected);
1069+
}
1070+
1071+
#[test]
1072+
#[should_panic(expected = "attempt to add with overflow")]
10621073
fn uint512_add_overflow_panics() {
1063-
let max = Uint512::new([255u8; 64]);
1074+
let max = Uint512::MAX;
10641075
let _ = max + Uint512::from(12u32);
10651076
}
10661077

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

@@ -753,6 +746,29 @@ mod tests {
753746
assert_eq!(operation, OverflowOperation::Sub);
754747
}
755748

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

0 commit comments

Comments
 (0)