Skip to content

Commit b6710f5

Browse files
Rollup merge of rust-lang#119222 - eholk:into-async-iterator, r=compiler-errors,dtolnay
Add `IntoAsyncIterator` This introduces the `IntoAsyncIterator` trait and uses it in the desugaring of the unstable `for await` loop syntax. This is mostly added for symmetry with `Iterator` and `IntoIterator`. r? `@compiler-errors` cc `@rust-lang/libs-api,` `@rust-lang/wg-async`
2 parents 42746f7 + 6a76e0c commit b6710f5

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

core/src/async_iter/async_iter.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,26 @@ impl<T> Poll<Option<T>> {
135135
#[lang = "AsyncGenFinished"]
136136
pub const FINISHED: Self = Poll::Ready(None);
137137
}
138+
139+
/// Convert something into an async iterator
140+
#[unstable(feature = "async_iterator", issue = "79024")]
141+
pub trait IntoAsyncIterator {
142+
/// The type of the item yielded by the iterator
143+
type Item;
144+
/// The type of the resulting iterator
145+
type IntoAsyncIter: AsyncIterator<Item = Self::Item>;
146+
147+
/// Converts `self` into an async iterator
148+
#[cfg_attr(not(bootstrap), lang = "into_async_iter_into_iter")]
149+
fn into_async_iter(self) -> Self::IntoAsyncIter;
150+
}
151+
152+
#[unstable(feature = "async_iterator", issue = "79024")]
153+
impl<I: AsyncIterator> IntoAsyncIterator for I {
154+
type Item = I::Item;
155+
type IntoAsyncIter = I;
156+
157+
fn into_async_iter(self) -> Self::IntoAsyncIter {
158+
self
159+
}
160+
}

core/src/async_iter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,5 @@
124124
mod async_iter;
125125
mod from_iter;
126126

127-
pub use async_iter::AsyncIterator;
127+
pub use async_iter::{AsyncIterator, IntoAsyncIterator};
128128
pub use from_iter::{from_iter, FromIter};

core/tests/async_iter/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use core::async_iter::{self, AsyncIterator, IntoAsyncIterator};
2+
use core::pin::pin;
3+
use core::task::Poll;
4+
5+
#[test]
6+
fn into_async_iter() {
7+
let async_iter = async_iter::from_iter(0..3);
8+
let mut async_iter = pin!(async_iter.into_async_iter());
9+
10+
let waker = core::task::Waker::noop();
11+
let mut cx = &mut core::task::Context::from_waker(&waker);
12+
13+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(0)));
14+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));
15+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(Some(2)));
16+
assert_eq!(async_iter.as_mut().poll_next(&mut cx), Poll::Ready(None));
17+
}

core/tests/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#![feature(array_windows)]
55
#![feature(ascii_char)]
66
#![feature(ascii_char_variants)]
7+
#![feature(async_iter_from_iter)]
8+
#![feature(async_iterator)]
79
#![feature(bigint_helper_methods)]
810
#![feature(cell_update)]
911
#![feature(const_align_offset)]
@@ -55,6 +57,7 @@
5557
#![feature(maybe_uninit_write_slice)]
5658
#![feature(maybe_uninit_uninit_array_transpose)]
5759
#![feature(min_specialization)]
60+
#![feature(noop_waker)]
5861
#![feature(numfmt)]
5962
#![feature(num_midpoint)]
6063
#![feature(isqrt)]
@@ -125,6 +128,7 @@ mod any;
125128
mod array;
126129
mod ascii;
127130
mod asserting;
131+
mod async_iter;
128132
mod atomic;
129133
mod bool;
130134
mod cell;

0 commit comments

Comments
 (0)