[][src]Struct thread_timer::ThreadTimer

pub struct ThreadTimer { /* fields omitted */ }

A simple, cancelable timer that can run a thunk after waiting for an arbitrary duration.

Waiting is accomplished by using a helper thread (the "wait thread") that listens for incoming wait requests and then executes the requested thunk after blocking for the requested duration. Because each ThreadTimer keeps only one wait thread, each ThreadTimer may only be waiting for a single thunk at a time.

use std::sync::mpsc::{self, TryRecvError};
use std::thread;
use std::time::Duration;
use thread_timer::ThreadTimer;

let (sender, receiver) = mpsc::channel::<bool>();
let timer = ThreadTimer::new();

timer.start(Duration::from_millis(50), move || { sender.send(true).unwrap() }).unwrap();

thread::sleep(Duration::from_millis(60));
assert_eq!(receiver.try_recv(), Ok(true));

If a ThreadTimer is currently waiting to execute a thunk, the wait can be canceled, in which case the thunk will not be run.

use std::sync::mpsc::{self, TryRecvError};
use std::thread;
use std::time::Duration;
use thread_timer::ThreadTimer;

let (sender, receiver) = mpsc::channel::<bool>();
let timer = ThreadTimer::new();

timer.start(Duration::from_millis(50), move || { sender.send(true).unwrap() }).unwrap();

thread::sleep(Duration::from_millis(10));
timer.cancel().unwrap();

thread::sleep(Duration::from_millis(60));
assert_eq!(receiver.try_recv(), Err(TryRecvError::Disconnected));

Implementations

impl ThreadTimer[src]

pub fn new() -> Self[src]

Creates and returns a new ThreadTimer. Spawns a new thread to do the waiting (the "wait thread").

use thread_timer::ThreadTimer;
let timer = ThreadTimer::new();

pub fn start<F>(&self, dur: Duration, f: F) -> Result<(), TimerStartError> where
    F: FnOnce() + Send + 'static, 
[src]

Start waiting. Wait for dur to elapse then execute f. Will not execute f if the timer is canceled before dur elapses. Returns TimerStartError::AlreadyWaiting if the timer is already waiting to execute a thunk.

use std::sync::mpsc::{self, TryRecvError};
use std::thread;
use std::time::Duration;
use thread_timer::ThreadTimer;

let (sender, receiver) = mpsc::channel::<bool>();
let timer = ThreadTimer::new();

timer.start(Duration::from_millis(50), move || { sender.send(true).unwrap() }).unwrap();
assert_eq!(
  receiver.try_recv(),
  Err(TryRecvError::Empty),
  "Received response before wait elapsed!",
);

thread::sleep(Duration::from_millis(60));
assert_eq!(
  receiver.try_recv(),
  Ok(true),
  "Did not receive response after wait elapsed!",
);

pub fn cancel(&self) -> Result<(), TimerCancelError>[src]

Cancel the current timer (the thunk will not be executed and the timer will be able to start waiting to execute another thunk). Returns TimerCancelError::NotWaiting if the timer is not currently waiting.

use std::sync::mpsc::{self, TryRecvError};
use std::thread;
use std::time::Duration;
use thread_timer::ThreadTimer;

let (sender, receiver) = mpsc::channel::<bool>();
let timer = ThreadTimer::new();

timer.start(Duration::from_millis(50), move || { sender.send(true).unwrap() }).unwrap();

// Make sure the wait has actually started before we cancel
thread::sleep(Duration::from_millis(10));
timer.cancel().unwrap();

thread::sleep(Duration::from_millis(60));
assert_eq!(
  receiver.try_recv(),
  // When the wait is canceled, the thunk and its Sender will be dropped
  Err(TryRecvError::Disconnected),
  "Received response from canceled wait!",
);

Auto Trait Implementations

Blanket Implementations

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

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

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

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

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

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.