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
use std::{
mem,
pin::Pin,
task::{Context, Poll},
};
use futures_core::ready;
use pin_project_lite::pin_project;
use super::ActorStream;
use crate::actor::Actor;
use crate::fut::future::ActorFuture;
pin_project! {
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Collect<S, C> {
#[pin]
stream: S,
collection: C,
}
}
impl<S, C> Collect<S, C>
where
C: Default,
{
pub(super) fn new(stream: S) -> Self {
Self {
stream,
collection: Default::default(),
}
}
}
impl<S, A, C> ActorFuture<A> for Collect<S, C>
where
S: ActorStream<A>,
A: Actor,
C: Default + Extend<S::Item>,
{
type Output = C;
fn poll(
mut self: Pin<&mut Self>,
act: &mut A,
ctx: &mut A::Context,
task: &mut Context<'_>,
) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
match ready!(this.stream.as_mut().poll_next(act, ctx, task)) {
Some(e) => this.collection.extend(Some(e)),
None => return Poll::Ready(mem::take(this.collection)),
}
}
}
}