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
use core::pin::Pin;
use core::task::{Context, Poll};
use crate::{from_stream, FromStream, IntoParallelStream, ParallelStream};
use async_std::stream::{from_iter, FromIter};
use std::vec;
pin_project_lite::pin_project! {
#[derive(Debug)]
pub struct IntoParStream<T> {
#[pin]
stream: FromStream<FromIter<vec::IntoIter<T>>>,
}
}
impl<T: Send + Sync + 'static> ParallelStream for IntoParStream<T> {
type Item = T;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
this.stream.poll_next(cx)
}
}
impl<T: Send + Sync + 'static> IntoParallelStream for Vec<T> {
type Item = T;
type IntoParStream = IntoParStream<T>;
#[inline]
fn into_par_stream(self) -> Self::IntoParStream {
IntoParStream {
stream: from_stream(from_iter(self)),
}
}
}
#[async_std::test]
async fn smoke() {
use crate::IntoParallelStream;
let v = vec![1, 2, 3, 4];
let mut stream = v.into_par_stream().map(|n| async move { n * n });
let mut out = vec![];
while let Some(n) = stream.next().await {
out.push(n);
}
out.sort();
assert_eq!(out, vec![1usize, 4, 9, 16]);
}