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
use core::pin::Pin;
use async_std::stream::{IntoStream, Stream};
use async_std::task::{Context, Poll};
use pin_project_lite::pin_project;
use crate::ParallelStream;
pin_project! {
#[derive(Clone, Debug)]
pub struct FromStream<S> {
#[pin]
stream: S,
limit: Option<usize>,
}
}
pub fn from_stream<S: IntoStream>(stream: S) -> FromStream<S::IntoStream>
where
S: Send + Sync,
{
FromStream {
limit: None,
stream: stream.into_stream(),
}
}
impl<S: Stream + Send + Sync + Unpin + 'static> ParallelStream for FromStream<S>
where
S::Item: Send,
{
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
this.stream.poll_next(cx)
}
fn limit(mut self, limit: impl Into<Option<usize>>) -> Self {
self.limit = limit.into();
self
}
fn get_limit(&self) -> Option<usize> {
self.limit
}
}