Skip to main content

Clock

Trait Clock 

Source
pub trait Clock:
    Clock<Instant = SystemTime>
    + ReasonablyRealtime
    + Send
    + Sync
    + 'static {
    // Required methods
    fn current(&self) -> SystemTime;
    fn sleep(
        &self,
        duration: Duration,
    ) -> impl Future<Output = ()> + Send + 'static;
    fn sleep_until(
        &self,
        deadline: SystemTime,
    ) -> impl Future<Output = ()> + Send + 'static;

    // Provided method
    fn timeout<F, T>(
        &self,
        duration: Duration,
        future: F,
    ) -> impl Future<Output = Result<T, Error>> + Send + '_
       where F: Future<Output = T> + Send + 'static,
             T: Send + 'static { ... }
}
Expand description

Interface that any task scheduler must implement to provide time-based operations.

It is necessary to mock time to provide deterministic execution of arbitrary tasks.

Required Methods§

Source

fn current(&self) -> SystemTime

Returns the current time.

Source

fn sleep(&self, duration: Duration) -> impl Future<Output = ()> + Send + 'static

Sleep for the given duration.

Source

fn sleep_until( &self, deadline: SystemTime, ) -> impl Future<Output = ()> + Send + 'static

Sleep until the given deadline.

Provided Methods§

Source

fn timeout<F, T>( &self, duration: Duration, future: F, ) -> impl Future<Output = Result<T, Error>> + Send + '_
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Await a future with a timeout, returning Error::Timeout if it expires.

§Examples
use std::time::Duration;
use commonware_runtime::{deterministic, Error, Runner, Clock};

let executor = deterministic::Runner::default();
executor.start(|context| async move {
    match context
        .timeout(Duration::from_millis(100), async { 42 })
        .await
    {
        Ok(value) => assert_eq!(value, 42),
        Err(Error::Timeout) => panic!("should not timeout"),
        Err(e) => panic!("unexpected error: {:?}", e),
    }
});

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Clock for commonware_runtime::deterministic::Context

Source§

impl Clock for commonware_runtime::tokio::Context