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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::Operator;
use futures::task::{Context, Poll};
use futures::{ready, Stream};
use pin_project_lite::pin_project;
pin_project! {
#[derive(Debug, Clone, Copy)]
pub struct Operated<St, P> {
#[pin]
source: St,
op: P,
}
}
impl<St, P> Stream for Operated<St, P>
where
St: Stream,
P: Operator<St::Item>,
{
type Item = P::Output;
fn poll_next(
self: core::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
let mut this = self.project();
let res = ready!(this.source.as_mut().poll_next(cx));
Poll::Ready(res.map(|x| this.op.next(x)))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.source.size_hint()
}
}
#[cfg(feature = "async")]
pub mod async_operated {
use crate::async_operator::AsyncOperator;
use futures::task::{Context, Poll};
use futures::{future::Future, pin_mut, ready, Stream};
use pin_project_lite::pin_project;
pin_project! {
#[derive(Debug, Clone, Copy)]
pub struct Operated<St, P> {
#[pin]
pub(crate) source: St,
pub(crate) op: P,
}
}
impl<St, P> Stream for Operated<St, P>
where
St: Stream,
P: AsyncOperator<St::Item>,
{
type Item = Result<P::Output, P::Error>;
fn poll_next(
self: core::pin::Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
let mut this = self.project();
match ready!(this.source.as_mut().poll_next(cx)) {
Some(input) => match ready!(this.op.poll_ready(cx)) {
Ok(()) => {
let fut = this.op.next(input);
pin_mut!(fut);
let next = ready!(fut.poll(cx));
Poll::Ready(Some(next))
}
Err(err) => Poll::Ready(Some(Err(err))),
},
None => Poll::Ready(None),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.source.size_hint()
}
}
}
#[cfg(feature = "async")]
use crate::async_operator::AsyncOperator;
pub trait IndicatorStreamExt: Stream {
fn indicator<P>(self, op: P) -> Operated<Self, P>
where
Self: Sized,
P: Operator<Self::Item>,
{
Operated { source: self, op }
}
#[cfg(feature = "async")]
fn async_indicator<P>(self, op: P) -> async_operated::Operated<Self, P>
where
Self: Sized,
P: AsyncOperator<Self::Item>,
{
async_operated::Operated { source: self, op }
}
}
impl<St: Stream> IndicatorStreamExt for St {}