about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2025-09-05 07:40:34 +0000
committerGitHub <noreply@github.com>2025-09-05 07:40:34 +0000
commit3a16bbc1de1b1f7dc9bb157c4065266727256364 (patch)
tree15fe19ce74c6565c50d9a3ee085c1d34285be78b
parent37f9c7deeed720f21a71d3cc9b6126c37606b23f (diff)
parent5cadd4541c96edf0caaf1847b3c13b1ced4d5121 (diff)
downloadrust-3a16bbc1de1b1f7dc9bb157c4065266727256364.tar.gz
rust-3a16bbc1de1b1f7dc9bb157c4065266727256364.zip
Merge pull request #4568 from RalfJung/duration-from-nanos
make use of Duration::from_nanos_u128
-rw-r--r--src/tools/miri/src/clock.rs9
-rw-r--r--src/tools/miri/src/lib.rs1
2 files changed, 2 insertions, 8 deletions
diff --git a/src/tools/miri/src/clock.rs b/src/tools/miri/src/clock.rs
index 34465e9cac6..47608f873a4 100644
--- a/src/tools/miri/src/clock.rs
+++ b/src/tools/miri/src/clock.rs
@@ -46,14 +46,7 @@ impl Instant {
                 InstantKind::Virtual { nanoseconds: earlier },
             ) => {
                 let duration = nanoseconds.saturating_sub(earlier);
-                // `Duration` does not provide a nice constructor from a `u128` of nanoseconds,
-                // so we have to implement this ourselves.
-                // It is possible for second to overflow because u64::MAX < (u128::MAX / 1e9).
-                // It will be saturated to u64::MAX seconds if the value after division exceeds u64::MAX.
-                let seconds = u64::try_from(duration / 1_000_000_000).unwrap_or(u64::MAX);
-                // It is impossible for nanosecond to overflow because u32::MAX > 1e9.
-                let nanosecond = u32::try_from(duration.wrapping_rem(1_000_000_000)).unwrap();
-                Duration::new(seconds, nanosecond)
+                Duration::from_nanos_u128(duration)
             }
             _ => panic!("all `Instant` must be of the same kind"),
         }
diff --git a/src/tools/miri/src/lib.rs b/src/tools/miri/src/lib.rs
index 7013b08e344..dbc2a99593d 100644
--- a/src/tools/miri/src/lib.rs
+++ b/src/tools/miri/src/lib.rs
@@ -18,6 +18,7 @@
 #![feature(derive_coerce_pointee)]
 #![feature(arbitrary_self_types)]
 #![feature(iter_advance_by)]
+#![feature(duration_from_nanos_u128)]
 // Configure clippy and other lints
 #![allow(
     clippy::collapsible_else_if,