Skip to content

std.time

distinct type Instant = Int

Nanoseconds since the clock’s own start. Only the difference of two instants means something.

distinct type Duration = Int

Nanoseconds.

capability SystemClock {}

The system’s monotonic clock. Only the runtime makes one, for main and tests.

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) { … }.

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.

fn nanos(n: Int) -> Duration

n nanoseconds.

fn micros(n: Int) -> Duration

n microseconds.

fn ms(n: Int) -> Duration

n milliseconds.

fn seconds(n: Int) -> Duration

n seconds.

fn elapsed(start: Instant, end: Instant) -> Duration

The time from start to end.

  • impl Clock for SystemClock
  • impl Clock for VirtualClock