Skip to main content

dfir_pipes/pull/
skip.rs

1use core::pin::Pin;
2
3use pin_project_lite::pin_project;
4
5use crate::pull::{FusedPull, Pull, PullStep};
6
7pin_project! {
8    /// Pull combinator that skips the first `n` items.
9    #[must_use = "`Pull`s do nothing unless polled"]
10    #[derive(Clone, Debug, Default)]
11    pub struct Skip<Prev> {
12        #[pin]
13        prev: Prev,
14        remaining: usize,
15    }
16}
17
18impl<Prev> Skip<Prev>
19where
20    Self: Pull,
21{
22    pub(crate) const fn new(prev: Prev, n: usize) -> Self {
23        Self { prev, remaining: n }
24    }
25}
26
27impl<Prev> Pull for Skip<Prev>
28where
29    Prev: Pull,
30{
31    type Ctx<'ctx> = Prev::Ctx<'ctx>;
32
33    type Item = Prev::Item;
34    type Meta = Prev::Meta;
35    type CanPend = Prev::CanPend;
36    type CanEnd = Prev::CanEnd;
37
38    fn pull(
39        self: Pin<&mut Self>,
40        ctx: &mut Self::Ctx<'_>,
41    ) -> PullStep<Self::Item, Self::Meta, Self::CanPend, Self::CanEnd> {
42        let mut this = self.project();
43
44        loop {
45            return match this.prev.as_mut().pull(ctx) {
46                PullStep::Ready(item, meta) => {
47                    if *this.remaining > 0 {
48                        *this.remaining -= 1;
49                        continue;
50                    }
51                    PullStep::Ready(item, meta)
52                }
53                PullStep::Pending(can_pend) => PullStep::Pending(can_pend),
54                PullStep::Ended(can_end) => PullStep::Ended(can_end),
55            };
56        }
57    }
58
59    fn size_hint(&self) -> (usize, Option<usize>) {
60        let (lower, upper) = self.prev.size_hint();
61        let remaining = self.remaining;
62        (
63            lower.saturating_sub(remaining),
64            upper.map(|u| u.saturating_sub(remaining)),
65        )
66    }
67}
68
69impl<Prev> FusedPull for Skip<Prev> where Prev: FusedPull {}