async_std/stream/
repeat.rs

1use core::pin::Pin;
2
3use crate::stream::Stream;
4use crate::task::{Context, Poll};
5
6/// Creates a stream that yields the same item repeatedly.
7///
8/// # Examples
9///
10/// ```
11/// # async_std::task::block_on(async {
12/// #
13/// use async_std::prelude::*;
14/// use async_std::stream;
15///
16/// let mut s = stream::repeat(7);
17///
18/// assert_eq!(s.next().await, Some(7));
19/// assert_eq!(s.next().await, Some(7));
20/// #
21/// # })
22/// ```
23pub fn repeat<T>(item: T) -> Repeat<T>
24where
25    T: Clone,
26{
27    Repeat { item }
28}
29
30/// A stream that yields the same item repeatedly.
31///
32/// This stream is created by the [`repeat`] function. See its
33/// documentation for more.
34///
35/// [`repeat`]: fn.repeat.html
36#[derive(Clone, Debug)]
37pub struct Repeat<T> {
38    item: T,
39}
40
41impl<T: Clone> Stream for Repeat<T> {
42    type Item = T;
43
44    fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
45        Poll::Ready(Some(self.item.clone()))
46    }
47}