A general purpose crate for working with timeouts and delays with futures.
This crate is intended to provide general purpose timeouts and interval
streams for working with
futures. The implementation may not be optimized
for your particular use case, though, so be sure to read up on the details
if you're concerned about that!
Basic usage of this crate is relatively simple:
use std::time::Duration; use futures_timer::Delay; use futures::prelude::*; let now = Delay::new(Duration::from_secs(3)).await; println!("waited for 3 secs");
In addition to a one-shot future you can also create a stream of delayed
notifications with the
use std::time::Duration; use futures_timer::Interval; use futures::prelude::*; let dur = Duration::from_secs(4); let stream = Interval::new(dur) .map(|()| println!("prints every four seconds")); // spawn or use the stream
And you're off to the races! Check out the API documentation for more
details about the various methods on
Interval types are powered by an associated
default constructors like
Interval::new use a global
Timer to power their usage. This global
Timer is spawned
onto a helper thread which continuously runs in the background sending out
If needed, however, a
Timer can be constructed manually and the
Delay::new_handle-style methods can be used to create delays/intervals
associated with a specific instance of
Timer has a
TimerHandle type which is used to associate new objects to it.
Note that there's also a
TimerHandle::set_fallback method which will
globally configure the fallback timer handle as well if you'd like to run
your own timer.
Finally, the implementation of
Timer itself is currently a binary heap.
Timer insertion is O(log n) where n is the number of active timers, and so
is firing a timer (which invovles removing from the heap).
Extension traits for the standard
A future representing the notification that an elapsed duration has occurred.
A stream representing notifications at fixed interval
Error returned from
A "timer heap" used to power separately owned instances of
A handle to a