[][src]Struct hjul::Timer

pub struct Timer(_);

Methods

impl Timer[src]

pub fn stop(&self)[src]

Stop the timer (preventing execution of the callback in the future)

Note

Another way to stop a timer is to drop every clone of the timer.

Example

let timer = runner.timer(|| assert!(false));
timer.reset(Duration::from_millis(200));
timer.stop();

// callback is never executed
thread::sleep(Duration::from_millis(500));

pub fn reset(&self, duration: Duration)[src]

Restart the timer, regardless of whether the timer is running or not. e.g. repeatably calling .reset(1 sec) will cause the timer to never fire.

Arguments

  • duration: duration until the callback should execute

Example

let timer = runner.timer(|| assert!(false));

// the timer never fires
let dur = Duration::from_millis(200);
for _ in 0..5 {
    timer.reset(dur);
    thread::sleep(dur / 2);
}

// timer is dropped and cancelled

pub fn start(&self, duration: Duration) -> bool[src]

Start the timer, but only if the timer is not already pending e.g. if repeatably calling .start(1 sec), the timer will fire ~ once every second

Arguments

  • duration: duration until the callback should execute

Returns

A bool indicating whether the timer was started (true) or already running (false).

Example

let timer = runner.timer(|| println!("fired"));

// this timer will fire twice
let dur = Duration::from_millis(200);
for _ in 0..5 {
    timer.start(dur);
    thread::sleep(dur / 2);
}

// timer is dropped and cancelled

pub fn fire(&self)[src]

Manually cause the timer to fire immediately. This cancels any pending timeout (equivalent to calling .stop()) before executing the callback.

Note

The callback is run in the calling thread as oppose to being run by the thread in the associated Runner instance.

Example

let timer = runner.timer(|| println!("fired"));

timer.start(Duration::from_millis(200));
timer.fire();

// timer is fired immediately, not after 200ms.

Trait Implementations

impl Clone for Timer[src]

impl Debug for Timer[src]

Auto Trait Implementations

impl Send for Timer

impl Sync for Timer

impl Unpin for Timer

impl !UnwindSafe for Timer

impl !RefUnwindSafe for Timer

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]