use super::plumbing::*; use super::*; use std::iter; /// `Cloned` is an iterator that clones the elements of an underlying iterator. /// /// This struct is created by the [`cloned()`] method on [`ParallelIterator`] /// /// [`cloned()`]: trait.ParallelIterator.html#method.cloned /// [`ParallelIterator`]: trait.ParallelIterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Debug, Clone)] pub struct Cloned { base: I, } impl Cloned where I: ParallelIterator, { /// Creates a new `Cloned` iterator. pub(super) fn new(base: I) -> Self { Cloned { base } } } impl<'a, T, I> ParallelIterator for Cloned where I: ParallelIterator, T: 'a + Clone + Send + Sync, { type Item = T; fn drive_unindexed(self, consumer: C) -> C::Result where C: UnindexedConsumer, { let consumer1 = ClonedConsumer::new(consumer); self.base.drive_unindexed(consumer1) } fn opt_len(&self) -> Option { self.base.opt_len() } } impl<'a, T, I> IndexedParallelIterator for Cloned where I: IndexedParallelIterator, T: 'a + Clone + Send + Sync, { fn drive(self, consumer: C) -> C::Result where C: Consumer, { let consumer1 = ClonedConsumer::new(consumer); self.base.drive(consumer1) } fn len(&self) -> usize { self.base.len() } fn with_producer(self, callback: CB) -> CB::Output where CB: ProducerCallback, { return self.base.with_producer(Callback { callback }); struct Callback { callback: CB, } impl<'a, T, CB> ProducerCallback<&'a T> for Callback where CB: ProducerCallback, T: 'a + Clone + Send, { type Output = CB::Output; fn callback

(self, base: P) -> CB::Output where P: Producer, { let producer = ClonedProducer { base }; self.callback.callback(producer) } } } } /// //////////////////////////////////////////////////////////////////////// struct ClonedProducer

{ base: P, } impl<'a, T, P> Producer for ClonedProducer

where P: Producer, T: 'a + Clone, { type Item = T; type IntoIter = iter::Cloned; fn into_iter(self) -> Self::IntoIter { self.base.into_iter().cloned() } fn min_len(&self) -> usize { self.base.min_len() } fn max_len(&self) -> usize { self.base.max_len() } fn split_at(self, index: usize) -> (Self, Self) { let (left, right) = self.base.split_at(index); ( ClonedProducer { base: left }, ClonedProducer { base: right }, ) } fn fold_with(self, folder: F) -> F where F: Folder, { self.base.fold_with(ClonedFolder { base: folder }).base } } /// //////////////////////////////////////////////////////////////////////// /// Consumer implementation struct ClonedConsumer { base: C, } impl ClonedConsumer { fn new(base: C) -> Self { ClonedConsumer { base } } } impl<'a, T, C> Consumer<&'a T> for ClonedConsumer where C: Consumer, T: 'a + Clone, { type Folder = ClonedFolder; type Reducer = C::Reducer; type Result = C::Result; fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) { let (left, right, reducer) = self.base.split_at(index); ( ClonedConsumer::new(left), ClonedConsumer::new(right), reducer, ) } fn into_folder(self) -> Self::Folder { ClonedFolder { base: self.base.into_folder(), } } fn full(&self) -> bool { self.base.full() } } impl<'a, T, C> UnindexedConsumer<&'a T> for ClonedConsumer where C: UnindexedConsumer, T: 'a + Clone, { fn split_off_left(&self) -> Self { ClonedConsumer::new(self.base.split_off_left()) } fn to_reducer(&self) -> Self::Reducer { self.base.to_reducer() } } struct ClonedFolder { base: F, } impl<'a, T, F> Folder<&'a T> for ClonedFolder where F: Folder, T: 'a + Clone, { type Result = F::Result; fn consume(self, item: &'a T) -> Self { ClonedFolder { base: self.base.consume(item.clone()), } } fn consume_iter(mut self, iter: I) -> Self where I: IntoIterator, { self.base = self.base.consume_iter(iter.into_iter().cloned()); self } fn complete(self) -> F::Result { self.base.complete() } fn full(&self) -> bool { self.base.full() } }