broker_tokio/stream/
iter.rs

1use crate::stream::Stream;
2
3use core::pin::Pin;
4use core::task::{Context, Poll};
5
6/// Stream for the [`iter`] function.
7#[derive(Debug)]
8#[must_use = "streams do nothing unless polled"]
9pub struct Iter<I> {
10    iter: I,
11}
12
13impl<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/// ```
32pub fn iter<I>(i: I) -> Iter<I::IntoIter>
33where
34    I: IntoIterator,
35{
36    Iter {
37        iter: i.into_iter(),
38    }
39}
40
41impl<I> Stream for Iter<I>
42where
43    I: Iterator,
44{
45    type Item = I::Item;
46
47    fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<I::Item>> {
48        Poll::Ready(self.iter.next())
49    }
50
51    fn size_hint(&self) -> (usize, Option<usize>) {
52        self.iter.size_hint()
53    }
54}