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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
//! Composable asynchronous iteration. //! //! This module is an async version of [`std::iter`]. //! //! If you've found yourself with an asynchronous collection of some kind, //! and needed to perform an operation on the elements of said collection, //! you'll quickly run into 'streams'. Streams are heavily used in idiomatic //! asynchronous Rust code, so it's worth becoming familiar with them. //! //! Before explaining more, let's talk about how this module is structured: //! //! # Organization //! //! This module is largely organized by type: //! //! * [Traits] are the core portion: these traits define what kind of streams //! exist and what you can do with them. The methods of these traits are worth //! putting some extra study time into. //! * [Functions] provide some helpful ways to create some basic streams. //! * [Structs] are often the return types of the various methods on this //! 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, //! see '[Implementing Stream](#implementing-stream)'. //! //! [Traits]: #traits //! [Functions]: #functions //! [Structs]: #structs //! //! That's it! Let's dig into streams. //! //! # Stream //! //! The heart and soul of this module is the [`Stream`] trait. The core of //! [`Stream`] looks like this: //! //! ``` //! # use async_std::task::{Context, Poll}; //! # use std::pin::Pin; //! trait Stream { //! type Item; //! fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>; //! } //! ``` //! //! A stream has a method, [`next`], which when called, returns an //! [`Poll`]<[`Option`]`<Item>>`. [`next`] will return `Ready(Some(Item))` //! as long as there are elements, and once they've all been exhausted, will //! return `None` to indicate that iteration is finished. If we're waiting on //! something asynchronous to resolve `Pending` is returned. //! //! Individual streams may choose to resume iteration, and so calling //! [`next`] again may or may not eventually start returning `Ready(Some(Item))` //! again at some point. //! //! [`Stream`]'s full definition includes a number of other methods as well, //! but they are default methods, built on top of [`next`], and so you get //! them for free. //! //! Streams are also composable, and it's common to chain them together to do //! more complex forms of processing. See the [Adapters](#adapters) section //! below for more details. //! //! [`Poll`]: ../task/enum.Poll.html //! [`Stream`]: trait.Stream.html //! [`next`]: trait.Stream.html#tymethod.next //! [`Option`]: ../../std/option/enum.Option.html //! //! # The three forms of streaming //! //! There are three common methods which can create streams from a collection: //! //! * `stream()`, which iterates over `&T`. //! * `stream_mut()`, which iterates over `&mut T`. //! * `into_stream()`, which iterates over `T`. //! //! Various things in async-std may implement one or more of the //! three, where appropriate. //! //! # Implementing Stream //! //! Creating a stream of your own involves two steps: creating a `struct` to //! hold the stream's state, and then `impl`ementing [`Stream`] for that //! `struct`. This is why there are so many `struct`s in this module: there is //! one for each stream and iterator adapter. //! //! Let's make a stream named `Counter` which counts from `1` to `5`: //! //! ``` //! # use async_std::prelude::*; //! # use async_std::task::{Context, Poll}; //! # use std::pin::Pin; //! // First, the struct: //! //! /// A stream which counts from one to five //! 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 `next()`'s implementation below. //! impl Counter { //! fn new() -> Counter { //! Counter { count: 0 } //! } //! } //! //! // Then, we implement `Stream` for our `Counter`: //! //! impl Stream for Counter { //! // 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) //! } //! } //! } //! //! // And now we can use it! //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { //! # //! let mut counter = Counter::new(); //! //! let x = counter.next().await.unwrap(); //! println!("{}", x); //! //! let x = counter.next().await.unwrap(); //! println!("{}", x); //! //! let x = counter.next().await.unwrap(); //! println!("{}", x); //! //! let x = counter.next().await.unwrap(); //! println!("{}", x); //! //! let x = counter.next().await.unwrap(); //! println!("{}", x); //! # //! # Ok(()) }) } //! ``` //! //! This will print `1` through `5`, each on their own line. //! //! Calling `next().await` this way gets repetitive. Rust has a construct which //! can call `next()` on your stream, until it reaches `None`. Let's go over //! that next. //! //! # while let Loops and IntoStream //! //! Rust's `while let` loop syntax is an idiomatic way to iterate over streams. Here's a basic //! example of `while let`: //! //! ``` //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { //! # //! # use async_std::prelude::*; //! # use async_std::stream; //! let mut values = stream::repeat(1u8).take(5); //! //! while let Some(x) = values.next().await { //! println!("{}", x); //! } //! # //! # Ok(()) }) } //! ``` //! //! This will print the numbers one through five, each on their own line. But //! you'll notice something here: we never called anything on our vector to //! produce a stream. What gives? //! //! There's a trait in the standard library for converting something into an //! stream: [`IntoStream`]. This trait has one method, [`into_stream`], //! which converts the thing implementing [`IntoStream`] into a stream. //! //! Unlike `std::iter::IntoIterator`, `IntoStream` does not have compiler //! support yet. This means that automatic conversions like with `for` loops //! doesn't occur yet, and `into_stream` will always have to be called manually. //! //! [`IntoStream`]: trait.IntoStream.html //! [`into_stream`]: trait.IntoStream.html#tymethod.into_stream //! //! # Adapters //! //! Functions which take an [`Stream`] and return another [`Stream`] are //! often called 'stream adapters', as they are a form of the 'adapter //! pattern'. //! //! Common stream adapters include [`map`], [`take`], and [`filter`]. //! For more, see their documentation. //! //! [`map`]: trait.Stream.html#method.map //! [`take`]: trait.Stream.html#method.take //! [`filter`]: trait.Stream.html#method.filter //! //! # Laziness //! //! Streams (and stream [adapters](#adapters)) are *lazy*. This means that //! just creating a stream doesn't _do_ a whole lot. Nothing really happens //! until you call [`next`]. This is sometimes a source of confusion when //! creating a stream solely for its side effects. For example, the [`map`] //! method calls a closure on each element it iterates over: //! //! ``` //! # #![allow(unused_must_use)] //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { //! # //! # use async_std::prelude::*; //! # use async_std::stream; //! let v = stream::repeat(1u8).take(5); //! v.map(|x| println!("{}", x)); //! # //! # Ok(()) }) } //! ``` //! //! This will not print any values, as we only created a stream, rather than //! using it. The compiler will warn us about this kind of behavior: //! //! ```text //! warning: unused result that must be used: streams are lazy and //! do nothing unless consumed //! ``` //! //! The idiomatic way to write a [`map`] for its side effects is to use a //! `while let` loop instead: //! //! ``` //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { //! # //! # use async_std::prelude::*; //! # use async_std::stream; //! let mut v = stream::repeat(1u8).take(5); //! //! while let Some(x) = &v.next().await { //! println!("{}", x); //! } //! # //! # Ok(()) }) } //! ``` //! //! [`map`]: trait.Stream.html#method.map //! //! The two most common ways to evaluate a stream are to use a `while let` loop //! like this, or using the [`collect`] method to produce a new collection. //! //! [`collect`]: trait.Stream.html#method.collect //! //! # Infinity //! //! Streams do not have to be finite. As an example, a repeat stream is //! an infinite stream: //! //! ``` //! # use async_std::stream; //! let numbers = stream::repeat(1u8); //! ``` //! //! It is common to use the [`take`] stream adapter to turn an infinite //! stream into a finite one: //! //! ``` //! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { //! # //! # use async_std::prelude::*; //! # use async_std::stream; //! let numbers = stream::repeat(1u8); //! let mut five_numbers = numbers.take(5); //! //! while let Some(number) = five_numbers.next().await { //! println!("{}", number); //! } //! # //! # Ok(()) }) } //! ``` //! //! This will print the numbers `0` through `4`, each on their own line. //! //! Bear in mind that methods on infinite streams, even those for which a //! result can be determined mathematically in finite time, may not terminate. //! Specifically, methods such as [`min`], which in the general case require //! traversing every element in the stream, are likely not to return //! successfully for any infinite streams. //! //! ```ignore //! let ones = async_std::stream::repeat(1); //! let least = ones.min().await.unwrap(); // Oh no! An infinite loop! //! // `ones.min()` causes an infinite loop, so we won't reach this point! //! println!("The smallest number one is {}.", least); //! ``` //! //! [`std::iter`]: https://doc.rust-lang.org/std/iter/index.html //! [`take`]: trait.Stream.html#method.take //! [`min`]: trait.Stream.html#method.min pub use empty::{empty, Empty}; pub use from_fn::{from_fn, FromFn}; pub use from_iter::{from_iter, FromIter}; pub use once::{once, Once}; pub use repeat::{repeat, Repeat}; pub use repeat_with::{repeat_with, RepeatWith}; pub use stream::*; pub(crate) mod stream; mod empty; mod from_fn; mod from_iter; mod once; mod repeat; mod repeat_with; cfg_unstable! { mod double_ended_stream; mod exact_size_stream; mod extend; mod from_stream; mod fused_stream; mod interval; mod into_stream; mod pending; mod product; mod successors; mod sum; pub use double_ended_stream::DoubleEndedStream; pub use exact_size_stream::ExactSizeStream; pub use extend::{extend, Extend}; pub use from_stream::FromStream; pub use fused_stream::FusedStream; pub use interval::{interval, Interval}; pub use into_stream::IntoStream; pub use pending::{pending, Pending}; pub use product::Product; pub use stream::Merge; pub use successors::{successors, Successors}; pub use sum::Sum; }