| 1 | use core::pin::Pin; |
| 2 | |
| 3 | use crate::stream::Stream; |
| 4 | use 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 | /// ``` |
| 23 | pub fn repeat<T>(item: T) -> Repeat<T> |
| 24 | where |
| 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)] |
| 37 | pub struct Repeat<T> { |
| 38 | item: T, |
| 39 | } |
| 40 | |
| 41 | impl<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 | } |
| 48 | |