Module time

Source
Expand description

Abstractions for time-based operations.

The main purpose of this module is to provide the definition of TimeAbstraction, the trait that has to be implemented to interact with the underlying operating system when performing time-based operations. In order to keep the time abstractions as decoupled as possible from the running environment, this module provides its own Instant and Duration types.

§Example

Runtime actors using time-based operations should rely on the TimeAbstraction trait, and never use specific implementations. It is during the runtime’s setup that the concrete implementation for the targeted environment has to be specified.

use core::convert::Infallible;

use veecle_osal_api::time::{Duration, TimeAbstraction};
use veecle_osal_std::time::Time;
use veecle_os_runtime::{Reader, Storable, Writer};

#[derive(Debug, Clone, PartialEq, Eq, Default, Storable)]
pub struct Tick {
    since_epoch: Duration,
}

#[veecle_os_runtime::actor]
async fn tick_writer<T>(mut writer: Writer<'_, Tick>) -> Infallible
where
    T: TimeAbstraction,
{
    let epoch = T::now();

    loop {
        let _ = T::sleep_until(T::now() + Duration::from_secs(1)).await;
        writer
            .write(Tick {
                since_epoch: T::now()
                    .duration_since(epoch)
                    .expect("now should be later than epoch"),
            })
            .await;
    }
}

#[veecle_os_runtime::actor]
async fn tick_reader(mut reader: Reader<'_, Tick>) -> Infallible {
    loop {
        reader.wait_for_update().await.read(|tick| {
            println!("[READER TASK] Tick received: {tick:?}");
        })
    }
}

veecle_os_runtime::execute! {
    store: [Tick],
    actors: [
        TickWriter<Time>,
        TickReader,
    ]
}.await;

unreachable!("the runtime instance does not return");

Structs§

Duration
Duration represents a span of time.
Exceeded
A TimeAbstraction::timeout_at reached the deadline before the future resolved.
Instant
An Instant in time. Instants should be always increasing and are generally obtainable through the operating system time driver.

Enums§

SystemTimeError
An error that may happen while working with System Time.

Traits§

Interval
A stream of periodic ticks, created by TimeAbstraction::interval.
SystemTime
Provides a measurement of the system time.
SystemTimeSync
Allows manual synchronization of SystemTime.
TimeAbstraction
TimeAbstraction is used to perform time-related operations in a platform-agnostic manner.