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
210
211
212
213
214
215
216
217
218
219
#![allow(clippy::type_complexity)]

use derive_new::new;
use futures::{pin_mut, ready, stream, Future, Stream, StreamExt};
use pin_project::pin_project;
use std::{
	marker::PhantomData, ops::DerefMut, pin::Pin, task::{Context, Poll}
};

// Sink could take Item as an input parameter to accept for<'a> &'a T, but this might be fixed anyway? https://github.com/rust-lang/rust/issues/49601

pub trait Sink {
	type Item;

	fn poll_forward(
		self: Pin<&mut Self>, cx: &mut Context, stream: Pin<&mut impl Stream<Item = Self::Item>>,
	) -> Poll<()>;
}

impl<P> Sink for Pin<P>
where
	P: DerefMut + Unpin,
	P::Target: Sink,
{
	type Item = <P::Target as Sink>::Item;

	fn poll_forward(
		self: Pin<&mut Self>, cx: &mut Context, stream: Pin<&mut impl Stream<Item = Self::Item>>,
	) -> Poll<()> {
		self.get_mut().as_mut().poll_forward(cx, stream)
	}
}

impl<T: ?Sized> Sink for &mut T
where
	T: Sink + Unpin,
{
	type Item = T::Item;

	fn poll_forward(
		mut self: Pin<&mut Self>, cx: &mut Context,
		stream: Pin<&mut impl Stream<Item = Self::Item>>,
	) -> Poll<()> {
		Pin::new(&mut **self).poll_forward(cx, stream)
	}
}

#[pin_project]
#[derive(new)]
pub struct SinkMap<F, I, R, Item> {
	#[pin]
	i: I,
	f: F,
	marker: PhantomData<fn() -> (R, Item)>,
}
impl<F, I, R, Item> Sink for SinkMap<F, I, R, Item>
where
	F: FnMut(Item) -> R,
	I: Sink<Item = R>,
{
	type Item = Item;

	fn poll_forward(
		self: Pin<&mut Self>, cx: &mut Context, stream: Pin<&mut impl Stream<Item = Self::Item>>,
	) -> Poll<()> {
		let self_ = self.project();
		let (f, i) = (self_.f, self_.i);
		let stream = stream.map(f);
		pin_mut!(stream);
		i.poll_forward(cx, stream)
	}
}

#[pin_project(project = SinkFilterStateProj, project_replace = SinkFilterStateProjOwn)]
pub enum SinkFilterState<T, Fut> {
	Some(T, #[pin] Fut),
	None,
}
impl<T, Fut> SinkFilterState<T, Fut> {
	fn fut(self: Pin<&mut Self>) -> Option<Pin<&mut Fut>> {
		match self.project() {
			SinkFilterStateProj::Some(_, fut) => Some(fut),
			SinkFilterStateProj::None => None,
		}
	}
	fn take(self: Pin<&mut Self>) -> Option<T> {
		match self.project_replace(SinkFilterState::None) {
			SinkFilterStateProjOwn::Some(t, _) => Some(t),
			SinkFilterStateProjOwn::None => None,
		}
	}
}
#[pin_project]
#[derive(new)]
pub struct SinkFilter<'a, F, I, T, Fut> {
	fut: Pin<&'a mut SinkFilterState<T, Fut>>,
	#[pin]
	i: I,
	f: F,
}
impl<'a, F, I, Fut, Item> Sink for SinkFilter<'a, F, I, Item, Fut>
where
	F: FnMut(&Item) -> Fut,
	Fut: Future<Output = bool>,
	I: Sink<Item = Item>,
{
	type Item = Item;

	fn poll_forward(
		self: Pin<&mut Self>, cx: &mut Context,
		mut stream: Pin<&mut impl Stream<Item = Self::Item>>,
	) -> Poll<()> {
		let self_ = self.project();
		let f = self_.f;
		let fut = self_.fut;
		let stream = stream::poll_fn(move |cx| loop {
			if fut.as_mut().fut().is_none() {
				let item = ready!(stream.as_mut().poll_next(cx));
				if item.is_none() {
					break Poll::Ready(None);
				}
				let item = item.unwrap();
				let fut_ = f(&item);
				fut.set(SinkFilterState::Some(item, fut_));
			}
			let filter = ready!(fut.as_mut().fut().unwrap().poll(cx));
			let item = fut.as_mut().take().unwrap();
			if filter {
				break Poll::Ready(Some(item));
			}
		});
		pin_mut!(stream);
		(self_.i).poll_forward(cx, stream)
	}
}

#[pin_project]
#[derive(new)]
pub struct SinkThen<'a, F, I, Fut, R, Item> {
	fut: Pin<&'a mut Option<Fut>>,
	#[pin]
	i: I,
	f: F,
	marker: PhantomData<fn(R, Item)>,
}
impl<'a, F, I, R, Fut, Item> Sink for SinkThen<'a, F, I, Fut, R, Item>
where
	F: FnMut(Item) -> Fut,
	Fut: Future<Output = R>,
	I: Sink<Item = R>,
{
	type Item = Item;

	fn poll_forward(
		self: Pin<&mut Self>, cx: &mut Context,
		mut stream: Pin<&mut impl Stream<Item = Self::Item>>,
	) -> Poll<()> {
		let self_ = self.project();
		let f = self_.f;
		let fut = self_.fut;
		let stream = stream::poll_fn(|cx| {
			if fut.is_none() {
				let item = ready!(stream.as_mut().poll_next(cx));
				if item.is_none() {
					return Poll::Ready(None);
				}
				fut.set(Some(f(item.unwrap())));
			}
			let item = ready!(fut.as_mut().as_pin_mut().unwrap().poll(cx));
			fut.set(None);
			Poll::Ready(Some(item))
		});
		pin_mut!(stream);
		(self_.i).poll_forward(cx, stream)
	}
}

#[pin_project]
#[derive(new)]
pub struct SinkFlatMap<'a, F, I, Fut, R, Item> {
	fut: Pin<&'a mut Option<Fut>>,
	#[pin]
	i: I,
	f: F,
	marker: PhantomData<fn() -> (Fut, R, Item)>,
}
impl<'a, F, I, R, Fut, Item> Sink for SinkFlatMap<'a, F, I, Fut, R, Item>
where
	F: FnMut(Item) -> Fut,
	Fut: Stream<Item = R>,
	I: Sink<Item = R>,
{
	type Item = Item;

	fn poll_forward(
		self: Pin<&mut Self>, cx: &mut Context,
		mut stream: Pin<&mut impl Stream<Item = Self::Item>>,
	) -> Poll<()> {
		let self_ = self.project();
		let f = self_.f;
		let fut = self_.fut;
		let stream = stream::poll_fn(|cx| loop {
			if fut.is_none() {
				let item = ready!(stream.as_mut().poll_next(cx));
				if item.is_none() {
					return Poll::Ready(None);
				}
				fut.set(Some(f(item.unwrap())));
			}
			let item = ready!(fut.as_mut().as_pin_mut().unwrap().poll_next(cx));
			if let Some(item) = item {
				break Poll::Ready(Some(item));
			}
			fut.set(None);
		});
		pin_mut!(stream);
		(self_.i).poll_forward(cx, stream)
	}
}