use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use super::{ConsumerMulti, DistributedIteratorMulti};
#[must_use]
pub struct Cloned<I, T, Source> {
i: I,
marker: PhantomData<fn(Source, T)>,
}
impl<I, T, Source> Cloned<I, T, Source> {
pub(super) fn new(i: I) -> Self {
Self {
i,
marker: PhantomData,
}
}
}
impl<'a, I, Source, T: 'a> DistributedIteratorMulti<&'a Source> for Cloned<I, T, Source>
where
I: DistributedIteratorMulti<&'a Source, Item = &'a T>,
T: Clone,
{
type Item = T;
type Task = ClonedConsumer<I::Task>;
fn task(&self) -> Self::Task {
let task = self.i.task();
ClonedConsumer { task }
}
}
#[derive(Serialize, Deserialize)]
pub struct ClonedConsumer<T> {
task: T,
}
impl<'a, C, Source, T: 'a> ConsumerMulti<&'a Source> for ClonedConsumer<C>
where
C: ConsumerMulti<&'a Source, Item = &'a T>,
T: Clone,
{
type Item = T;
fn run(&self, source: &'a Source, i: &mut impl FnMut(Self::Item) -> bool) -> bool {
self.task.run(source, &mut |item| i(item.clone()))
}
}