1use crate::pin::Pin;
2
3use crate::async_iter::AsyncIterator;
4use crate::task::{Context, Poll};
5
6/// An async iterator that was created from iterator.
7///
8/// This async iterator is created by the [`from_iter`] function.
9/// See it documentation for more.
10///
11/// [`from_iter`]: fn.from_iter.html
12#[unstable(feature = "async_iter_from_iter", issue = "81798")]
13#[derive(Clone, Debug)]
14pub struct FromIter<I> {
15 iter: I,
16}
17
18#[unstable(feature = "async_iter_from_iter", issue = "81798")]
19impl<I> Unpin for FromIter<I> {}
20
21/// Converts an iterator into an async iterator.
22#[unstable(feature = "async_iter_from_iter", issue = "81798")]
23pub fn from_iter<I: IntoIterator>(iter: I) -> FromIter<I::IntoIter> {
24 FromIter { iter: iter.into_iter() }
25}
26
27#[unstable(feature = "async_iter_from_iter", issue = "81798")]
28impl<I: Iterator> AsyncIterator for FromIter<I> {
29 type Item = I::Item;
30
31 fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
32 Poll::Ready(self.iter.next())
33 }
34
35 fn size_hint(&self) -> (usize, Option<usize>) {
36 self.iter.size_hint()
37 }
38}
39