2020-11-13 18:24:26 +01:00
|
|
|
//! Composable asynchronous iteration.
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! If you've found yourself with an asynchronous collection of some kind,
|
2020-11-13 18:24:26 +01:00
|
|
|
//! and needed to perform an operation on the elements of said collection,
|
2022-02-03 17:56:10 +08:00
|
|
|
//! you'll quickly run into 'async iterators'. Async Iterators are heavily used in
|
|
|
|
|
//! idiomatic asynchronous Rust code, so it's worth becoming familiar with them.
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
|
|
|
|
//! Before explaining more, let's talk about how this module is structured:
|
|
|
|
|
//!
|
|
|
|
|
//! # Organization
|
|
|
|
|
//!
|
|
|
|
|
//! This module is largely organized by type:
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! * [Traits] are the core portion: these traits define what kind of async iterators
|
2020-11-13 18:24:26 +01:00
|
|
|
//! exist and what you can do with them. The methods of these traits are worth
|
|
|
|
|
//! putting some extra study time into.
|
2022-02-03 17:56:10 +08:00
|
|
|
//! * Functions provide some helpful ways to create some basic async iterators.
|
2021-01-15 15:54:09 +01:00
|
|
|
//! * Structs are often the return types of the various methods on this
|
2020-11-13 18:24:26 +01:00
|
|
|
//! module's traits. You'll usually want to look at the method that creates
|
|
|
|
|
//! the `struct`, rather than the `struct` itself. For more detail about why,
|
2022-02-03 17:56:10 +08:00
|
|
|
//! see '[Implementing Async Iterator](#implementing-async-iterator)'.
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
|
|
|
|
//! [Traits]: #traits
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! That's it! Let's dig into async iterators.
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! # Async Iterators
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! The heart and soul of this module is the [`AsyncIterator`] trait. The core of
|
|
|
|
|
//! [`AsyncIterator`] looks like this:
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
|
|
|
|
//! ```
|
|
|
|
|
//! # use core::task::{Context, Poll};
|
|
|
|
|
//! # use core::pin::Pin;
|
2022-02-03 17:56:10 +08:00
|
|
|
//! trait AsyncIterator {
|
2020-11-13 18:24:26 +01:00
|
|
|
//! type Item;
|
|
|
|
|
//! fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
|
|
|
|
|
//! }
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! Unlike `Iterator`, `AsyncIterator` makes a distinction between the [`poll_next`]
|
|
|
|
|
//! method which is used when implementing an `AsyncIterator`, and a (to-be-implemented)
|
|
|
|
|
//! `next` method which is used when consuming an async iterator. Consumers of `AsyncIterator`
|
2021-01-15 15:54:09 +01:00
|
|
|
//! only need to consider `next`, which when called, returns a future which
|
2022-02-03 17:56:10 +08:00
|
|
|
//! yields `Option<AsyncIterator::Item>`.
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
2021-01-15 15:54:09 +01:00
|
|
|
//! The future returned by `next` will yield `Some(Item)` as long as there are
|
2020-11-13 18:24:26 +01:00
|
|
|
//! elements, and once they've all been exhausted, will yield `None` to indicate
|
|
|
|
|
//! that iteration is finished. If we're waiting on something asynchronous to
|
2022-02-03 17:56:10 +08:00
|
|
|
//! resolve, the future will wait until the async iterator is ready to yield again.
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! Individual async iterators may choose to resume iteration, and so calling `next`
|
2020-11-13 18:24:26 +01:00
|
|
|
//! again may or may not eventually yield `Some(Item)` again at some point.
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! [`AsyncIterator`]'s full definition includes a number of other methods as well,
|
2020-11-13 18:24:26 +01:00
|
|
|
//! but they are default methods, built on top of [`poll_next`], and so you get
|
|
|
|
|
//! them for free.
|
|
|
|
|
//!
|
|
|
|
|
//! [`Poll`]: super::task::Poll
|
2022-02-03 17:56:10 +08:00
|
|
|
//! [`poll_next`]: AsyncIterator::poll_next
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! # Implementing Async Iterator
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! Creating an async iterator of your own involves two steps: creating a `struct` to
|
|
|
|
|
//! hold the async iterator's state, and then implementing [`AsyncIterator`] for that
|
2020-11-13 18:24:26 +01:00
|
|
|
//! `struct`.
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! Let's make an async iterator named `Counter` which counts from `1` to `5`:
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
|
|
|
|
//! ```no_run
|
2022-02-03 17:56:10 +08:00
|
|
|
//! #![feature(async_iterator)]
|
|
|
|
|
//! # use core::async_iter::AsyncIterator;
|
2020-11-13 18:24:26 +01:00
|
|
|
//! # use core::task::{Context, Poll};
|
|
|
|
|
//! # use core::pin::Pin;
|
|
|
|
|
//!
|
|
|
|
|
//! // First, the struct:
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! /// An async iterator which counts from one to five
|
2020-11-13 18:24:26 +01:00
|
|
|
//! struct Counter {
|
|
|
|
|
//! count: usize,
|
|
|
|
|
//! }
|
|
|
|
|
//!
|
|
|
|
|
//! // we want our count to start at one, so let's add a new() method to help.
|
|
|
|
|
//! // This isn't strictly necessary, but is convenient. Note that we start
|
|
|
|
|
//! // `count` at zero, we'll see why in `poll_next()`'s implementation below.
|
|
|
|
|
//! impl Counter {
|
|
|
|
|
//! fn new() -> Counter {
|
|
|
|
|
//! Counter { count: 0 }
|
|
|
|
|
//! }
|
|
|
|
|
//! }
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! // Then, we implement `AsyncIterator` for our `Counter`:
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! impl AsyncIterator for Counter {
|
2020-11-13 18:24:26 +01:00
|
|
|
//! // we will be counting with usize
|
|
|
|
|
//! type Item = usize;
|
|
|
|
|
//!
|
|
|
|
|
//! // poll_next() is the only required method
|
|
|
|
|
//! fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
|
//! // Increment our count. This is why we started at zero.
|
|
|
|
|
//! self.count += 1;
|
|
|
|
|
//!
|
|
|
|
|
//! // Check to see if we've finished counting or not.
|
|
|
|
|
//! if self.count < 6 {
|
|
|
|
|
//! Poll::Ready(Some(self.count))
|
|
|
|
|
//! } else {
|
|
|
|
|
//! Poll::Ready(None)
|
|
|
|
|
//! }
|
|
|
|
|
//! }
|
|
|
|
|
//! }
|
|
|
|
|
//! ```
|
|
|
|
|
//!
|
|
|
|
|
//! # Laziness
|
|
|
|
|
//!
|
2022-02-03 17:56:10 +08:00
|
|
|
//! Async iterators are *lazy*. This means that just creating an async iterator doesn't
|
|
|
|
|
//! _do_ a whole lot. Nothing really happens until you call `poll_next`. This is
|
|
|
|
|
//! sometimes a source of confusion when creating an async iterator solely for its side
|
2021-11-21 11:57:38 +01:00
|
|
|
//! effects. The compiler will warn us about this kind of behavior:
|
2020-11-13 18:24:26 +01:00
|
|
|
//!
|
|
|
|
|
//! ```text
|
2022-02-03 17:56:10 +08:00
|
|
|
//! warning: unused result that must be used: async iterators do nothing unless polled
|
2020-11-13 18:24:26 +01:00
|
|
|
//! ```
|
|
|
|
|
|
2022-02-03 17:56:10 +08:00
|
|
|
mod async_iter;
|
2021-02-04 01:25:43 +01:00
|
|
|
mod from_iter;
|
2020-11-13 18:24:26 +01:00
|
|
|
|
2022-02-03 17:56:10 +08:00
|
|
|
pub use async_iter::AsyncIterator;
|
2021-02-04 01:25:43 +01:00
|
|
|
pub use from_iter::{from_iter, FromIter};
|