1 use crate::stream::Stream;
2 
3 use core::pin::Pin;
4 use core::task::{Context, Poll};
5 
6 /// Stream for the [`iter`](fn@iter) function.
7 #[derive(Debug)]
8 #[must_use = "streams do nothing unless polled"]
9 pub struct Iter<I> {
10     iter: I,
11 }
12 
13 impl<I> Unpin for Iter<I> {}
14 
15 /// Converts an `Iterator` into a `Stream` which is always ready
16 /// to yield the next value.
17 ///
18 /// Iterators in Rust don't express the ability to block, so this adapter
19 /// simply always calls `iter.next()` and returns that.
20 ///
21 /// ```
22 /// # async fn dox() {
23 /// use tokio::stream::{self, StreamExt};
24 ///
25 /// let mut stream = stream::iter(vec![17, 19]);
26 ///
27 /// assert_eq!(stream.next().await, Some(17));
28 /// assert_eq!(stream.next().await, Some(19));
29 /// assert_eq!(stream.next().await, None);
30 /// # }
31 /// ```
iter<I>(i: I) -> Iter<I::IntoIter> where I: IntoIterator,32 pub fn iter<I>(i: I) -> Iter<I::IntoIter>
33 where
34     I: IntoIterator,
35 {
36     Iter {
37         iter: i.into_iter(),
38     }
39 }
40 
41 impl<I> Stream for Iter<I>
42 where
43     I: Iterator,
44 {
45     type Item = I::Item;
46 
poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<I::Item>>47     fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<I::Item>> {
48         let coop = ready!(crate::coop::poll_proceed(cx));
49         coop.made_progress();
50         Poll::Ready(self.iter.next())
51     }
52 
size_hint(&self) -> (usize, Option<usize>)53     fn size_hint(&self) -> (usize, Option<usize>) {
54         self.iter.size_hint()
55     }
56 }
57