1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/async--fuse-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/async-fuse)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/async-fuse.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/async-fuse)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-async--fuse-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/async-fuse)
//!
//! Helpers for "fusing" asynchronous computations.
//!
//! A fused operation has a well-defined behavior once the operation has
//! completed. For [`Fuse`] it means that an operation that has completed will
//! *block forever* by returning [`Poll::Pending`].
//!
//! This is similar to the [`Fuse`][futures-fs-fuse] type provided in
//! futures-rs, but provides more utility allowing it to interact with types
//! which does not implement [`FusedFuture`] or [`FusedStream`] as is now the
//! case with all Tokio types since 1.0.
//!
//! We also use [`Fuse`] to represent optional values, just like `Option`. But
//! [`Fuse`] provides implementations and functions which allow us to safely
//! perform operations over the value when it's pinned. Exactly what's needed to
//! drive a [`Stream`] (see [`next`]) or poll a [`Future`] that might or might
//! not be set.
//!
//! <br>
//!
//! ## Features
//!
//! * `stream` - Makes the [`Fuse`] implement the [`Stream`] trait if it contains a
//! stream.
//!
//! <br>
//!
//! ## Simplifying [`tokio::select!`]
//!
//! One of the main uses for [`Fuse`] is to simplify how we use
//! [`tokio::select!`]. In this section we'll look at how we can improve an
//! optional branch, where the future being polled might or might not be set.
//!
//! ```rust
//! # #[tokio::main]
//! # async fn main() {
//! let mut maybe_future = Some(async { 42u32 });
//! tokio::pin!(maybe_future);
//!
//! tokio::select! {
//! value = async { maybe_future.as_mut().as_pin_mut().unwrap().await }, if maybe_future.is_some() => {
//! maybe_future.set(None);
//! assert_eq!(value, 42);
//! }
//! /* other branches */
//! }
//!
//! assert!(maybe_future.is_none());
//! # }
//! ```
//!
//! The `async` block above is necessary because the future is polled *eagerly*
//! regardless of the [branch precondition]. This would cause the `unwrap` to
//! panic in case the future isn't set. We also need to explicitly set the pin
//! to `None` after completion. Otherwise we might poll it later [which might
//! panic].
//!
//! With [`Fuse`] we can rewrite the branch and remove the `async` block. It also
//! unsets the future for us after completion.
//!
//! ```rust
//! use async_fuse::Fuse;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let mut maybe_future = Fuse::new(async { 42u32 });
//! tokio::pin!(maybe_future);
//!
//! tokio::select! {
//! value = &mut maybe_future, if !maybe_future.is_empty() => {
//! assert_eq!(value, 42);
//! }
//! /* other branches */
//! }
//!
//! assert!(maybe_future.is_empty());
//! # }
//! ```
//!
//! Finally if we don't need the [else branch] to evalute we can skip the
//! [branch precondition] entirely. Allowing us to further reduce the code.
//!
//! ```rust
//! use async_fuse::Fuse;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let mut maybe_future = Fuse::new(async { 42u32 });
//! tokio::pin!(maybe_future);
//!
//! tokio::select! {
//! value = &mut maybe_future => {
//! assert_eq!(value, 42);
//! }
//! /* other branches */
//! }
//!
//! assert!(maybe_future.is_empty());
//! # }
//! ```
//!
//! <br>
//!
//! ## Fusing on the stack
//!
//! For the first example we'll be fusing the value *on the stack* using
//! [`tokio::pin!`]. We'll also be updating the fuse as it completes with
//! another sleep with a configurable delay. Mimicking the behavior of
//! [`Interval`].
//!
//! > This is available as the `stack_ticker` example:
//! > ```sh
//! > cargo run --example stack_ticker
//! > ```
//!
//! ```rust
//! use async_fuse::Fuse;
//! use std::time::Duration;
//! use tokio::time;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let mut duration = Duration::from_millis(500);
//!
//! let sleep = Fuse::new(time::sleep(duration));
//! tokio::pin!(sleep);
//!
//! let update_duration = Fuse::new(time::sleep(Duration::from_secs(2)));
//! tokio::pin!(update_duration);
//!
//! for _ in 0..10usize {
//! tokio::select! {
//! _ = &mut sleep => {
//! println!("Tick");
//! sleep.set(Fuse::new(time::sleep(duration)));
//! }
//! _ = &mut update_duration => {
//! println!("Tick faster!");
//! duration = Duration::from_millis(250);
//! }
//! }
//! }
//! # }
//! ```
//!
//! <br>
//!
//! ## Fusing on the heap
//!
//! For some types it might be easier to fuse the value on the heap. To make
//! this easier, we provide the [`Fuse::pin`] constructor which provides a fused
//! value which is pinned on the heap.
//!
//! As a result, it looks pretty similar to the above example.
//!
//! > This is available as the `heap_ticker` example:
//! > ```sh
//! > cargo run --example heap_ticker
//! > ```
//!
//! ```rust
//! use async_fuse::Fuse;
//! use std::time::Duration;
//! use tokio::time;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let mut duration = Duration::from_millis(500);
//!
//! let mut sleep = Fuse::pin(time::sleep(duration));
//! let mut update_duration = Fuse::pin(time::sleep(Duration::from_secs(2)));
//!
//! for _ in 0..10usize {
//! tokio::select! {
//! _ = &mut sleep => {
//! println!("Tick");
//! sleep.set(Box::pin(time::sleep(duration)));
//! }
//! _ = &mut update_duration => {
//! println!("Tick faster!");
//! duration = Duration::from_millis(250);
//! }
//! }
//! }
//! # }
//! ```
//!
//! <br>
//!
//! ## Fusing trait objects
//!
//! The following showcases how we can fuse a trait object. Trait objects are
//! useful since they allow the fused value to change between distinct
//! implementations. The price is that we perform dynamic dispatch which has a
//! small cost.
//!
//! Also note that because [`CoerceUnsized`] is not yet stable, we cannot use
//! [`Fuse::pin`] for convenience and have to pass a pinned box through
//! [`Fuse::new`].
//!
//! > This is available as the `trait_object_ticker` example:
//! > ```sh
//! > cargo run --example trait_object_ticker
//! > ```
//!
//! ```rust
//! use async_fuse::Fuse;
//! use std::future::Future;
//! use std::pin::Pin;
//! use std::time::Duration;
//! use tokio::time;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let mut duration = Duration::from_millis(500);
//!
//! let mut sleep: Fuse<Pin<Box<dyn Future<Output = ()>>>> =
//! Fuse::new(Box::pin(time::sleep(duration)));
//!
//! let mut update_duration: Fuse<Pin<Box<dyn Future<Output = ()>>>> =
//! Fuse::new(Box::pin(time::sleep(Duration::from_secs(2))));
//!
//! for _ in 0..10usize {
//! tokio::select! {
//! _ = &mut sleep => {
//! println!("Tick");
//! sleep.set(Box::pin(time::sleep(duration)));
//! }
//! _ = &mut update_duration => {
//! println!("Tick faster!");
//! duration = Duration::from_millis(250);
//! }
//! }
//! }
//! # }
//! ```
//!
//! [`CoerceUnsized`]: https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html
//! [`Fuse::new`]: https://docs.rs/async-fuse/0/async_fuse/struct.Fuse.html#method.new
//! [`Fuse::pin`]: https://docs.rs/async-fuse/0/async_fuse/struct.Fuse.html#method.pin
//! [`Fuse`]: https://docs.rs/async-fuse/0/async_fuse/struct.Fuse.html
//! [`FusedFuture`]: https://docs.rs/futures/0/futures/future/trait.FusedFuture.html
//! [`FusedStream`]: https://docs.rs/futures/0/futures/stream/trait.FusedStream.html
//! [`Future`]: https://doc.rust-lang.org/std/future/trait.Future.html
//! [`Interval`]: https://docs.rs/tokio/1/tokio/time/struct.Interval.html
//! [`next`]: https://docs.rs/async-fuse/0/async_fuse/struct.Fuse.html#method.next
//! [`Poll::Pending`]: https://doc.rust-lang.org/std/task/enum.Poll.html#variant.Pending
//! [`Stream`]: https://docs.rs/futures-core/0/futures_core/stream/trait.Stream.html
//! [`tokio::pin!`]: https://docs.rs/tokio/1/tokio/macro.pin.html
//! [`tokio::select!`]: https://docs.rs/tokio/1/tokio/macro.select.html
//! [branch precondition]: https://docs.rs/tokio/1.0.1/tokio/macro.select.html#avoid-racy-if-preconditions
//! [else branch]: https://docs.rs/tokio/1.0.1/tokio/macro.select.html
//! [futures-fs-fuse]: https://docs.rs/futures/0/futures/future/struct.Fuse.html
//! [which might panic]: https://doc.rust-lang.org/std/future/trait.Future.html#panics
#![deny(missing_docs)]
mod fuse;
mod poll;
pub use self::fuse::Fuse;
#[cfg(feature = "stream")]
pub use futures_core::Stream;