Clock

Trait Clock 

Source
pub trait Clock {
    // Required methods
    fn now(&self) -> Duration;
    fn consume(&mut self, inteval: &Duration);
}
Expand description

Clock is a simulated system clock. Its run rate may be real-time (i.e. one simulated second per actual wall-clock second) or it may run faster or slower than real-time.

The clock keeps track of how many of its cycles have been “consumed” by callers. Callers requiring more clock cycles will find that their calls to Clock::consume block so that their average consumption of cycles matches the simulated clock rate.

On the other hand, if the simulated clock produces ticks very rapidly (for example because it is set up to run 1,000,000x “real” time) then callers will never block and hence can proceed as fast as they are able.

Required Methods§

Source

fn now(&self) -> Duration

Retrieves the current (simulated) time.

Source

fn consume(&mut self, inteval: &Duration)

The caller calls consume to simulate the passing of a duration interval. The returned value is the interval after which it is next OK to call consume.

§Examples
use std::thread::sleep;
use std::time::Duration;
use cpu::Clock;

fn g<C: Clock>(clk: &mut C) {
  // We just performed an action which would have taken
  // one millisecond on the simulated machine.
  clk.consume(&Duration::from_millis(1));
}

Implementors§