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
use derive_new::new; use serde::{Deserialize, Serialize}; use serde_closure::traits::FnMut; use super::{ParallelPipe, ParallelStream, PipeTask, StreamTask}; #[derive(new)] #[must_use] pub struct Filter<I, F> { i: I, f: F, } impl_par_dist! { impl<I: ParallelStream, F> ParallelStream for Filter<I, F> where F: for<'a> FnMut<(&'a I::Item,), Output = bool> + Clone + Send + 'static, { type Item = I::Item; type Task = FilterTask<I::Task, F>; fn size_hint(&self) -> (usize, Option<usize>) { (0, self.i.size_hint().1) } fn next_task(&mut self) -> Option<Self::Task> { self.i.next_task().map(|task| { let f = self.f.clone(); FilterTask { task, f } }) } } impl<I: ParallelPipe<Source>, F, Source> ParallelPipe<Source> for Filter<I, F> where F: for<'a> FnMut<(&'a I::Item,), Output = bool> + Clone + Send + 'static, { type Item = I::Item; type Task = FilterTask<I::Task, F>; fn task(&self) -> Self::Task { let task = self.i.task(); let f = self.f.clone(); FilterTask { task, f } } } } #[derive(Serialize, Deserialize)] pub struct FilterTask<C, F> { task: C, f: F, } impl<C: StreamTask, F> StreamTask for FilterTask<C, F> where F: for<'a> FnMut<(&'a C::Item,), Output = bool>, { type Item = C::Item; type Async = crate::pipe::Filter<C::Async, F>; fn into_async(self) -> Self::Async { crate::pipe::Filter::new(self.task.into_async(), self.f) } } impl<C: PipeTask<Source>, F, Source> PipeTask<Source> for FilterTask<C, F> where F: for<'a> FnMut<(&'a C::Item,), Output = bool>, { type Item = C::Item; type Async = crate::pipe::Filter<C::Async, F>; fn into_async(self) -> Self::Async { crate::pipe::Filter::new(self.task.into_async(), self.f) } }