futures_util/stream/repeat.rs
1use core::marker;
2
3use futures_core::{Stream, Async, Poll};
4use futures_core::task;
5
6/// Stream that produces the same element repeatedly.
7///
8/// This structure is created by the `stream::repeat` function.
9#[derive(Debug)]
10#[must_use = "streams do nothing unless polled"]
11pub struct Repeat<T, E>
12 where T: Clone
13{
14 item: T,
15 error: marker::PhantomData<E>,
16}
17
18/// Create a stream which produces the same item repeatedly.
19///
20/// Stream never produces an error or EOF. Note that you likely want to avoid
21/// usage of `collect` or such on the returned stream as it will exhaust
22/// available memory as it tries to just fill up all RAM.
23///
24/// ```rust
25/// # extern crate futures;
26/// # extern crate futures_executor;
27/// use futures::prelude::*;
28/// use futures::stream;
29/// use futures_executor::block_on;
30///
31/// # fn main() {
32/// let mut stream = stream::repeat::<_, bool>(10);
33/// assert_eq!(Ok(vec![10, 10, 10]), block_on(stream.take(3).collect()));
34/// # }
35/// ```
36pub fn repeat<T, E>(item: T) -> Repeat<T, E>
37 where T: Clone
38{
39 Repeat {
40 item: item,
41 error: marker::PhantomData,
42 }
43}
44
45impl<T, E> Stream for Repeat<T, E>
46 where T: Clone
47{
48 type Item = T;
49 type Error = E;
50
51 fn poll_next(&mut self, _: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
52 Ok(Async::Ready(Some(self.item.clone())))
53 }
54}