actix/fut/stream/
skip_while.rs1use 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 #[derive(Debug)]
17 #[must_use = "streams do nothing unless polled"]
18 pub struct SkipWhile<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_skipping: bool,
26 }
27}
28
29pub(super) fn new<S, A, F, Fut>(stream: S, f: F) -> SkipWhile<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 SkipWhile {
37 stream,
38 f,
39 pending_fut: None,
40 pending_item: None,
41 done_skipping: false,
42 }
43}
44
45impl<S, A, F, Fut> ActorStream<A> for SkipWhile<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 let mut this = self.project();
61
62 if *this.done_skipping {
63 return this.stream.poll_next(act, ctx, task);
64 }
65
66 Poll::Ready(loop {
67 if let Some(fut) = this.pending_fut.as_mut().as_pin_mut() {
68 let skipped = ready!(fut.poll(act, ctx, task));
69 let item = this.pending_item.take();
70 this.pending_fut.set(None);
71 if !skipped {
72 *this.done_skipping = true;
73 break item;
74 }
75 } else if let Some(item) = ready!(this.stream.as_mut().poll_next(act, ctx, task)) {
76 this.pending_fut.set(Some((this.f)(&item, act, ctx)));
77 *this.pending_item = Some(item);
78 } else {
79 break None;
80 }
81 })
82 }
83}