Trait async_timer::timer::SyncTimer[][src]

pub trait SyncTimer: Timer {
    fn init<R, F: Fn(&TimerState) -> R>(&mut self, init: F) -> R;

    fn tick(&mut self) -> bool { ... }
}
Expand description

Describes timer interface that doesn’t require async event loop.

In most cases these timers are implemented by OS calling back a provided callback.

As notification is not done via event loop, timer has to store callback in its own state.

Whenever async timer relies on async event loop to handle notifications

Usage

use async_timer::timer::{Timer, SyncTimer, new_sync_timer};

use core::sync::atomic::{AtomicBool, Ordering};
use core::time;

use std::thread;

static EXPIRED: AtomicBool = AtomicBool::new(false);
fn on_expire() {
    EXPIRED.store(true, Ordering::Release);
}

let mut work = new_sync_timer(time::Duration::from_secs(1));
assert!(!work.is_ticking());
assert!(!work.is_expired());

work.init(|state| state.register(on_expire as fn()));
work.tick();

assert!(work.is_ticking());
assert!(!work.is_expired());
thread::sleep(time::Duration::from_millis(1250)); //timer is not necessary expires immediately

assert!(work.is_expired());
assert!(EXPIRED.load(Ordering::Acquire));

Required methods

Initializes timer state, performing initial arming and allowing to access TimerState during initialization

The state can be used to register callback and check whether timer has notified user using configured callback

If Timer is already armed, then TimerState is granted as it is.

Provided methods

Ticks timer.

If timer is not started yet, starts returning false. Otherwise performs necessary actions, if any, to drive timer and returns whether it is expired.

Default implementation initializes timer state, if necessary, and returns whether timer has expired or not.

Implementors