Skip to main content

dfir_pipes/pull/
iter.rs

1use core::iter::FusedIterator;
2use core::pin::Pin;
3
4use pin_project_lite::pin_project;
5
6use crate::pull::{FusedPull, Pull, PullStep};
7use crate::{No, Yes};
8
9pin_project! {
10    /// A pull that wraps an iterator.
11    #[must_use = "`Pull`s do nothing unless polled"]
12    #[derive(Clone, Debug, Default)]
13    pub struct Iter<I> {
14        iter: I,
15    }
16}
17
18impl<I> Iter<I> {
19    pub(crate) const fn new(iter: I) -> Self {
20        Self { iter }
21    }
22}
23
24impl<I> Pull for Iter<I>
25where
26    I: Iterator,
27{
28    type Ctx<'ctx> = ();
29
30    type Item = I::Item;
31    type Meta = ();
32    type CanPend = No;
33    type CanEnd = Yes;
34
35    fn pull(
36        self: Pin<&mut Self>,
37        _ctx: &mut Self::Ctx<'_>,
38    ) -> PullStep<Self::Item, Self::Meta, Self::CanPend, Self::CanEnd> {
39        let this = self.project();
40        match this.iter.next() {
41            Some(item) => PullStep::Ready(item, ()),
42            None => PullStep::Ended(Yes),
43        }
44    }
45
46    fn size_hint(&self) -> (usize, Option<usize>) {
47        self.iter.size_hint()
48    }
49}
50
51impl<I> FusedPull for Iter<I> where I: FusedIterator {}