use super::plumbing::*; use super::*; use std::fmt::{self, Debug}; use std::iter; /// `Map` is an iterator that transforms the elements of an underlying iterator. /// /// This struct is created by the [`map()`] method on [`ParallelIterator`] /// /// [`map()`]: trait.ParallelIterator.html#method.map /// [`ParallelIterator`]: trait.ParallelIterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Clone)] pub struct Map { base: I, map_op: F, } impl Debug for Map { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Map").field("base", &self.base).finish() } } impl Map where I: ParallelIterator, { /// Creates a new `Map` iterator. pub(super) fn new(base: I, map_op: F) -> Self { Map { base, map_op } } } impl ParallelIterator for Map where I: ParallelIterator, F: Fn(I::Item) -> R + Sync + Send, R: Send, { type Item = F::Output; fn drive_unindexed(self, consumer: C) -> C::Result where C: UnindexedConsumer, { let consumer1 = MapConsumer::new(consumer, &self.map_op); self.base.drive_unindexed(consumer1) } fn opt_len(&self) -> Option { self.base.opt_len() } } impl IndexedParallelIterator for Map where I: IndexedParallelIterator, F: Fn(I::Item) -> R + Sync + Send, R: Send, { fn drive(self, consumer: C) -> C::Result where C: Consumer, { let consumer1 = MapConsumer::new(consumer, &self.map_op); 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, map_op: self.map_op, }); struct Callback { callback: CB, map_op: F, } impl ProducerCallback for Callback where CB: ProducerCallback, F: Fn(T) -> R + Sync, R: Send, { type Output = CB::Output; fn callback

(self, base: P) -> CB::Output where P: Producer, { let producer = MapProducer { base, map_op: &self.map_op, }; self.callback.callback(producer) } } } } /// //////////////////////////////////////////////////////////////////////// struct MapProducer<'f, P, F> { base: P, map_op: &'f F, } impl<'f, P, F, R> Producer for MapProducer<'f, P, F> where P: Producer, F: Fn(P::Item) -> R + Sync, R: Send, { type Item = F::Output; type IntoIter = iter::Map; fn into_iter(self) -> Self::IntoIter { self.base.into_iter().map(self.map_op) } 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); ( MapProducer { base: left, map_op: self.map_op, }, MapProducer { base: right, map_op: self.map_op, }, ) } fn fold_with(self, folder: G) -> G where G: Folder, { let folder1 = MapFolder { base: folder, map_op: self.map_op, }; self.base.fold_with(folder1).base } } /// //////////////////////////////////////////////////////////////////////// /// Consumer implementation struct MapConsumer<'f, C, F> { base: C, map_op: &'f F, } impl<'f, C, F> MapConsumer<'f, C, F> { fn new(base: C, map_op: &'f F) -> Self { MapConsumer { base, map_op } } } impl<'f, T, R, C, F> Consumer for MapConsumer<'f, C, F> where C: Consumer, F: Fn(T) -> R + Sync, R: Send, { type Folder = MapFolder<'f, C::Folder, F>; 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); ( MapConsumer::new(left, self.map_op), MapConsumer::new(right, self.map_op), reducer, ) } fn into_folder(self) -> Self::Folder { MapFolder { base: self.base.into_folder(), map_op: self.map_op, } } fn full(&self) -> bool { self.base.full() } } impl<'f, T, R, C, F> UnindexedConsumer for MapConsumer<'f, C, F> where C: UnindexedConsumer, F: Fn(T) -> R + Sync, R: Send, { fn split_off_left(&self) -> Self { MapConsumer::new(self.base.split_off_left(), self.map_op) } fn to_reducer(&self) -> Self::Reducer { self.base.to_reducer() } } struct MapFolder<'f, C, F> { base: C, map_op: &'f F, } impl<'f, T, R, C, F> Folder for MapFolder<'f, C, F> where C: Folder, F: Fn(T) -> R, { type Result = C::Result; fn consume(self, item: T) -> Self { let mapped_item = (self.map_op)(item); MapFolder { base: self.base.consume(mapped_item), map_op: self.map_op, } } fn consume_iter(mut self, iter: I) -> Self where I: IntoIterator, { self.base = self.base.consume_iter(iter.into_iter().map(self.map_op)); self } fn complete(self) -> C::Result { self.base.complete() } fn full(&self) -> bool { self.base.full() } }