Skip to main content

dfir_pipes/pull/
map.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 transforms each item with a closure.
9    #[must_use = "`Pull`s do nothing unless polled"]
10    #[derive(Clone, Debug)]
11    pub struct Map<Prev, Func> {
12        #[pin]
13        prev: Prev,
14        func: Func,
15    }
16}
17
18impl<Prev, Func> Map<Prev, Func>
19where
20    Self: Pull,
21{
22    pub(crate) const fn new(prev: Prev, func: Func) -> Self {
23        Self { prev, func }
24    }
25}
26
27impl<Prev, Func, Item> Pull for Map<Prev, Func>
28where
29    Prev: Pull,
30    Func: FnMut(Prev::Item) -> Item,
31{
32    type Ctx<'ctx> = Prev::Ctx<'ctx>;
33
34    type Item = Item;
35    type Meta = Prev::Meta;
36    type CanPend = Prev::CanPend;
37    type CanEnd = Prev::CanEnd;
38
39    fn pull(
40        self: Pin<&mut Self>,
41        ctx: &mut Self::Ctx<'_>,
42    ) -> PullStep<Self::Item, Self::Meta, Self::CanPend, Self::CanEnd> {
43        let this = self.project();
44        match this.prev.pull(ctx) {
45            PullStep::Ready(item, meta) => PullStep::Ready((this.func)(item), meta),
46            PullStep::Pending(can_pend) => PullStep::Pending(can_pend),
47            PullStep::Ended(can_finish) => PullStep::Ended(can_finish),
48        }
49    }
50
51    fn size_hint(&self) -> (usize, Option<usize>) {
52        self.prev.size_hint()
53    }
54}
55
56impl<Prev, Func, Item> FusedPull for Map<Prev, Func>
57where
58    Prev: FusedPull,
59    Func: FnMut(Prev::Item) -> Item,
60{
61}