actix/fut/stream/
take_while.rs

1use std::{
2    pin::Pin,
3    task::{self, Poll},
4};
5
6use futures_core::ready;
7use pin_project_lite::pin_project;
8
9use crate::{
10    actor::Actor,
11    fut::{ActorFuture, ActorStream},
12};
13
14pin_project! {
15    /// Stream for the [`take_while`](super::ActorStreamExt::take_while) method.
16    #[must_use = "streams do nothing unless polled"]
17    #[derive(Debug)]
18    pub struct TakeWhile<S, I, F, Fut> {
19        #[pin]
20        stream: S,
21        f: F,
22        #[pin]
23        pending_fut: Option<Fut>,
24        pending_item: Option<I>,
25        done_taking: bool,
26    }
27}
28
29pub(super) fn new<S, A, F, Fut>(stream: S, f: F) -> TakeWhile<S, S::Item, F, Fut>
30where
31    S: ActorStream<A>,
32    A: Actor,
33    F: FnMut(&S::Item, &mut A, &mut A::Context) -> Fut,
34    Fut: ActorFuture<A, Output = bool>,
35{
36    TakeWhile {
37        stream,
38        f,
39        pending_fut: None,
40        pending_item: None,
41        done_taking: false,
42    }
43}
44
45impl<S, A, F, Fut> ActorStream<A> for TakeWhile<S, S::Item, F, Fut>
46where
47    S: ActorStream<A>,
48    A: Actor,
49    F: FnMut(&S::Item, &mut A, &mut A::Context) -> Fut,
50    Fut: ActorFuture<A, Output = bool>,
51{
52    type Item = S::Item;
53
54    fn poll_next(
55        self: Pin<&mut Self>,
56        act: &mut A,
57        ctx: &mut A::Context,
58        task: &mut task::Context<'_>,
59    ) -> Poll<Option<Self::Item>> {
60        if self.done_taking {
61            return Poll::Ready(None);
62        }
63
64        let mut this = self.project();
65
66        Poll::Ready(loop {
67            if let Some(fut) = this.pending_fut.as_mut().as_pin_mut() {
68                let take = ready!(fut.poll(act, ctx, task));
69                let item = this.pending_item.take();
70                this.pending_fut.set(None);
71                if take {
72                    break item;
73                } else {
74                    *this.done_taking = true;
75                    break None;
76                }
77            } else if let Some(item) = ready!(this.stream.as_mut().poll_next(act, ctx, task)) {
78                this.pending_fut.set(Some((this.f)(&item, act, ctx)));
79                *this.pending_item = Some(item);
80            } else {
81                break None;
82            }
83        })
84    }
85}