Skip to content

Commit b265b33

Browse files
committed
Make overflow behaviour explicit for Timestamp
1 parent bbb788d commit b265b33

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ and this project adheres to
2525
custom message type ([#2099])
2626
- cosmwasm-std: Add `Uint{64,128,256,512}::panicking_add` and `::panicking_sub`
2727
which are like the `Add`/`Sub` implementations but `const`.
28-
- cosmwasm-std: Let `Timestamp::minus_nanos` use `Uint64::panicking_sub` and
29-
document panicking behaviour.
28+
- cosmwasm-std: Let `Timestamp::plus_nanos`/`::minus_nanos` use
29+
`Uint64::panicking_add`/`::panicking_sub` and document panicking behaviour.
3030

3131
[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
3232
[#2057]: https://github.com/CosmWasm/cosmwasm/pull/2057

packages/std/src/timestamp.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,54 @@ impl Timestamp {
3737
Timestamp(Uint64::new(seconds_since_epoch * 1_000_000_000))
3838
}
3939

40+
/// Adds the given amount of days to the timestamp and
41+
/// returns the result. The original value remains unchanged.
42+
///
43+
/// Panics if the result exceeds the value range of [`Timestamp`].
4044
#[must_use = "this returns the result of the operation, without modifying the original"]
4145
#[inline]
4246
pub const fn plus_days(&self, addition: u64) -> Timestamp {
4347
self.plus_hours(addition * 24)
4448
}
4549

50+
/// Adds the given amount of hours to the timestamp and
51+
/// returns the result. The original value remains unchanged.
52+
///
53+
/// Panics if the result exceeds the value range of [`Timestamp`].
4654
#[must_use = "this returns the result of the operation, without modifying the original"]
4755
#[inline]
4856
pub const fn plus_hours(&self, addition: u64) -> Timestamp {
4957
self.plus_minutes(addition * 60)
5058
}
5159

60+
/// Adds the given amount of minutes to the timestamp and
61+
/// returns the result. The original value remains unchanged.
62+
///
63+
/// Panics if the result exceeds the value range of [`Timestamp`].
5264
#[must_use = "this returns the result of the operation, without modifying the original"]
5365
#[inline]
5466
pub const fn plus_minutes(&self, addition: u64) -> Timestamp {
5567
self.plus_seconds(addition * 60)
5668
}
5769

70+
/// Adds the given amount of seconds to the timestamp and
71+
/// returns the result. The original value remains unchanged.
72+
///
73+
/// Panics if the result exceeds the value range of [`Timestamp`].
5874
#[must_use = "this returns the result of the operation, without modifying the original"]
5975
#[inline]
6076
pub const fn plus_seconds(&self, addition: u64) -> Timestamp {
6177
self.plus_nanos(addition * 1_000_000_000)
6278
}
6379

80+
/// Adds the given amount of nanoseconds to the timestamp and
81+
/// returns the result. The original value remains unchanged.
82+
///
83+
/// Panics if the result exceeds the value range of [`Timestamp`].
6484
#[must_use = "this returns the result of the operation, without modifying the original"]
6585
// no #[inline] here as this could be shared with all the callers
6686
pub const fn plus_nanos(&self, addition: u64) -> Timestamp {
67-
let nanos = Uint64::new(self.0.u64() + addition);
87+
let nanos = self.0.panicking_add(Uint64::new(addition));
6888
Timestamp(nanos)
6989
}
7090

@@ -182,6 +202,13 @@ mod tests {
182202
assert_eq!(sum.0.u64(), 123);
183203
}
184204

205+
#[test]
206+
#[should_panic(expected = "attempt to add with overflow")]
207+
fn timestamp_plus_nanos_panics_on_overflow() {
208+
let max = Timestamp::from_nanos(u64::MAX);
209+
let _earlier = max.plus_nanos(20);
210+
}
211+
185212
#[test]
186213
fn timestamp_minus_seconds() {
187214
let earlier = Timestamp::from_seconds(123).minus_seconds(0);

0 commit comments

Comments
 (0)