Skip to content

Commit 38a5e6b

Browse files
committed
fix miri bootstrap build
1 parent 5a14200 commit 38a5e6b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/clock.rs

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

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#![feature(derive_coerce_pointee)]
1919
#![feature(arbitrary_self_types)]
2020
#![feature(iter_advance_by)]
21-
#![feature(duration_from_nanos_u128)]
21+
#![cfg_attr(not(bootstrap), feature(duration_from_nanos_u128))]
2222
// Configure clippy and other lints
2323
#![allow(
2424
clippy::collapsible_else_if,

0 commit comments

Comments
 (0)