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

(self, base: P) -> CB::Output where P: Producer, { let producer = MapWithProducer { base, item: self.item, map_op: &self.map_op, }; self.callback.callback(producer) } } } } /// //////////////////////////////////////////////////////////////////////// struct MapWithProducer<'f, P, U, F> { base: P, item: U, map_op: &'f F, } impl<'f, P, U, F, R> Producer for MapWithProducer<'f, P, U, F> where P: Producer, U: Send + Clone, F: Fn(&mut U, P::Item) -> R + Sync, R: Send, { type Item = R; type IntoIter = MapWithIter<'f, P::IntoIter, U, F>; fn into_iter(self) -> Self::IntoIter { MapWithIter { base: self.base.into_iter(), item: self.item, map_op: 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); ( MapWithProducer { base: left, item: self.item.clone(), map_op: self.map_op, }, MapWithProducer { base: right, item: self.item, map_op: self.map_op, }, ) } fn fold_with(self, folder: G) -> G where G: Folder, { let folder1 = MapWithFolder { base: folder, item: self.item, map_op: self.map_op, }; self.base.fold_with(folder1).base } } struct MapWithIter<'f, I, U, F> { base: I, item: U, map_op: &'f F, } impl<'f, I, U, F, R> Iterator for MapWithIter<'f, I, U, F> where I: Iterator, F: Fn(&mut U, I::Item) -> R + Sync, R: Send, { type Item = R; fn next(&mut self) -> Option { let item = self.base.next()?; Some((self.map_op)(&mut self.item, item)) } fn size_hint(&self) -> (usize, Option) { self.base.size_hint() } } impl<'f, I, U, F, R> DoubleEndedIterator for MapWithIter<'f, I, U, F> where I: DoubleEndedIterator, F: Fn(&mut U, I::Item) -> R + Sync, R: Send, { fn next_back(&mut self) -> Option { let item = self.base.next_back()?; Some((self.map_op)(&mut self.item, item)) } } impl<'f, I, U, F, R> ExactSizeIterator for MapWithIter<'f, I, U, F> where I: ExactSizeIterator, F: Fn(&mut U, I::Item) -> R + Sync, R: Send, { } /// //////////////////////////////////////////////////////////////////////// /// Consumer implementation struct MapWithConsumer<'f, C, U, F> { base: C, item: U, map_op: &'f F, } impl<'f, C, U, F> MapWithConsumer<'f, C, U, F> { fn new(base: C, item: U, map_op: &'f F) -> Self { MapWithConsumer { base, item, map_op } } } impl<'f, T, U, R, C, F> Consumer for MapWithConsumer<'f, C, U, F> where C: Consumer, U: Send + Clone, F: Fn(&mut U, T) -> R + Sync, R: Send, { type Folder = MapWithFolder<'f, C::Folder, U, 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); ( MapWithConsumer::new(left, self.item.clone(), self.map_op), MapWithConsumer::new(right, self.item, self.map_op), reducer, ) } fn into_folder(self) -> Self::Folder { MapWithFolder { base: self.base.into_folder(), item: self.item, map_op: self.map_op, } } fn full(&self) -> bool { self.base.full() } } impl<'f, T, U, R, C, F> UnindexedConsumer for MapWithConsumer<'f, C, U, F> where C: UnindexedConsumer, U: Send + Clone, F: Fn(&mut U, T) -> R + Sync, R: Send, { fn split_off_left(&self) -> Self { MapWithConsumer::new(self.base.split_off_left(), self.item.clone(), self.map_op) } fn to_reducer(&self) -> Self::Reducer { self.base.to_reducer() } } struct MapWithFolder<'f, C, U, F> { base: C, item: U, map_op: &'f F, } impl<'f, T, U, R, C, F> Folder for MapWithFolder<'f, C, U, F> where C: Folder, F: Fn(&mut U, T) -> R, { type Result = C::Result; fn consume(mut self, item: T) -> Self { let mapped_item = (self.map_op)(&mut self.item, item); self.base = self.base.consume(mapped_item); self } fn consume_iter(mut self, iter: I) -> Self where I: IntoIterator, { fn with<'f, T, U, R>( item: &'f mut U, map_op: impl Fn(&mut U, T) -> R + 'f, ) -> impl FnMut(T) -> R + 'f { move |x| map_op(item, x) } { let mapped_iter = iter.into_iter().map(with(&mut self.item, self.map_op)); self.base = self.base.consume_iter(mapped_iter); } self } fn complete(self) -> C::Result { self.base.complete() } fn full(&self) -> bool { self.base.full() } } // ------------------------------------------------------------------------------------------------ /// `MapInit` is an iterator that transforms the elements of an underlying iterator. /// /// This struct is created by the [`map_init()`] method on [`ParallelIterator`] /// /// [`map_init()`]: trait.ParallelIterator.html#method.map_init /// [`ParallelIterator`]: trait.ParallelIterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Clone)] pub struct MapInit { base: I, init: INIT, map_op: F, } impl Debug for MapInit { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("MapInit").field("base", &self.base).finish() } } impl MapInit where I: ParallelIterator, { /// Creates a new `MapInit` iterator. pub(super) fn new(base: I, init: INIT, map_op: F) -> Self { MapInit { base, init, map_op } } } impl ParallelIterator for MapInit where I: ParallelIterator, INIT: Fn() -> T + Sync + Send, F: Fn(&mut T, I::Item) -> R + Sync + Send, R: Send, { type Item = R; fn drive_unindexed(self, consumer: C) -> C::Result where C: UnindexedConsumer, { let consumer1 = MapInitConsumer::new(consumer, &self.init, &self.map_op); self.base.drive_unindexed(consumer1) } fn opt_len(&self) -> Option { self.base.opt_len() } } impl IndexedParallelIterator for MapInit where I: IndexedParallelIterator, INIT: Fn() -> T + Sync + Send, F: Fn(&mut T, I::Item) -> R + Sync + Send, R: Send, { fn drive(self, consumer: C) -> C::Result where C: Consumer, { let consumer1 = MapInitConsumer::new(consumer, &self.init, &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, init: self.init, map_op: self.map_op, }); struct Callback { callback: CB, init: INIT, map_op: F, } impl ProducerCallback for Callback where CB: ProducerCallback, INIT: Fn() -> U + Sync, F: Fn(&mut U, T) -> R + Sync, R: Send, { type Output = CB::Output; fn callback

(self, base: P) -> CB::Output where P: Producer, { let producer = MapInitProducer { base, init: &self.init, map_op: &self.map_op, }; self.callback.callback(producer) } } } } /// //////////////////////////////////////////////////////////////////////// struct MapInitProducer<'f, P, INIT, F> { base: P, init: &'f INIT, map_op: &'f F, } impl<'f, P, INIT, U, F, R> Producer for MapInitProducer<'f, P, INIT, F> where P: Producer, INIT: Fn() -> U + Sync, F: Fn(&mut U, P::Item) -> R + Sync, R: Send, { type Item = R; type IntoIter = MapWithIter<'f, P::IntoIter, U, F>; fn into_iter(self) -> Self::IntoIter { MapWithIter { base: self.base.into_iter(), item: (self.init)(), map_op: 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); ( MapInitProducer { base: left, init: self.init, map_op: self.map_op, }, MapInitProducer { base: right, init: self.init, map_op: self.map_op, }, ) } fn fold_with(self, folder: G) -> G where G: Folder, { let folder1 = MapWithFolder { base: folder, item: (self.init)(), map_op: self.map_op, }; self.base.fold_with(folder1).base } } /// //////////////////////////////////////////////////////////////////////// /// Consumer implementation struct MapInitConsumer<'f, C, INIT, F> { base: C, init: &'f INIT, map_op: &'f F, } impl<'f, C, INIT, F> MapInitConsumer<'f, C, INIT, F> { fn new(base: C, init: &'f INIT, map_op: &'f F) -> Self { MapInitConsumer { base, init, map_op } } } impl<'f, T, INIT, U, R, C, F> Consumer for MapInitConsumer<'f, C, INIT, F> where C: Consumer, INIT: Fn() -> U + Sync, F: Fn(&mut U, T) -> R + Sync, R: Send, { type Folder = MapWithFolder<'f, C::Folder, U, 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); ( MapInitConsumer::new(left, self.init, self.map_op), MapInitConsumer::new(right, self.init, self.map_op), reducer, ) } fn into_folder(self) -> Self::Folder { MapWithFolder { base: self.base.into_folder(), item: (self.init)(), map_op: self.map_op, } } fn full(&self) -> bool { self.base.full() } } impl<'f, T, INIT, U, R, C, F> UnindexedConsumer for MapInitConsumer<'f, C, INIT, F> where C: UnindexedConsumer, INIT: Fn() -> U + Sync, F: Fn(&mut U, T) -> R + Sync, R: Send, { fn split_off_left(&self) -> Self { MapInitConsumer::new(self.base.split_off_left(), self.init, self.map_op) } fn to_reducer(&self) -> Self::Reducer { self.base.to_reducer() } }