Skip to main content

dfir_pipes/pull/
from_fn.rs

1//! [`FromFn`] - a `Pull` created from a closure.
2
3use core::pin::Pin;
4
5use crate::pull::{Pull, PullStep};
6use crate::{No, Toggle};
7
8/// A `Pull` implementation created from a closure.
9#[must_use = "`Pull`s do nothing unless polled"]
10#[derive(Clone, Debug)]
11pub struct FromFn<F, Item, Meta, CanEnd> {
12    func: F,
13    #[expect(clippy::type_complexity, reason = "phantom data")]
14    _marker: core::marker::PhantomData<fn() -> (Item, Meta, CanEnd)>,
15}
16
17impl<F, Item, Meta, CanEnd> FromFn<F, Item, Meta, CanEnd>
18where
19    Self: Pull,
20{
21    /// Create a new `FromFn` from the given closure.
22    pub(crate) fn new(func: F) -> Self {
23        Self {
24            func,
25            _marker: core::marker::PhantomData,
26        }
27    }
28}
29
30impl<F, Item, Meta, CanEnd> Unpin for FromFn<F, Item, Meta, CanEnd> {}
31
32impl<F, Item, Meta, CanEnd> Pull for FromFn<F, Item, Meta, CanEnd>
33where
34    F: FnMut() -> PullStep<Item, Meta, No, CanEnd>,
35    Meta: Copy,
36    CanEnd: Toggle,
37{
38    type Ctx<'ctx> = ();
39
40    type Item = Item;
41    type Meta = Meta;
42    type CanPend = No;
43    type CanEnd = CanEnd;
44
45    fn pull(
46        self: Pin<&mut Self>,
47        _ctx: &mut Self::Ctx<'_>,
48    ) -> PullStep<Self::Item, Self::Meta, Self::CanPend, Self::CanEnd> {
49        let this = self.get_mut();
50        (this.func)()
51    }
52
53    fn size_hint(&self) -> (usize, Option<usize>) {
54        // Depends on UDF.
55        (0, None)
56    }
57}