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