Skip to content

Commit c5942a3

Browse files
authored
Merge pull request #4568 from RalfJung/duration-from-nanos
make use of Duration::from_nanos_u128
2 parents 1729efb + 6a29477 commit c5942a3

File tree

2 files changed

+2
-8
lines changed

2 files changed

+2
-8
lines changed

src/clock.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ impl Instant {
4646
InstantKind::Virtual { nanoseconds: earlier },
4747
) => {
4848
let duration = nanoseconds.saturating_sub(earlier);
49-
// `Duration` does not provide a nice constructor from a `u128` of nanoseconds,
50-
// so we have to implement this ourselves.
51-
// It is possible for second to overflow because u64::MAX < (u128::MAX / 1e9).
52-
// It will be saturated to u64::MAX seconds if the value after division exceeds u64::MAX.
53-
let seconds = u64::try_from(duration / 1_000_000_000).unwrap_or(u64::MAX);
54-
// It is impossible for nanosecond to overflow because u32::MAX > 1e9.
55-
let nanosecond = u32::try_from(duration.wrapping_rem(1_000_000_000)).unwrap();
56-
Duration::new(seconds, nanosecond)
49+
Duration::from_nanos_u128(duration)
5750
}
5851
_ => panic!("all `Instant` must be of the same kind"),
5952
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(derive_coerce_pointee)]
1919
#![feature(arbitrary_self_types)]
2020
#![feature(iter_advance_by)]
21+
#![feature(duration_from_nanos_u128)]
2122
// Configure clippy and other lints
2223
#![allow(
2324
clippy::collapsible_else_if,

0 commit comments

Comments
 (0)