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

use super::{ParallelPipe, ParallelStream, PipeTask, StreamTask};

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

impl_par_dist! {
	impl<P: ParallelStream, F, R: Stream> ParallelStream for FlatMap<P, F>
	where
		F: FnMut<(P::Item,), Output = R> + Clone + Send + 'static,
	{
		type Item = R::Item;
		type Task = FlatMapTask<P::Task, F>;

		fn size_hint(&self) -> (usize, Option<usize>) {
			(0, None)
		}
		fn next_task(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Task>> {
			let self_ = self.project();
			let f = self_.f;
			self_.pipe.next_task(cx).map(|task| {
				task.map(|task| {
					let f = f.clone();
					FlatMapTask { task, f }
				})
			})
		}
	}

	impl<P: ParallelPipe<Input>, F, R: Stream, Input> ParallelPipe<Input> for FlatMap<P, F>
	where
		F: FnMut<(P::Output,), Output = R> + Clone + Send + 'static,
	{
		type Output = R::Item;
		type Task = FlatMapTask<P::Task, F>;

		fn task(&self) -> Self::Task {
			let task = self.pipe.task();
			let f = self.f.clone();
			FlatMapTask { task, f }
		}
	}
}

#[derive(Serialize, Deserialize)]
pub struct FlatMapTask<C, F> {
	task: C,
	f: F,
}
impl<C: StreamTask, F: FnMut<(C::Item,), Output = R> + Clone, R: Stream> StreamTask
	for FlatMapTask<C, F>
{
	type Item = R::Item;
	type Async = crate::pipe::FlatMap<C::Async, F, R>;

	fn into_async(self) -> Self::Async {
		crate::pipe::FlatMap::new(self.task.into_async(), self.f)
	}
}
impl<C: PipeTask<Input>, F: FnMut<(C::Output,), Output = R> + Clone, R: Stream, Input>
	PipeTask<Input> for FlatMapTask<C, F>
{
	type Output = R::Item;
	type Async = crate::pipe::FlatMap<C::Async, F, R>;

	fn into_async(self) -> Self::Async {
		crate::pipe::FlatMap::new(self.task.into_async(), self.f)
	}
}