| 1 | use crate::stream::Stream; |
| 2 | use crate::task::{Context, Poll}; |
| 3 | use pin_project_lite::pin_project; |
| 4 | use core::pin::Pin; |
| 5 | |
| 6 | pin_project! { |
| 7 | /// A stream that clones the elements of an underlying stream. |
| 8 | #[derive (Debug)] |
| 9 | pub struct Cloned<S> { |
| 10 | #[pin] |
| 11 | stream: S, |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | impl<S> Cloned<S> { |
| 16 | pub(super) fn new(stream: S) -> Self { |
| 17 | Self { stream } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl<'a, S, T: 'a> Stream for Cloned<S> |
| 22 | where |
| 23 | S: Stream<Item = &'a T>, |
| 24 | T: Clone, |
| 25 | { |
| 26 | type Item = T; |
| 27 | |
| 28 | fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 29 | let this: Projection<'_, S> = self.project(); |
| 30 | let next: Option<&'a T> = futures_core::ready!(this.stream.poll_next(cx)); |
| 31 | Poll::Ready(next.cloned()) |
| 32 | } |
| 33 | } |
| 34 | |