1 use super::assert_stream;
2 use core::pin::Pin;
3 use futures_core::stream::{Stream, FusedStream};
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 /// ```
repeat<T>(item: T) -> Repeat<T> where T: Clone27 pub fn repeat<T>(item: T) -> Repeat<T>
28     where T: Clone
29 {
30     assert_stream::<T, _>(Repeat { item })
31 }
32 
33 impl<T> Unpin for Repeat<T> {}
34 
35 impl<T> Stream for Repeat<T>
36     where T: Clone
37 {
38     type Item = T;
39 
poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>>40     fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
41         Poll::Ready(Some(self.item.clone()))
42     }
43 
size_hint(&self) -> (usize, Option<usize>)44     fn size_hint(&self) -> (usize, Option<usize>) {
45         (usize::max_value(), None)
46     }
47 }
48 
49 impl<T> FusedStream for Repeat<T>
50     where T: Clone,
51 {
is_terminated(&self) -> bool52     fn is_terminated(&self) -> bool {
53         false
54     }
55 }
56