Closed
Description
I tried this code on M1 macOS:
use std::time::{Duration, Instant};
fn main() {
let t0 = Instant::now();
let t1 = t0 + Duration::from_nanos(50);
let d = t1 - t0;
dbg!(t0, t1, d);
assert_eq!(t0 + d, t1);
}
It's expected to succeed.
Instead, this happened:
[src/main.rs:6] t0 = Instant {
t: 147092181000,
}
[src/main.rs:6] t1 = Instant {
t: 147092181001,
}
[src/main.rs:6] d = 41ns
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `Instant { t: 147092181000 }`,
right: `Instant { t: 147092181001 }`', src/main.rs:7:5
It seems that there is something wrong with the Duration -> Instant
casting in Rust std:
rust/library/std/src/sys/unix/time.rs
Lines 227 to 232 in 1d6f242
This problem does not exist on x86_64-apple-darwin, because info.denom = info.numer = 1
.
Meta
rustc --version --verbose
:
rustc 1.56.1 (59eed8a2a 2021-11-01)
binary: rustc
commit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35
commit-date: 2021-11-01
host: aarch64-apple-darwin
release: 1.56.1
LLVM version: 13.0.0