[−][src]Struct thread_timer::ThreadTimer
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]
F: FnOnce() + Send + 'static,
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). This function waits until the wait thread has confirmed that it is ready to start waiting again, so it is safe to call start immediately after calling this function. 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!", );
Trait Implementations
impl Clone for ThreadTimer
[src]
fn clone(&self) -> ThreadTimer
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Default for ThreadTimer
[src]
Auto Trait Implementations
impl !RefUnwindSafe for ThreadTimer
impl Send for ThreadTimer
impl !Sync for ThreadTimer
impl Unpin for ThreadTimer
impl !UnwindSafe for ThreadTimer
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,