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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use async_std::prelude::*;
use async_std::sync::{self, Receiver, Sender};
use async_std::task::{self, Context, Poll};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use crate::ParallelStream;
pin_project_lite::pin_project! {
#[derive(Debug)]
pub struct ForEach {
#[pin]
receiver: Receiver<()>,
exhausted: Arc<AtomicBool>,
ref_count: Arc<AtomicU64>,
}
}
impl ForEach {
pub fn new<S, F, Fut>(mut input: S, mut f: F) -> Self
where
S: ParallelStream,
F: FnMut(S::Item) -> Fut + Send + Sync + Copy + 'static,
Fut: Future<Output = ()> + Send,
{
let exhausted = Arc::new(AtomicBool::new(false));
let ref_count = Arc::new(AtomicU64::new(0));
let (sender, receiver): (Sender<()>, Receiver<()>) = sync::channel(1);
let this = Self {
receiver,
exhausted: exhausted.clone(),
ref_count: ref_count.clone(),
};
task::spawn(async move {
while let Some(item) = input.next().await {
let sender = sender.clone();
let exhausted = exhausted.clone();
let ref_count = ref_count.clone();
ref_count.fetch_add(1, Ordering::SeqCst);
task::spawn(async move {
f(item).await;
ref_count.fetch_sub(1, Ordering::SeqCst);
if exhausted.load(Ordering::SeqCst) && ref_count.load(Ordering::SeqCst) == 0 {
sender.send(()).await;
}
});
}
exhausted.store(true, Ordering::SeqCst);
});
this
}
}
impl Future for ForEach {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
task::ready!(this.receiver.poll_next(cx));
Poll::Ready(())
}
}
#[async_std::test]
async fn smoke() {
let s = async_std::stream::repeat(5usize);
crate::from_stream(s)
.take(3)
.for_each(|n| async move {
dbg!(n);
})
.await;
}