Skip to content

Commit 1f8964f

Browse files
authored
Merge pull request #2097 from CosmWasm/mergify/bp/release/1.4/pr-2092
Implement add for Uint* more consistently (backport #2092)
2 parents da9a979 + 77749ef commit 1f8964f

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
### Changed
1018

1119
- cosmwasm-vm: Read `Region` from Wasm memory as bytes and convert to `Region`

packages/std/src/math/uint128.rs

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

365365
fn add(self, rhs: Self) -> Self {
366-
Uint128(
366+
Self(
367367
self.u128()
368368
.checked_add(rhs.u128())
369369
.expect("attempt to add with overflow"),
370370
)
371371
}
372372
}
373-
374-
impl<'a> Add<&'a Uint128> for Uint128 {
375-
type Output = Self;
376-
377-
fn add(self, rhs: &'a Uint128) -> Self {
378-
self + *rhs
379-
}
380-
}
373+
forward_ref_binop!(impl Add, add for Uint128, Uint128);
381374

382375
impl Sub<Uint128> for Uint128 {
383376
type Output = Self;
@@ -781,10 +774,6 @@ mod tests {
781774
let a = Uint128(12345);
782775
let b = Uint128(23456);
783776

784-
// test + with owned and reference right hand side
785-
assert_eq!(a + b, Uint128(35801));
786-
assert_eq!(a + &b, Uint128(35801));
787-
788777
// test - with owned and reference right hand side
789778
assert_eq!(b - a, Uint128(11111));
790779
assert_eq!(b - &a, Uint128(11111));
@@ -814,11 +803,32 @@ mod tests {
814803
}
815804

816805
#[test]
817-
#[should_panic]
806+
#[allow(clippy::op_ref)]
807+
fn uint128_add_works() {
808+
assert_eq!(
809+
Uint128::from(2u32) + Uint128::from(1u32),
810+
Uint128::from(3u32)
811+
);
812+
assert_eq!(
813+
Uint128::from(2u32) + Uint128::from(0u32),
814+
Uint128::from(2u32)
815+
);
816+
817+
// works for refs
818+
let a = Uint128::from(10u32);
819+
let b = Uint128::from(3u32);
820+
let expected = Uint128::from(13u32);
821+
assert_eq!(a + b, expected);
822+
assert_eq!(a + &b, expected);
823+
assert_eq!(&a + b, expected);
824+
assert_eq!(&a + &b, expected);
825+
}
826+
827+
#[test]
828+
#[should_panic(expected = "attempt to add with overflow")]
818829
fn uint128_add_overflow_panics() {
819-
// almost_max is 2^128 - 10
820-
let almost_max = Uint128(340282366920938463463374607431768211446);
821-
let _ = almost_max + Uint128(12);
830+
let max = Uint128::MAX;
831+
let _ = max + Uint128(12);
822832
}
823833

824834
#[test]

packages/std/src/math/uint256.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,7 @@ impl Add<Uint256> for Uint256 {
435435
)
436436
}
437437
}
438-
439-
impl<'a> Add<&'a Uint256> for Uint256 {
440-
type Output = Self;
441-
442-
fn add(self, rhs: &'a Uint256) -> Self {
443-
self + *rhs
444-
}
445-
}
438+
forward_ref_binop!(impl Add, add for Uint256, Uint256);
446439

447440
impl Sub<Uint256> for Uint256 {
448441
type Output = Self;
@@ -1289,10 +1282,6 @@ mod tests {
12891282
let a = Uint256::from(12345u32);
12901283
let b = Uint256::from(23456u32);
12911284

1292-
// test + with owned and reference right hand side
1293-
assert_eq!(a + b, Uint256::from(35801u32));
1294-
assert_eq!(a + &b, Uint256::from(35801u32));
1295-
12961285
// test - with owned and reference right hand side
12971286
assert_eq!(b - a, Uint256::from(11111u32));
12981287
assert_eq!(b - &a, Uint256::from(11111u32));
@@ -1322,7 +1311,29 @@ mod tests {
13221311
}
13231312

13241313
#[test]
1325-
#[should_panic]
1314+
#[allow(clippy::op_ref)]
1315+
fn uint256_add_works() {
1316+
assert_eq!(
1317+
Uint256::from(2u32) + Uint256::from(1u32),
1318+
Uint256::from(3u32)
1319+
);
1320+
assert_eq!(
1321+
Uint256::from(2u32) + Uint256::from(0u32),
1322+
Uint256::from(2u32)
1323+
);
1324+
1325+
// works for refs
1326+
let a = Uint256::from(10u32);
1327+
let b = Uint256::from(3u32);
1328+
let expected = Uint256::from(13u32);
1329+
assert_eq!(a + b, expected);
1330+
assert_eq!(a + &b, expected);
1331+
assert_eq!(&a + b, expected);
1332+
assert_eq!(&a + &b, expected);
1333+
}
1334+
1335+
#[test]
1336+
#[should_panic(expected = "attempt to add with overflow")]
13261337
fn uint256_add_overflow_panics() {
13271338
let max = Uint256::new([255u8; 32]);
13281339
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
@@ -415,17 +415,14 @@ impl Add<Uint512> for Uint512 {
415415
type Output = Self;
416416

417417
fn add(self, rhs: Self) -> Self {
418-
Uint512(self.0.checked_add(rhs.0).unwrap())
419-
}
420-
}
421-
422-
impl<'a> Add<&'a Uint512> for Uint512 {
423-
type Output = Self;
424-
425-
fn add(self, rhs: &'a Uint512) -> Self {
426-
Uint512(self.0.checked_add(rhs.0).unwrap())
418+
Self(
419+
self.0
420+
.checked_add(rhs.0)
421+
.expect("attempt to add with overflow"),
422+
)
427423
}
428424
}
425+
forward_ref_binop!(impl Add, add for Uint512, Uint512);
429426

430427
impl Sub<Uint512> for Uint512 {
431428
type Output = Self;
@@ -993,14 +990,6 @@ mod tests {
993990
let a = Uint512::from(12345u32);
994991
let b = Uint512::from(23456u32);
995992

996-
// test + with owned and reference right hand side
997-
assert_eq!(a + b, Uint512::from(35801u32));
998-
assert_eq!(a + &b, Uint512::from(35801u32));
999-
1000-
// test - with owned and reference right hand side
1001-
assert_eq!(b - a, Uint512::from(11111u32));
1002-
assert_eq!(b - &a, Uint512::from(11111u32));
1003-
1004993
// test += with owned and reference right hand side
1005994
let mut c = Uint512::from(300000u32);
1006995
c += b;
@@ -1026,9 +1015,31 @@ mod tests {
10261015
}
10271016

10281017
#[test]
1029-
#[should_panic]
1018+
#[allow(clippy::op_ref)]
1019+
fn uint512_add_works() {
1020+
assert_eq!(
1021+
Uint512::from(2u32) + Uint512::from(1u32),
1022+
Uint512::from(3u32)
1023+
);
1024+
assert_eq!(
1025+
Uint512::from(2u32) + Uint512::from(0u32),
1026+
Uint512::from(2u32)
1027+
);
1028+
1029+
// works for refs
1030+
let a = Uint512::from(10u32);
1031+
let b = Uint512::from(3u32);
1032+
let expected = Uint512::from(13u32);
1033+
assert_eq!(a + b, expected);
1034+
assert_eq!(a + &b, expected);
1035+
assert_eq!(&a + b, expected);
1036+
assert_eq!(&a + &b, expected);
1037+
}
1038+
1039+
#[test]
1040+
#[should_panic(expected = "attempt to add with overflow")]
10301041
fn uint512_add_overflow_panics() {
1031-
let max = Uint512::new([255u8; 64]);
1042+
let max = Uint512::MAX;
10321043
let _ = max + Uint512::from(12u32);
10331044
}
10341045

packages/std/src/math/uint64.rs

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

328328
fn add(self, rhs: Self) -> Self {
329-
Uint64(self.u64().checked_add(rhs.u64()).unwrap())
330-
}
331-
}
332-
333-
impl<'a> Add<&'a Uint64> for Uint64 {
334-
type Output = Self;
335-
336-
fn add(self, rhs: &'a Uint64) -> Self {
337-
Uint64(self.u64().checked_add(rhs.u64()).unwrap())
329+
Self(
330+
self.u64()
331+
.checked_add(rhs.u64())
332+
.expect("attempt to add with overflow"),
333+
)
338334
}
339335
}
336+
forward_ref_binop!(impl Add, add for Uint64, Uint64);
340337

341338
impl Sub<Uint64> for Uint64 {
342339
type Output = Self;
@@ -704,10 +701,6 @@ mod tests {
704701
let a = Uint64(12345);
705702
let b = Uint64(23456);
706703

707-
// test + with owned and reference right hand side
708-
assert_eq!(a + b, Uint64(35801));
709-
assert_eq!(a + &b, Uint64(35801));
710-
711704
// test - with owned and reference right hand side
712705
assert_eq!((b.checked_sub(a)).unwrap(), Uint64(11111));
713706

@@ -727,6 +720,29 @@ mod tests {
727720
assert_eq!((operand1, operand2), (a.to_string(), b.to_string()));
728721
}
729722

723+
#[test]
724+
#[allow(clippy::op_ref)]
725+
fn uint64_add_works() {
726+
assert_eq!(Uint64::from(2u32) + Uint64::from(1u32), Uint64::from(3u32));
727+
assert_eq!(Uint64::from(2u32) + Uint64::from(0u32), Uint64::from(2u32));
728+
729+
// works for refs
730+
let a = Uint64::from(10u32);
731+
let b = Uint64::from(3u32);
732+
let expected = Uint64::from(13u32);
733+
assert_eq!(a + b, expected);
734+
assert_eq!(a + &b, expected);
735+
assert_eq!(&a + b, expected);
736+
assert_eq!(&a + &b, expected);
737+
}
738+
739+
#[test]
740+
#[should_panic(expected = "attempt to add with overflow")]
741+
fn uint64_add_overflow_panics() {
742+
let max = Uint64::MAX;
743+
let _ = max + Uint64(12);
744+
}
745+
730746
#[test]
731747
#[allow(clippy::op_ref)]
732748
fn uint64_sub_works() {

0 commit comments

Comments
 (0)