Skip to content

Commit 8260c5b

Browse files
committed
Use saturating multiplication for durations
1 parent aaabcb9 commit 8260c5b

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ panic = "abort"
4545
opt-level = 3
4646
codegen-units = 1
4747
incremental = false
48+
49+
[profile.dev]
50+
overflow-checks=true

src/helpers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#[inline]
22
pub fn _sec_to_u64(sec: u64) -> u64 {
3-
sec << 32
3+
sec.saturating_mul(1 << 32)
44
}
55

66
#[inline]
77
pub fn _millis_to_u64(millis: u64) -> u64 {
88
let secs = millis / 1_000;
9-
(secs << 32) | ((millis - secs * 1_000) << 22)
9+
secs.saturating_mul(1 << 32) | ((millis - secs * 1_000) << 22)
1010
}
1111

1212
#[inline]
@@ -17,10 +17,10 @@ pub fn _nsecs_to_u64(nsecs: u64) -> u64 {
1717

1818
#[inline]
1919
pub fn _timespec_to_u64(tp_sec: u64, tp_nsec: u32) -> u64 {
20-
(tp_sec << 32) | ((tp_nsec as u64 * 9_223_372_037) >> 31)
20+
tp_sec.saturating_mul(1 << 32) | ((tp_nsec as u64 * 9_223_372_037) >> 31)
2121
}
2222

2323
#[inline]
2424
pub fn _timeval_to_u64(tv_sec: u64, tv_usec: u32) -> u64 {
25-
(tv_sec << 32) | ((tv_usec as u64 * 9_223_372_036_855) >> 31)
25+
tv_sec.saturating_mul(1 << 32) | ((tv_usec as u64 * 9_223_372_036_855) >> 31)
2626
}

src/tests.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn tests() {
3232
}
3333

3434
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
35+
#[test]
3536
fn tests_updater() {
3637
let updater = Updater::new(250)
3738
.start()
@@ -45,6 +46,11 @@ fn tests_updater() {
4546
let ts = Instant::recent();
4647
let clock_recent = Clock::recent_since_epoch();
4748
sleep(time::Duration::new(1, 0));
48-
assert_eq!(Instant::recent(), ts);
4949
assert_eq!(Clock::recent_since_epoch(), clock_recent);
5050
}
51+
52+
#[test]
53+
fn tests_duration() {
54+
let duration = Duration::from_days(1000);
55+
assert_eq!(duration.as_days(), 1000);
56+
}

0 commit comments

Comments
 (0)