dfir_rs/compiled/pull/
for_each.rs

1use std::pin::Pin;
2use std::task::{Context, Poll, ready};
3
4use futures::future::FusedFuture;
5use futures::stream::{FusedStream, Stream};
6use pin_project_lite::pin_project;
7
8pin_project! {
9    /// A future which consumes a stream by feeding a sync function `Func` with all items.
10    pub struct ForEach<St, Func> {
11        #[pin]
12        pub(crate) stream: St,
13        pub(crate) f: Func,
14    }
15}
16
17impl<St, Func> ForEach<St, Func>
18where
19    St: Stream,
20    Func: FnMut(St::Item),
21{
22    /// Create a new ForEach future.
23    pub fn new(stream: St, f: Func) -> Self {
24        Self { stream, f }
25    }
26}
27
28impl<St, Func> Future for ForEach<St, Func>
29where
30    St: Stream,
31    Func: FnMut(St::Item),
32{
33    type Output = ();
34
35    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36        let mut this = self.project();
37        while let Some(item) = ready!(this.stream.as_mut().poll_next(cx)) {
38            let () = (this.f)(item);
39        }
40        Poll::Ready(())
41    }
42}
43
44impl<St, Func> FusedFuture for ForEach<St, Func>
45where
46    St: FusedStream,
47    Func: FnMut(St::Item),
48{
49    fn is_terminated(&self) -> bool {
50        self.stream.is_terminated()
51    }
52}