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
use derive_new::new;
use educe::Educe;
use futures::{ready, Stream};
use pin_project::pin_project;
use serde::{Deserialize, Serialize};
use serde_closure::traits::FnMut;
use std::{
	marker::PhantomData, pin::Pin, task::{Context, Poll}
};

use super::{
	DistributedPipe, DistributedSink, ParallelPipe, ParallelSink, Reducer, ReducerProcessSend, ReducerSend
};
use crate::{pipe::Sink, pool::ProcessSend};

#[derive(new)]
#[must_use]
pub struct Any<P, F> {
	pipe: P,
	f: F,
}

impl<P: ParallelPipe<Input>, Input, F> ParallelSink<Input> for Any<P, F>
where
	F: FnMut<(P::Output,), Output = bool> + Clone + Send + 'static,
{
	type Done = bool;
	type Pipe = P;
	type ReduceA = AnyReducer<P::Output, F>;
	type ReduceC = BoolOrReducer;

	fn reducers(self) -> (Self::Pipe, Self::ReduceA, Self::ReduceC) {
		(self.pipe, AnyReducer(self.f, PhantomData), BoolOrReducer)
	}
}
impl<P: DistributedPipe<Input>, Input, F> DistributedSink<Input> for Any<P, F>
where
	F: FnMut<(P::Output,), Output = bool> + Clone + ProcessSend + 'static,
{
	type Done = bool;
	type Pipe = P;
	type ReduceA = AnyReducer<P::Output, F>;
	type ReduceB = BoolOrReducer;
	type ReduceC = BoolOrReducer;

	fn reducers(self) -> (Self::Pipe, Self::ReduceA, Self::ReduceB, Self::ReduceC) {
		(
			self.pipe,
			AnyReducer(self.f, PhantomData),
			BoolOrReducer,
			BoolOrReducer,
		)
	}
}

#[derive(Educe, Serialize, Deserialize)]
#[educe(Clone(bound = "F: Clone"))]
#[serde(
	bound(serialize = "F: Serialize"),
	bound(deserialize = "F: Deserialize<'de>")
)]
pub struct AnyReducer<Item, F>(F, PhantomData<fn() -> Item>);

impl<Item, F> Reducer<Item> for AnyReducer<Item, F>
where
	F: FnMut<(Item,), Output = bool>,
{
	type Done = bool;
	type Async = AnyReducerAsync<Item, F>;

	fn into_async(self) -> Self::Async {
		AnyReducerAsync(self.0, true, PhantomData)
	}
}
impl<Item, F> ReducerProcessSend<Item> for AnyReducer<Item, F>
where
	F: FnMut<(Item,), Output = bool>,
{
	type Done = bool;
}
impl<Item, F> ReducerSend<Item> for AnyReducer<Item, F>
where
	F: FnMut<(Item,), Output = bool>,
{
	type Done = bool;
}

#[pin_project]
#[derive(Serialize, Deserialize)]
#[serde(
	bound(serialize = "F: Serialize"),
	bound(deserialize = "F: Deserialize<'de>")
)]
pub struct AnyReducerAsync<Item, F>(F, bool, PhantomData<fn() -> Item>);

impl<Item, F> Sink<Item> for AnyReducerAsync<Item, F>
where
	F: FnMut<(Item,), Output = bool>,
{
	type Done = bool;

	#[inline(always)]
	fn poll_forward(
		self: Pin<&mut Self>, cx: &mut Context, mut stream: Pin<&mut impl Stream<Item = Item>>,
	) -> Poll<Self::Done> {
		let self_ = self.project();
		while *self_.1 {
			if let Some(item) = ready!(stream.as_mut().poll_next(cx)) {
				*self_.1 = *self_.1 && !self_.0.call_mut((item,));
			} else {
				break;
			}
		}
		Poll::Ready(!*self_.1)
	}
}

#[derive(Clone, Serialize, Deserialize)]
pub struct BoolOrReducer;

impl Reducer<bool> for BoolOrReducer {
	type Done = bool;
	type Async = BoolOrReducerAsync;

	fn into_async(self) -> Self::Async {
		BoolOrReducerAsync(true)
	}
}
impl ReducerProcessSend<bool> for BoolOrReducer {
	type Done = bool;
}
impl ReducerSend<bool> for BoolOrReducer {
	type Done = bool;
}

#[pin_project]
#[derive(Serialize, Deserialize)]
pub struct BoolOrReducerAsync(bool);

impl Sink<bool> for BoolOrReducerAsync {
	type Done = bool;

	#[inline(always)]
	fn poll_forward(
		mut self: Pin<&mut Self>, cx: &mut Context, mut stream: Pin<&mut impl Stream<Item = bool>>,
	) -> Poll<Self::Done> {
		while self.0 {
			if let Some(item) = ready!(stream.as_mut().poll_next(cx)) {
				self.0 = self.0 && !item;
			} else {
				break;
			}
		}
		Poll::Ready(!self.0)
	}
}