From 281b410b19888acc78faf8cff439ded1ec636bcb Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Apr 2024 10:47:37 +0200 Subject: [PATCH 1/7] Add panicking_sub to Uint64/Uint128/Uint256/Uint512 (cherry picked from commit 547efeda0726c608f5cd37f5fb62f416c3c63e92) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 7 +++++++ packages/std/src/math/uint128.rs | 32 +++++++++++++++++++++++++++----- packages/std/src/math/uint256.rs | 32 +++++++++++++++++++++++++++----- packages/std/src/math/uint512.rs | 28 +++++++++++++++++++++++++++- packages/std/src/math/uint64.rs | 32 +++++++++++++++++++++++++++----- 5 files changed, 115 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 006c8d8304..079ff4adb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ and this project adheres to - cosmwasm-std: Implement `&T + T` and `&T op &T` for `Uint64`, `Uint128`, `Uint256` and `Uint512`; improve panic message for `Uint64::add` and `Uint512::add` ([#2092]) +<<<<<<< HEAD +======= +- cosmwasm-std: Add `{CosmosMsg,SubMsg,Response}::change_custom` to change the + custom message type ([#2099]) +- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the + `Sub` implementation but `const`. +>>>>>>> 547efeda0 (Add panicking_sub to Uint64/Uint128/Uint256/Uint512) [#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092 diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 9ee5c9841c..1c7f9a3304 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -263,6 +263,17 @@ impl Uint128 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint128::sub`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_sub(self, other: Self) -> Self { + match self.0.checked_sub(other.u128()) { + None => panic!("attempt to subtract with overflow"), + Some(diff) => Self(diff), + } + } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs_diff(self, other: Self) -> Self { Self(if self.0 < other.0 { @@ -385,11 +396,7 @@ impl Sub for Uint128 { type Output = Self; fn sub(self, rhs: Self) -> Self { - Uint128( - self.u128() - .checked_sub(rhs.u128()) - .expect("attempt to subtract with overflow"), - ) + self.panicking_sub(rhs) } } forward_ref_binop!(impl Sub, sub for Uint128, Uint128); @@ -1176,6 +1183,21 @@ mod tests { assert_eq!(a, Uint128::from(1u32)); } + #[test] + fn uint128_panicking_sub_works() { + let a = Uint128::new(5); + let b = Uint128::new(3); + assert_eq!(a.panicking_sub(b), Uint128::new(2)); + } + + #[test] + #[should_panic(expected = "attempt to subtract with overflow")] + fn uint128_panicking_sub_panics_on_overflow() { + let a = Uint128::ZERO; + let b = Uint128::ONE; + let _diff = a.panicking_sub(b); + } + #[test] fn uint128_abs_diff_works() { let a = Uint128::from(42u32); diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 6b4814feea..3fc41e022b 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -335,6 +335,17 @@ impl Uint256 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint256::sub`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_sub(self, other: Self) -> Self { + match self.0.checked_sub(other.0) { + None => panic!("attempt to subtract with overflow"), + Some(diff) => Self(diff), + } + } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs_diff(self, other: Self) -> Self { Self(self.0.abs_diff(other.0)) @@ -453,11 +464,7 @@ impl Sub for Uint256 { type Output = Self; fn sub(self, rhs: Self) -> Self { - Self( - self.0 - .checked_sub(rhs.0) - .expect("attempt to subtract with overflow"), - ) + self.panicking_sub(rhs) } } forward_ref_binop!(impl Sub, sub for Uint256, Uint256); @@ -1723,6 +1730,21 @@ mod tests { assert_eq!(a, Uint256::from(1u32)); } + #[test] + fn uint256_panicking_sub_works() { + let a = Uint256::from(5u32); + let b = Uint256::from(3u32); + assert_eq!(a.panicking_sub(b), Uint256::from(2u32)); + } + + #[test] + #[should_panic(expected = "attempt to subtract with overflow")] + fn uint256_panicking_sub_panics_on_overflow() { + let a = Uint256::ZERO; + let b = Uint256::ONE; + let _diff = a.panicking_sub(b); + } + #[test] fn uint256_abs_diff_works() { let a = Uint256::from(42u32); diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 2842b726bd..376c618f1c 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -298,6 +298,17 @@ impl Uint512 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint512::sub`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_sub(self, other: Self) -> Self { + match self.0.checked_sub(other.0) { + None => panic!("attempt to subtract with overflow"), + Some(diff) => Self(diff), + } + } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs_diff(self, other: Self) -> Self { Self(self.0.abs_diff(other.0)) @@ -434,7 +445,7 @@ impl Sub for Uint512 { type Output = Self; fn sub(self, rhs: Self) -> Self { - Uint512(self.0.checked_sub(rhs.0).unwrap()) + self.panicking_sub(rhs) } } forward_ref_binop!(impl Sub, sub for Uint512, Uint512); @@ -1370,6 +1381,21 @@ mod tests { assert_eq!(a, Uint512::from(1u32)); } + #[test] + fn uint512_panicking_sub_works() { + let a = Uint512::from(5u32); + let b = Uint512::from(3u32); + assert_eq!(a.panicking_sub(b), Uint512::from(2u32)); + } + + #[test] + #[should_panic(expected = "attempt to subtract with overflow")] + fn uint512_panicking_sub_panics_on_overflow() { + let a = Uint512::ZERO; + let b = Uint512::ONE; + let _diff = a.panicking_sub(b); + } + #[test] fn uint512_abs_diff_works() { let a = Uint512::from(42u32); diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index 188115b4ac..6cb3070ddf 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -257,6 +257,17 @@ impl Uint64 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint64::sub`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_sub(self, other: Self) -> Self { + match self.0.checked_sub(other.u64()) { + None => panic!("attempt to subtract with overflow"), + Some(diff) => Self(diff), + } + } + #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn abs_diff(self, other: Self) -> Self { Self(if self.0 < other.0 { @@ -358,11 +369,7 @@ impl Sub for Uint64 { type Output = Self; fn sub(self, rhs: Self) -> Self { - Uint64( - self.u64() - .checked_sub(rhs.u64()) - .expect("attempt to subtract with overflow"), - ) + self.panicking_sub(rhs) } } forward_ref_binop!(impl Sub, sub for Uint64, Uint64); @@ -1088,6 +1095,21 @@ mod tests { assert_eq!(a, Uint64::from(1u32)); } + #[test] + fn uint64_panicking_sub_works() { + let a = Uint64::new(5); + let b = Uint64::new(3); + assert_eq!(a.panicking_sub(b), Uint64::new(2)); + } + + #[test] + #[should_panic(expected = "attempt to subtract with overflow")] + fn uint64_panicking_sub_panics_on_overflow() { + let a = Uint64::ZERO; + let b = Uint64::ONE; + let _diff = a.panicking_sub(b); + } + #[test] fn uint64_abs_diff_works() { let a = Uint64::from(42u32); From 9d97e8dc6491ca8e18352270adeea763fd607cd2 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 9 Apr 2024 14:55:16 +0200 Subject: [PATCH 2/7] Improve docs of Timestamp::minus_* and use panicking_sub (cherry picked from commit 242cc1ffcb22021e1a6e4bef5cffd6b646f660f1) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 5 +++++ packages/std/src/timestamp.rs | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 079ff4adb0..aab06120e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,12 @@ and this project adheres to custom message type ([#2099]) - cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the `Sub` implementation but `const`. +<<<<<<< HEAD >>>>>>> 547efeda0 (Add panicking_sub to Uint64/Uint128/Uint256/Uint512) +======= +- cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and + document panicking behaviour. +>>>>>>> 242cc1ffc (Improve docs of Timestamp::minus_* and use panicking_sub) [#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092 diff --git a/packages/std/src/timestamp.rs b/packages/std/src/timestamp.rs index 8421e3b1b6..7511866f60 100644 --- a/packages/std/src/timestamp.rs +++ b/packages/std/src/timestamp.rs @@ -68,33 +68,52 @@ impl Timestamp { Timestamp(nanos) } + /// Subtracts the given amount of days from the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub const fn minus_days(&self, subtrahend: u64) -> Timestamp { self.minus_hours(subtrahend * 24) } + /// Subtracts the given amount of hours from the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub const fn minus_hours(&self, subtrahend: u64) -> Timestamp { self.minus_minutes(subtrahend * 60) } + /// Subtracts the given amount of minutes from the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub const fn minus_minutes(&self, subtrahend: u64) -> Timestamp { self.minus_seconds(subtrahend * 60) } + /// Subtracts the given amount of seconds from the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented. #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn minus_seconds(&self, subtrahend: u64) -> Timestamp { self.minus_nanos(subtrahend * 1_000_000_000) } + /// Subtracts the given amount of nanoseconds from the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented. #[must_use = "this returns the result of the operation, without modifying the original"] pub const fn minus_nanos(&self, subtrahend: u64) -> Timestamp { - let nanos = Uint64::new(self.0.u64() - subtrahend); - Timestamp(nanos) + Timestamp(self.0.panicking_sub(Uint64::new(subtrahend))) } /// Returns nanoseconds since epoch From 0acd67b4a54ad6805bdf0e94f147e627db23ebda Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Apr 2024 10:48:18 +0200 Subject: [PATCH 3/7] Add Uint{64,128,256,512}::panicking_add (cherry picked from commit a74aba0ef21d2f29347449d510897d7bf715f2d5) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 5 +++++ packages/std/src/math/uint128.rs | 17 ++++++++++++----- packages/std/src/math/uint256.rs | 17 ++++++++++++----- packages/std/src/math/uint512.rs | 17 ++++++++++++----- packages/std/src/math/uint64.rs | 17 ++++++++++++----- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aab06120e0..637315b44a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,11 +15,16 @@ and this project adheres to ======= - cosmwasm-std: Add `{CosmosMsg,SubMsg,Response}::change_custom` to change the custom message type ([#2099]) +<<<<<<< HEAD - cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the `Sub` implementation but `const`. <<<<<<< HEAD >>>>>>> 547efeda0 (Add panicking_sub to Uint64/Uint128/Uint256/Uint512) ======= +======= +- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub` + which are like the `Add`/`Sub` implementations but `const`. +>>>>>>> a74aba0ef (Add Uint{64,128,256,512}::panicking_add) - cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and document panicking behaviour. >>>>>>> 242cc1ffc (Improve docs of Timestamp::minus_* and use panicking_sub) diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 1c7f9a3304..49130bbb34 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -263,6 +263,17 @@ impl Uint128 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint128::add`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_add(self, other: Self) -> Self { + match self.0.checked_add(other.u128()) { + None => panic!("attempt to add with overflow"), + Some(sum) => Self(sum), + } + } + /// This is the same as [`Uint128::sub`] but const. /// /// Panics on overflow. @@ -383,11 +394,7 @@ impl Add for Uint128 { type Output = Self; fn add(self, rhs: Self) -> Self { - Self( - self.u128() - .checked_add(rhs.u128()) - .expect("attempt to add with overflow"), - ) + self.panicking_add(rhs) } } forward_ref_binop!(impl Add, add for Uint128, Uint128); diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 3fc41e022b..489a1f28c8 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -335,6 +335,17 @@ impl Uint256 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint256::add`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_add(self, other: Self) -> Self { + match self.0.checked_add(other.0) { + None => panic!("attempt to add with overflow"), + Some(sum) => Self(sum), + } + } + /// This is the same as [`Uint256::sub`] but const. /// /// Panics on overflow. @@ -451,11 +462,7 @@ impl Add for Uint256 { type Output = Self; fn add(self, rhs: Self) -> Self { - Self( - self.0 - .checked_add(rhs.0) - .expect("attempt to add with overflow"), - ) + self.panicking_add(rhs) } } forward_ref_binop!(impl Add, add for Uint256, Uint256); diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 376c618f1c..82e10937f2 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -298,6 +298,17 @@ impl Uint512 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint512::add`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_add(self, other: Self) -> Self { + match self.0.checked_add(other.0) { + None => panic!("attempt to add with overflow"), + Some(sum) => Self(sum), + } + } + /// This is the same as [`Uint512::sub`] but const. /// /// Panics on overflow. @@ -432,11 +443,7 @@ impl Add for Uint512 { type Output = Self; fn add(self, rhs: Self) -> Self { - Self( - self.0 - .checked_add(rhs.0) - .expect("attempt to add with overflow"), - ) + self.panicking_add(rhs) } } forward_ref_binop!(impl Add, add for Uint512, Uint512); diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index 6cb3070ddf..d130873a25 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -257,6 +257,17 @@ impl Uint64 { Self(self.0.saturating_pow(exp)) } + /// This is the same as [`Uint64::add`] but const. + /// + /// Panics on overflow. + #[must_use = "this returns the result of the operation, without modifying the original"] + pub const fn panicking_add(self, other: Self) -> Self { + match self.0.checked_add(other.u64()) { + None => panic!("attempt to add with overflow"), + Some(sum) => Self(sum), + } + } + /// This is the same as [`Uint64::sub`] but const. /// /// Panics on overflow. @@ -356,11 +367,7 @@ impl Add for Uint64 { type Output = Self; fn add(self, rhs: Self) -> Self { - Self( - self.u64() - .checked_add(rhs.u64()) - .expect("attempt to add with overflow"), - ) + self.panicking_add(rhs) } } forward_ref_binop!(impl Add, add for Uint64, Uint64); From cc17fdc7a614022435846c29e31b75ab11b3938b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Apr 2024 09:51:49 +0200 Subject: [PATCH 4/7] Add #[inline] annotations to plus_seconds/minus_seconds (cherry picked from commit bbb788dc3d7d515045e89ae98bcdffecc406b830) --- packages/std/src/timestamp.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/std/src/timestamp.rs b/packages/std/src/timestamp.rs index 7511866f60..9edb47b8de 100644 --- a/packages/std/src/timestamp.rs +++ b/packages/std/src/timestamp.rs @@ -58,11 +58,13 @@ impl Timestamp { } #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] pub const fn plus_seconds(&self, addition: u64) -> Timestamp { self.plus_nanos(addition * 1_000_000_000) } #[must_use = "this returns the result of the operation, without modifying the original"] + // no #[inline] here as this could be shared with all the callers pub const fn plus_nanos(&self, addition: u64) -> Timestamp { let nanos = Uint64::new(self.0.u64() + addition); Timestamp(nanos) @@ -103,6 +105,7 @@ impl Timestamp { /// /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented. #[must_use = "this returns the result of the operation, without modifying the original"] + #[inline] pub const fn minus_seconds(&self, subtrahend: u64) -> Timestamp { self.minus_nanos(subtrahend * 1_000_000_000) } @@ -112,6 +115,7 @@ impl Timestamp { /// /// Panics if the result is not >= 0. I.e. times before epoch cannot be represented. #[must_use = "this returns the result of the operation, without modifying the original"] + // no #[inline] here as this could be shared with all the callers pub const fn minus_nanos(&self, subtrahend: u64) -> Timestamp { Timestamp(self.0.panicking_sub(Uint64::new(subtrahend))) } From 3622c3eadc13c1d52903f357507aff8d11a4b62e Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Apr 2024 09:54:32 +0200 Subject: [PATCH 5/7] Make overflow behaviour explicit for Timestamp (cherry picked from commit b265b33c709d95260939c59e03b5dd9f875fbbd1) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 5 +++++ packages/std/src/timestamp.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 637315b44a..c924c5705a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,10 +24,15 @@ and this project adheres to ======= - cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub` which are like the `Add`/`Sub` implementations but `const`. +<<<<<<< HEAD >>>>>>> a74aba0ef (Add Uint{64,128,256,512}::panicking_add) - cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and document panicking behaviour. >>>>>>> 242cc1ffc (Improve docs of Timestamp::minus_* and use panicking_sub) +======= +- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use + `Uint64::panicking_add`/`::panicking_sub` and document panicking behaviour. +>>>>>>> b265b33c7 (Make overflow behaviour explicit for Timestamp) [#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092 diff --git a/packages/std/src/timestamp.rs b/packages/std/src/timestamp.rs index 9edb47b8de..1e7b110606 100644 --- a/packages/std/src/timestamp.rs +++ b/packages/std/src/timestamp.rs @@ -39,34 +39,54 @@ impl Timestamp { Timestamp(Uint64::new(seconds_since_epoch * 1_000_000_000)) } + /// Adds the given amount of days to the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result exceeds the value range of [`Timestamp`]. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub const fn plus_days(&self, addition: u64) -> Timestamp { self.plus_hours(addition * 24) } + /// Adds the given amount of hours to the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result exceeds the value range of [`Timestamp`]. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub const fn plus_hours(&self, addition: u64) -> Timestamp { self.plus_minutes(addition * 60) } + /// Adds the given amount of minutes to the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result exceeds the value range of [`Timestamp`]. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub const fn plus_minutes(&self, addition: u64) -> Timestamp { self.plus_seconds(addition * 60) } + /// Adds the given amount of seconds to the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result exceeds the value range of [`Timestamp`]. #[must_use = "this returns the result of the operation, without modifying the original"] #[inline] pub const fn plus_seconds(&self, addition: u64) -> Timestamp { self.plus_nanos(addition * 1_000_000_000) } + /// Adds the given amount of nanoseconds to the timestamp and + /// returns the result. The original value remains unchanged. + /// + /// Panics if the result exceeds the value range of [`Timestamp`]. #[must_use = "this returns the result of the operation, without modifying the original"] // no #[inline] here as this could be shared with all the callers pub const fn plus_nanos(&self, addition: u64) -> Timestamp { - let nanos = Uint64::new(self.0.u64() + addition); + let nanos = self.0.panicking_add(Uint64::new(addition)); Timestamp(nanos) } @@ -184,6 +204,13 @@ mod tests { assert_eq!(sum.0.u64(), 123); } + #[test] + #[should_panic(expected = "attempt to add with overflow")] + fn timestamp_plus_nanos_panics_on_overflow() { + let max = Timestamp::from_nanos(u64::MAX); + let _earlier = max.plus_nanos(20); + } + #[test] fn timestamp_minus_seconds() { let earlier = Timestamp::from_seconds(123).minus_seconds(0); From a76a1d78ce8769c76e2c021123bfa383bb9e7325 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Apr 2024 10:51:19 +0200 Subject: [PATCH 6/7] Add PR link to new CHANGELOG entries (cherry picked from commit 198001e03196aa8366026c7729469f7f6d4b5ca4) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c924c5705a..1d63dfe539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to ======= ======= - cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub` +<<<<<<< HEAD which are like the `Add`/`Sub` implementations but `const`. <<<<<<< HEAD >>>>>>> a74aba0ef (Add Uint{64,128,256,512}::panicking_add) @@ -33,8 +34,36 @@ and this project adheres to - cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use `Uint64::panicking_add`/`::panicking_sub` and document panicking behaviour. >>>>>>> b265b33c7 (Make overflow behaviour explicit for Timestamp) +======= + which are like the `Add`/`Sub` implementations but `const`. ([#2098]) +- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use + `Uint64::panicking_add`/`::panicking_sub` and document overflows. ([#2098]) +>>>>>>> 198001e03 (Add PR link to new CHANGELOG entries) [#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092 +<<<<<<< HEAD +======= +[#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098 +[#2099]: https://github.com/CosmWasm/cosmwasm/pull/2099 + +### Changed + +- cosmwasm-std: Enable `add_event` and `add_events` functions to process types + implementing `Into` ([#2044]) +- cosmwasm-vm: Improve performance of the `Cache::analyze` function ([#2051]) +- cosmwasm-derive: Update to `syn` v2 ([#2063]) +- cosmwasm-schema-derive: Update to `syn` v2 ([#2063]) +- cosmwasm-schema-derive: Improve emitted error messages ([#2063]) +- cosmwasm-schema: `#[cw_serde]` now doesn't add `#[serde(deny_unknown_fields)]` + to the expanded code anymore ([#2080]) + +[#2044]: https://github.com/CosmWasm/cosmwasm/pull/2044 +[#2051]: https://github.com/CosmWasm/cosmwasm/pull/2051 +[#2059]: https://github.com/CosmWasm/cosmwasm/pull/2059 +[#2063]: https://github.com/CosmWasm/cosmwasm/pull/2063 +[#2070]: https://github.com/CosmWasm/cosmwasm/pull/2070 +[#2080]: https://github.com/CosmWasm/cosmwasm/pull/2080 +>>>>>>> 198001e03 (Add PR link to new CHANGELOG entries) ## [2.0.1] - 2024-04-03 From cf562b4518ea0c16462cabfefa805cfec76c0ee7 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Apr 2024 12:37:49 +0200 Subject: [PATCH 7/7] Fix CHANGELOG --- CHANGELOG.md | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d63dfe539..1f84622888 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,59 +11,13 @@ and this project adheres to - cosmwasm-std: Implement `&T + T` and `&T op &T` for `Uint64`, `Uint128`, `Uint256` and `Uint512`; improve panic message for `Uint64::add` and `Uint512::add` ([#2092]) -<<<<<<< HEAD -======= -- cosmwasm-std: Add `{CosmosMsg,SubMsg,Response}::change_custom` to change the - custom message type ([#2099]) -<<<<<<< HEAD -- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_sub` which is like the - `Sub` implementation but `const`. -<<<<<<< HEAD ->>>>>>> 547efeda0 (Add panicking_sub to Uint64/Uint128/Uint256/Uint512) -======= -======= - cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub` -<<<<<<< HEAD - which are like the `Add`/`Sub` implementations but `const`. -<<<<<<< HEAD ->>>>>>> a74aba0ef (Add Uint{64,128,256,512}::panicking_add) -- cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and - document panicking behaviour. ->>>>>>> 242cc1ffc (Improve docs of Timestamp::minus_* and use panicking_sub) -======= -- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use - `Uint64::panicking_add`/`::panicking_sub` and document panicking behaviour. ->>>>>>> b265b33c7 (Make overflow behaviour explicit for Timestamp) -======= which are like the `Add`/`Sub` implementations but `const`. ([#2098]) - cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use `Uint64::panicking_add`/`::panicking_sub` and document overflows. ([#2098]) ->>>>>>> 198001e03 (Add PR link to new CHANGELOG entries) [#2092]: https://github.com/CosmWasm/cosmwasm/pull/2092 -<<<<<<< HEAD -======= [#2098]: https://github.com/CosmWasm/cosmwasm/pull/2098 -[#2099]: https://github.com/CosmWasm/cosmwasm/pull/2099 - -### Changed - -- cosmwasm-std: Enable `add_event` and `add_events` functions to process types - implementing `Into` ([#2044]) -- cosmwasm-vm: Improve performance of the `Cache::analyze` function ([#2051]) -- cosmwasm-derive: Update to `syn` v2 ([#2063]) -- cosmwasm-schema-derive: Update to `syn` v2 ([#2063]) -- cosmwasm-schema-derive: Improve emitted error messages ([#2063]) -- cosmwasm-schema: `#[cw_serde]` now doesn't add `#[serde(deny_unknown_fields)]` - to the expanded code anymore ([#2080]) - -[#2044]: https://github.com/CosmWasm/cosmwasm/pull/2044 -[#2051]: https://github.com/CosmWasm/cosmwasm/pull/2051 -[#2059]: https://github.com/CosmWasm/cosmwasm/pull/2059 -[#2063]: https://github.com/CosmWasm/cosmwasm/pull/2063 -[#2070]: https://github.com/CosmWasm/cosmwasm/pull/2070 -[#2080]: https://github.com/CosmWasm/cosmwasm/pull/2080 ->>>>>>> 198001e03 (Add PR link to new CHANGELOG entries) ## [2.0.1] - 2024-04-03