futures_time/future/mod.rs
1//! Asynchronous values.
2//!
3//! # Cancellation
4//!
5//! Futures can be cancelled by dropping them before they finish executing. This
6//! is useful when we're no longer interested in the result of an operation, as
7//! it allows us to stop doing needless work. This also means that a future may cancel at any `.await` point, and so just
8//! like with `?` we have to be careful to roll back local state if our future
9//! halts there.
10//!
11//! In order to perform a cancellation remotely, you can use the [`channel::bounded`]
12//! function to create a sender/receiver pair. When the sender side of
13//! this pair emits a message, all receivers are trigered. Receivers can be passed to
14//! [`Future::timeout`] or [`Stream::timeout`] to perform a cancellation when
15//! the message is received.
16//!
17//! [`channel::bounded`]: crate::channel::bounded
18//! [`Future::timeout`]: crate::future::FutureExt::timeout
19//! [`Stream::timeout`]: crate::stream::StreamExt::timeout
20//!
21//!
22//! ```
23//! use futures_lite::prelude::*;
24//! use futures_time::prelude::*;
25//! use futures_time::channel;
26//! use futures_time::time::Duration;
27//!
28//! fn main() {
29//! async_io::block_on(async {
30//! let (send, mut recv) = channel::bounded::<()>(1); // create a new send/receive pair
31//! let mut counter = 0;
32//! let value = async { "meow" }
33//! .delay(Duration::from_millis(100))
34//! .timeout(recv.next()) // time-out if the sender is dropped.
35//! .await;
36//!
37//! assert_eq!(value.unwrap(), "meow");
38//! })
39//! }
40//! ```
41
42mod delay;
43mod future_ext;
44mod into_future;
45mod park;
46mod relative_future;
47mod timeout;
48
49pub use delay::Delay;
50pub use future_ext::FutureExt;
51pub use into_future::IntoFuture;
52pub use park::Park;
53pub use relative_future::Timer;
54pub use timeout::Timeout;