1use core::pin::Pin;
2use core::task::{Context, Poll};
3
4use pin_project_lite::pin_project;
5
6use crate::stream::Stream;
7
8pin_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
24impl<S> Skip<S> {
25 pub(crate) fn new(stream: S, n: usize) -> Self {
26 Self { stream, n }
27 }
28}
29
30impl<S> Stream for Skip<S>
31where
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