unconditionally use Duration::from_nanos_u128
This commit is contained in:
parent
8c678cc8e0
commit
3048ef2c4b
1 changed files with 1 additions and 15 deletions
|
|
@ -46,21 +46,7 @@ impl Instant {
|
|||
InstantKind::Virtual { nanoseconds: earlier },
|
||||
) => {
|
||||
let duration = nanoseconds.saturating_sub(earlier);
|
||||
cfg_select! {
|
||||
bootstrap => {
|
||||
// `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)
|
||||
}
|
||||
}
|
||||
Duration::from_nanos_u128(duration)
|
||||
}
|
||||
_ => panic!("all `Instant` must be of the same kind"),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue