Skip to main content

Timer

Trait Timer 

Source
pub trait Timer: Send + Sync {
    // Required methods
    fn once<'async_trait>(
        duration_millis: u32,
    ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
       where Self: 'async_trait;
    fn interval<'async_trait>(
        duration_millis: u32,
    ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
       where Self: 'async_trait;
    fn cancel<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn once_sync(duration_millis: u32) -> Self;
    fn interval_sync(duration_millis: u32) -> Self;
    fn cancel_sync(&mut self);
    fn tick<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = u32> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

Trait for timer functionality across async runtimes.

Provides a unified interface for one-shot and interval timers that work with any supported async runtime.

§Examples

use product_os_async_executor::{Timer, TokioExecutor};

#[tokio::main]
async fn main() {
    // Create an interval timer that fires every 100ms
    let mut timer = TokioExecutor::interval(100).await;
     
    // Wait for the first tick
    let elapsed = timer.tick().await;
    println!("Elapsed: {}ms", elapsed);
}

Required Methods§

Source

fn once<'async_trait>( duration_millis: u32, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
where Self: 'async_trait,

Creates a one-shot timer that fires after the specified duration.

Source

fn interval<'async_trait>( duration_millis: u32, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
where Self: 'async_trait,

Creates an interval timer that fires repeatedly at the specified interval.

Source

fn cancel<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Cancels the timer.

Source

fn once_sync(duration_millis: u32) -> Self

Creates a one-shot timer synchronously.

Source

fn interval_sync(duration_millis: u32) -> Self

Creates an interval timer synchronously.

Source

fn cancel_sync(&mut self)

Cancels the timer synchronously.

Source

fn tick<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = u32> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Waits for the next tick of the timer and returns elapsed milliseconds.

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§