| 1 | use core::pin::Pin; |
| 2 | use core::task::{Context, Poll}; |
| 3 | |
| 4 | use pin_project_lite::pin_project; |
| 5 | |
| 6 | use crate::stream::Stream; |
| 7 | |
| 8 | pin_project! { |
| 9 | /// A stream to skip first n elements of another stream. |
| 10 | /// |
| 11 | /// This `struct` is created by the [`skip`] method on [`Stream`]. See its |
| 12 | /// documentation for more. |
| 13 | /// |
| 14 | /// [`skip`]: trait.Stream.html#method.skip |
| 15 | /// [`Stream`]: trait.Stream.html |
| 16 | #[derive (Debug)] |
| 17 | pub struct Skip<S> { |
| 18 | #[pin] |
| 19 | stream: S, |
| 20 | n: usize, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl<S> Skip<S> { |
| 25 | pub(crate) fn new(stream: S, n: usize) -> Self { |
| 26 | Self { stream, n } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl<S> Stream for Skip<S> |
| 31 | where |
| 32 | S: Stream, |
| 33 | { |
| 34 | type Item = S::Item; |
| 35 | |
| 36 | fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 37 | let mut this: Projection<'_, S> = self.project(); |
| 38 | loop { |
| 39 | let next: Option<::Item> = futures_core::ready!(this.stream.as_mut().poll_next(cx)); |
| 40 | |
| 41 | match next { |
| 42 | Some(v: ::Item) => match *this.n { |
| 43 | 0 => return Poll::Ready(Some(v)), |
| 44 | _ => *this.n -= 1, |
| 45 | }, |
| 46 | None => return Poll::Ready(None), |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |