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
use derive_new::new;
use futures::{ready, Stream};
use pin_project::pin_project;
use std::{
	pin::Pin, task::{Context, Poll}
};

use super::Pipe;

#[pin_project]
#[derive(new)]
pub struct Filter<C, F> {
	#[pin]
	task: C,
	f: F,
}

impl<C: Stream, F> Stream for Filter<C, F>
where
	F: FnMut(&C::Item) -> bool,
{
	type Item = C::Item;

	fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
		let mut self_ = self.project();
		let (mut task, f) = (self_.task, &mut self_.f);
		Poll::Ready(loop {
			match ready!(task.as_mut().poll_next(cx)) {
				Some(t) if f(&t) => break Some(t),
				Some(_) => (),
				None => break None,
			}
		})
	}
}

impl<C: Pipe<Source>, F, Source> Pipe<Source> for Filter<C, F>
where
	F: FnMut(&C::Item) -> bool,
{
	type Item = C::Item;

	fn poll_next(
		self: Pin<&mut Self>, cx: &mut Context, mut stream: Pin<&mut impl Stream<Item = Source>>,
	) -> Poll<Option<Self::Item>> {
		let mut self_ = self.project();
		let (mut task, f) = (self_.task, &mut self_.f);
		Poll::Ready(loop {
			match ready!(task.as_mut().poll_next(cx, stream.as_mut())) {
				Some(t) if f(&t) => break Some(t),
				Some(_) => (),
				None => break None,
			}
		})
	}
}