/rust/registry/src/index.crates.io-6f17d22bba15001f/futures-util-0.3.31/src/stream/iter.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::assert_stream; |
2 | | use core::pin::Pin; |
3 | | use futures_core::stream::Stream; |
4 | | use futures_core::task::{Context, Poll}; |
5 | | |
6 | | /// Stream for the [`iter`] function. |
7 | | #[derive(Debug, Clone)] |
8 | | #[must_use = "streams do nothing unless polled"] |
9 | | pub struct Iter<I> { |
10 | | iter: I, |
11 | | } |
12 | | |
13 | | impl<I> Iter<I> { |
14 | | /// Acquires a reference to the underlying iterator that this stream is pulling from. |
15 | 0 | pub fn get_ref(&self) -> &I { |
16 | 0 | &self.iter |
17 | 0 | } |
18 | | |
19 | | /// Acquires a mutable reference to the underlying iterator that this stream is pulling from. |
20 | 0 | pub fn get_mut(&mut self) -> &mut I { |
21 | 0 | &mut self.iter |
22 | 0 | } |
23 | | |
24 | | /// Consumes this stream, returning the underlying iterator. |
25 | 0 | pub fn into_inner(self) -> I { |
26 | 0 | self.iter |
27 | 0 | } |
28 | | } |
29 | | |
30 | | impl<I> Unpin for Iter<I> {} |
31 | | |
32 | | /// Converts an `Iterator` into a `Stream` which is always ready |
33 | | /// to yield the next value. |
34 | | /// |
35 | | /// Iterators in Rust don't express the ability to block, so this adapter |
36 | | /// simply always calls `iter.next()` and returns that. |
37 | | /// |
38 | | /// ``` |
39 | | /// # futures::executor::block_on(async { |
40 | | /// use futures::stream::{self, StreamExt}; |
41 | | /// |
42 | | /// let stream = stream::iter(vec![17, 19]); |
43 | | /// assert_eq!(vec![17, 19], stream.collect::<Vec<i32>>().await); |
44 | | /// # }); |
45 | | /// ``` |
46 | 0 | pub fn iter<I>(i: I) -> Iter<I::IntoIter> |
47 | 0 | where |
48 | 0 | I: IntoIterator, |
49 | 0 | { |
50 | 0 | assert_stream::<I::Item, _>(Iter { iter: i.into_iter() }) |
51 | 0 | } |
52 | | |
53 | | impl<I> Stream for Iter<I> |
54 | | where |
55 | | I: Iterator, |
56 | | { |
57 | | type Item = I::Item; |
58 | | |
59 | 0 | fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<I::Item>> { |
60 | 0 | Poll::Ready(self.iter.next()) |
61 | 0 | } |
62 | | |
63 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
64 | 0 | self.iter.size_hint() |
65 | 0 | } |
66 | | } |