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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::{future::Future, marker::PhantomPinned, ops::DerefMut, pin::Pin};
use futures_task::{noop_waker, Context, Poll};
use pin_project::pin_project;
use self::{
chain::ChainStream, filter::FilterStream, find::FindStream, map::MapStream, merge::MergeStream,
once::OnceStream, repeat::RepeatStream,
};
mod chain;
mod errors;
mod filter;
mod find;
mod map;
mod merge;
mod once;
mod repeat;
#[cfg(feature = "logging")]
mod stream_log;
pub use errors::*;
#[must_use = "streams do nothing unless polled"]
pub trait Stream {
type Item;
fn poll_recv(self: Pin<&mut Self>, cx: &mut Context<'_>) -> PollRecv<Self::Item>;
fn recv(&mut self) -> RecvFuture<'_, Self>
where
Self: Unpin,
{
RecvFuture::new(self)
}
fn try_recv(&mut self) -> Result<Self::Item, TryRecvError>
where
Self: Unpin,
{
let pin = Pin::new(self);
let waker = noop_waker();
let mut context = Context::from_waker(&waker);
match pin.poll_recv(&mut context) {
PollRecv::Ready(value) => Ok(value),
PollRecv::Pending => Err(TryRecvError::Pending),
PollRecv::Closed => Err(TryRecvError::Rejected),
}
}
fn once(item: Self::Item) -> OnceStream<Self::Item> {
OnceStream::new(item)
}
fn repeat(item: Self::Item) -> RepeatStream<Self::Item>
where
Self::Item: Clone,
{
RepeatStream::new(item)
}
fn map<Map, Into>(self, map: Map) -> MapStream<Self, Map, Into>
where
Map: Fn(Self::Item) -> Into,
Self: Sized,
{
MapStream::new(self, map)
}
fn filter<Filter>(self, filter: Filter) -> FilterStream<Self, Filter>
where
Self: Sized + Unpin,
Filter: FnMut(&Self::Item) -> bool + Unpin,
{
FilterStream::new(self, filter)
}
fn merge<Other>(self, other: Other) -> MergeStream<Self, Other>
where
Other: Stream<Item = Self::Item>,
Self: Sized,
{
MergeStream::new(self, other)
}
fn chain<Other>(self, other: Other) -> ChainStream<Self, Other>
where
Other: Stream<Item = Self::Item>,
Self: Sized,
{
ChainStream::new(self, other)
}
fn find<Condition>(self, condition: Condition) -> FindStream<Self, Condition>
where
Self: Sized + Unpin,
Condition: Fn(&Self::Item) -> bool + Unpin,
{
FindStream::new(self, condition)
}
#[cfg(feature = "logging")]
fn log(self, level: log::Level) -> stream_log::StreamLog<Self>
where
Self: Sized,
Self::Item: std::fmt::Debug,
{
stream_log::StreamLog::new(self, level)
}
}
impl<S> Stream for &mut S
where
S: Stream + Unpin + ?Sized,
{
type Item = S::Item;
fn poll_recv(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> PollRecv<Self::Item> {
S::poll_recv(Pin::new(&mut **self), cx)
}
}
impl<P, S> Stream for Pin<P>
where
P: DerefMut<Target = S> + Unpin,
S: Stream + Unpin + ?Sized,
{
type Item = <S as Stream>::Item;
fn poll_recv(self: Pin<&mut Self>, cx: &mut Context<'_>) -> PollRecv<Self::Item> {
Pin::get_mut(self).as_mut().poll_recv(cx)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PollRecv<T> {
Ready(T),
Pending,
Closed,
}
#[pin_project]
#[must_use = "futures do nothing unless polled"]
pub struct RecvFuture<'s, S>
where
S: Stream + ?Sized,
{
recv: &'s mut S,
#[pin]
_pin: PhantomPinned,
}
impl<'s, S: Stream> RecvFuture<'s, S>
where
S: ?Sized,
{
pub fn new(recv: &'s mut S) -> RecvFuture<'s, S> {
Self {
recv,
_pin: PhantomPinned,
}
}
}
impl<'s, S> Future for RecvFuture<'s, S>
where
S: Stream + Unpin + ?Sized,
{
type Output = Option<S::Item>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
match Pin::new(this.recv).poll_recv(cx) {
PollRecv::Ready(v) => Poll::Ready(Some(v)),
PollRecv::Pending => Poll::Pending,
PollRecv::Closed => Poll::Ready(None),
}
}
}