pub trait TimeServiceTrait: Send + Sync + Clone + Debug {
    // Required methods
    fn now(&self) -> Instant;
    fn now_unix_time(&self) -> Duration;
    fn sleep(&self, duration: Duration) -> Sleep ;
    fn sleep_blocking(&self, duration: Duration);

    // Provided methods
    fn now_secs(&self) -> u64 { ... }
    fn sleep_until(&self, deadline: Instant) -> Sleep  { ... }
    fn interval(&self, period: Duration) -> Interval { ... }
    fn interval_at(&self, start: Instant, period: Duration) -> Interval { ... }
    fn timeout<F: Future>(&self, duration: Duration, future: F) -> Timeout<F>  { ... }
    fn timeout_at<F: Future>(&self, deadline: Instant, future: F) -> Timeout<F>  { ... }
}

Required Methods§

source

fn now(&self) -> Instant

Query a monotonically nondecreasing clock. Returns an opaque type that can only be compared to other Instants, i.e., this is a monotonic relative time whereas now_unix_time is a non-monotonic absolute time.

On Linux, this is equivalent to clock_gettime(CLOCK_MONOTONIC, _)

See Instant for more details.

source

fn now_unix_time(&self) -> Duration

Query the current unix timestamp as a Duration.

When used on a TimeService::real(), this is equivalent to SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).

Note: the Duration returned from this function is NOT guaranteed to be monotonic. Use now if you need monotonicity.

From the SystemTime docs:

Distinct from the Instant type, this time measurement is not monotonic. This means that you can save a file to the file system, then save another file to the file system, and the second file has a SystemTime measurement earlier than the first. In other words, an operation that happens after another operation in real time may have an earlier SystemTime!

For example, the system administrator could clock_settime into the past, breaking clock time monotonicity.

On Linux, this is equivalent to clock_gettime(CLOCK_REALTIME, _).

source

fn sleep(&self, duration: Duration) -> Sleep

Return a Future that waits until duration has passed.

No work is performed while awaiting on the sleep future to complete. Sleep operates at millisecond granularity and should not be used for tasks that require high-resolution timers.

Cancelation

Canceling a sleep instance is done by dropping the returned future. No additional cleanup work is required.

source

fn sleep_blocking(&self, duration: Duration)

Blocks the current thread until duration time has passed.

Provided Methods§

source

fn now_secs(&self) -> u64

Query the current unix timestamp in seconds.

Equivalent to self.now_unix_time().as_secs(). See now_unix_time.

source

fn sleep_until(&self, deadline: Instant) -> Sleep

Return a Future that waits until the deadline.

If the deadline is in the past, the Sleep will trigger as soons as it’s polled.

See sleep for more details.

source

fn interval(&self, period: Duration) -> Interval

Creates a new Interval that yields with interval of period. The first tick completes immediately. An interval will tick indefinitely.

Cancelation

At any time, the Interval value can be dropped. This cancels the interval.

Panics

This function panics if period is zero.

source

fn interval_at(&self, start: Instant, period: Duration) -> Interval

Creates a new Interval that yields with interval of period. The first tick completes after the start deadline. An interval will tick indefinitely.

See interval for more details.

source

fn timeout<F: Future>(&self, duration: Duration, future: F) -> Timeout<F>

Require a Future to complete before the specified duration has elapsed.

If the future completes before the duration has elapsed, then the completed value is returned. Otherwise, Err(Elapsed) is returned and the future is canceled.

Cancelation

Cancelling a timeout is done by dropping the future. No additional cleanup or other work is required.

The original future may be obtained by calling Timeout::into_inner. This consumes the Timeout.

source

fn timeout_at<F: Future>(&self, deadline: Instant, future: F) -> Timeout<F>

Require a Future to complete before the deadline.

If the future completes before the duration has elapsed, then the completed value is returned. Otherwise, Err(Elapsed) is returned and the future is canceled.

See timeout for more details.

Implementors§