Skip to content

Commit e486cac

Browse files
authored
Point to AsyncIterator in std instead of futures
1 parent 9a22035 commit e486cac

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

posts/inside-rust/2022-11-17-async-fn-in-trait-nightly.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,10 @@ Besides, users [expect][alan-async-traits] to be able to write `async fn` in tra
106106

107107
Traits that need to work with zero overhead or in no_std contexts have another option: they can take the concept of polling from the [`Future` trait](https://doc.rust-lang.org/stable/std/future/trait.Future.html) and build it directly into their interface. The `Future::poll` method returns `Poll::Ready(Output)` if the future is complete and `Poll::Pending` if the future is waiting on some other event.
108108

109-
You can see this pattern, for example, in the Stream[^stream-futures] trait. The signature of `Stream::poll_next` is a cross between `Future::poll` and `Iterator::next`.
110-
111-
[^stream-futures]: The [`Stream` trait](https://docs.rs/futures/latest/futures/stream/trait.Stream.html) is part of the `futures` crate, not the standard library.
109+
You can see this pattern, for example, in the current version of the unstable [AsyncIterator](https://doc.rust-lang.org/stable/std/async_iter/trait.AsyncIterator.html) trait. The signature of `AsyncIterator::poll_next` is a cross between `Future::poll` and `Iterator::next`.
112110

113111
```rust
114-
pub trait Stream {
112+
pub trait AsyncIterator {
115113
type Item;
116114

117115
fn poll_next(
@@ -143,7 +141,7 @@ impl Database for MyDb {
143141
}
144142
```
145143

146-
One thing this will allow us to do is standardize new traits we've been waiting on this feature for. For example, the `Stream` trait from above is significantly more complicated than its analogue, `Iterator`. With the new support, we can simply write this instead:
144+
One thing this will allow us to do is standardize new traits we've been waiting on this feature for. For example, the `AsyncIterator` trait from above is significantly more complicated than its analogue, `Iterator`. With the new support, we can simply write this instead:
147145

148146
```rust
149147
#![feature(async_fn_in_trait)]
@@ -154,7 +152,7 @@ trait AsyncIterator {
154152
}
155153
```
156154

157-
There's a decent chance that exactly this trait will end up in the standard library! For now though, you can use the one in the [`async_iterator` crate](https://docs.rs/async-iterator/latest/async_iterator/) and write generic code with it, just like you would normally.
155+
There's a decent chance that the trait in the standard library will end up exactly like this! For now, you can also use the one in the [`async_iterator` crate](https://docs.rs/async-iterator/latest/async_iterator/) and write generic code with it, just like you would normally.
158156

159157
```rust
160158
async fn print_all<I: AsyncIterator>(mut count: I)

0 commit comments

Comments
 (0)