Trait Clock

Source
pub trait Clock: Clone + Default {
    type Instant: Copy + Sub<Output = Duration>;
    type Delay: Future<Output = ()> + Unpin;

    // Required methods
    fn now(&self) -> Self::Instant;
    fn sleep(&self, dur: Duration) -> Self::Delay;
}
Expand description

A Clock controls the passing of time.

Limiter uses sleep() to impose speed limit, and it relies on the current and past timestamps to determine how long to sleep. Both of these time-related features are encapsulated into this Clock trait.

§Implementing

The StandardClock should be enough in most situation. However, these are cases for a custom clock, e.g. use a coarse clock instead of the standard high-precision clock, or use a specialized future associated with an executor instead of the generic futures-timer.

Types implementing Clock must be cheap to clone (e.g. using Arc), and the default value must be ready to use.

Required Associated Types§

Source

type Instant: Copy + Sub<Output = Duration>

Type to represent a point of time.

Subtracting two instances should return the duration elapsed between them. The subtraction must never block or panic when they are properly ordered.

Source

type Delay: Future<Output = ()> + Unpin

Future type returned by sleep().

Required Methods§

Source

fn now(&self) -> Self::Instant

Returns the current time instant. It should be monotonically increasing, but not necessarily high-precision or steady.

This function must never block or panic.

Source

fn sleep(&self, dur: Duration) -> Self::Delay

Asynchronously sleeps the current task for the given duration.

This method should return a future which fulfilled as () after a duration of dur. It should not block the current thread.

sleep() is often called with a duration of 0 s. This should result in a future being resolved immediately.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§