/rust/registry/src/index.crates.io-6f17d22bba15001f/futures-util-0.3.28/src/stream/repeat.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::assert_stream; |
2 | | use core::pin::Pin; |
3 | | use futures_core::stream::{FusedStream, Stream}; |
4 | | use futures_core::task::{Context, Poll}; |
5 | | |
6 | | /// Stream for the [`repeat`] function. |
7 | | #[derive(Debug, Clone)] |
8 | | #[must_use = "streams do nothing unless polled"] |
9 | | pub struct Repeat<T> { |
10 | | item: T, |
11 | | } |
12 | | |
13 | | /// Create a stream which produces the same item repeatedly. |
14 | | /// |
15 | | /// The stream never terminates. Note that you likely want to avoid |
16 | | /// usage of `collect` or such on the returned stream as it will exhaust |
17 | | /// available memory as it tries to just fill up all RAM. |
18 | | /// |
19 | | /// ``` |
20 | | /// # futures::executor::block_on(async { |
21 | | /// use futures::stream::{self, StreamExt}; |
22 | | /// |
23 | | /// let stream = stream::repeat(9); |
24 | | /// assert_eq!(vec![9, 9, 9], stream.take(3).collect::<Vec<i32>>().await); |
25 | | /// # }); |
26 | | /// ``` |
27 | 0 | pub fn repeat<T>(item: T) -> Repeat<T> |
28 | 0 | where |
29 | 0 | T: Clone, |
30 | 0 | { |
31 | 0 | assert_stream::<T, _>(Repeat { item }) |
32 | 0 | } |
33 | | |
34 | | impl<T> Unpin for Repeat<T> {} |
35 | | |
36 | | impl<T> Stream for Repeat<T> |
37 | | where |
38 | | T: Clone, |
39 | | { |
40 | | type Item = T; |
41 | | |
42 | 0 | fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
43 | 0 | Poll::Ready(Some(self.item.clone())) |
44 | 0 | } |
45 | | |
46 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
47 | 0 | (usize::max_value(), None) |
48 | 0 | } |
49 | | } |
50 | | |
51 | | impl<T> FusedStream for Repeat<T> |
52 | | where |
53 | | T: Clone, |
54 | | { |
55 | 0 | fn is_terminated(&self) -> bool { |
56 | 0 | false |
57 | 0 | } |
58 | | } |