std.time
Instant
Section titled “Instant”distinct type Instant = IntNanoseconds since the clock’s own start. Only the difference of two instants means something.
Duration
Section titled “Duration”distinct type Duration = IntNanoseconds.
SystemClock
Section titled “SystemClock”capability SystemClock {}The system’s monotonic clock. Only the runtime makes one, for main and
tests.
VirtualClock
Section titled “VirtualClock”capability VirtualClock {}A clock for tests whose time jumps instead of passing.
Each run (the program, or each test) has its own, at 0 at the start,
shared by its tasks.
When every task of the run waits and one waits on this clock, its time
jumps to the nearest deadline.
A test asks for it by name: test "times out" (clock: VirtualClock) { … }.
Traits
Section titled “Traits”capability trait Clock { /// Never goes back. fn now(self) -> Instant /// Returns at once for a duration of 0 or less. A cancelled task stops waiting at once. fn sleep(self, d: Duration) /// Wall-clock time since 1970-01-01 00:00 UTC. It may go back when the system clock is set. /// A clock without a calendar gives its own time. fn since_epoch(self) -> Duration}A source of time: SystemClock, VirtualClock or a mock.
In a select, clock.after(d) => … is an arm that fires once d has
passed since the select began.
Functions
Section titled “Functions”fn nanos(n: Int) -> Durationn nanoseconds.
micros
Section titled “micros”fn micros(n: Int) -> Durationn microseconds.
fn ms(n: Int) -> Durationn milliseconds.
seconds
Section titled “seconds”fn seconds(n: Int) -> Durationn seconds.
elapsed
Section titled “elapsed”fn elapsed(start: Instant, end: Instant) -> DurationThe time from start to end.
Implementations
Section titled “Implementations”impl Clock for SystemClockimpl Clock for VirtualClock