1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::ProcessingError;
use futures::{try_ready, Async, Future, IntoFuture, Poll, Stream};
use std::mem;
#[must_use = "futures do nothing unless polled"]
pub struct StreamProcessor<S, P, I>
where
I: IntoFuture,
{
receiver: S,
processor: P,
state: State<I>,
}
impl<S, P, I> StreamProcessor<S, P, I>
where
I: IntoFuture,
{
pub fn new(receiver: S, processor: P) -> Self {
StreamProcessor {
receiver,
processor,
state: State::WaitingForItem,
}
}
}
enum State<I: IntoFuture> {
WaitingForItem,
RunningProcessor(I::Future),
}
impl<S, P, I> Future for StreamProcessor<S, P, I>
where
S: Stream,
P: FnMut(S::Item) -> I,
I: IntoFuture,
{
type Item = ();
type Error = ProcessingError<S::Error, I::Error>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
let next = match &mut self.state {
State::WaitingForItem => {
if let Some(item) =
try_ready!(self.receiver.poll().map_err(ProcessingError::ReceiverError))
{
let fut = (self.processor)(item);
State::RunningProcessor(fut.into_future())
} else {
return Ok(Async::Ready(()));
}
}
State::RunningProcessor(fut) => {
try_ready!(fut.poll().map_err(ProcessingError::FutureError));
State::WaitingForItem
}
};
mem::replace(&mut self.state, next);
}
}
}