Skip to content

Commit 8e34391

Browse files
committed
Add IntoAsyncIterator
1 parent 208dd20 commit 8e34391

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

library/core/src/async_iter/async_iter.rs

+22
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,25 @@ impl<T> Poll<Option<T>> {
135135
#[cfg_attr(not(bootstrap), 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+
fn into_async_iter(self) -> Self::IntoAsyncIter;
149+
}
150+
151+
#[unstable(feature = "async_iterator", issue = "79024")]
152+
impl<I: AsyncIterator> IntoAsyncIterator for I {
153+
type Item = I::Item;
154+
type IntoAsyncIter = I;
155+
156+
fn into_async_iter(self) -> Self::IntoAsyncIter {
157+
self
158+
}
159+
}

library/core/src/async_iter/mod.rs

+1-1
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};

library/core/tests/async_iter/mod.rs

+17
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+
}

library/core/tests/lib.rs

+4
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)]
@@ -126,6 +129,7 @@ mod any;
126129
mod array;
127130
mod ascii;
128131
mod asserting;
132+
mod async_iter;
129133
mod atomic;
130134
mod bool;
131135
mod cell;

0 commit comments

Comments
 (0)