1 use super::plumbing
::*;
4 use std
::fmt
::{self, Debug}
;
8 /// `Map` is an iterator that transforms the elements of an underlying iterator.
10 /// This struct is created by the [`map()`] method on [`ParallelIterator`]
12 /// [`map()`]: trait.ParallelIterator.html#method.map
13 /// [`ParallelIterator`]: trait.ParallelIterator.html
14 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
16 pub struct Map
<I
: ParallelIterator
, F
> {
21 impl<I
: ParallelIterator
+ Debug
, F
> Debug
for Map
<I
, F
> {
22 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
24 .field("base", &self.base
)
29 /// Create a new `Map` iterator.
31 /// NB: a free fn because it is NOT part of the end-user API.
32 pub fn new
<I
, F
>(base
: I
, map_op
: F
) -> Map
<I
, F
>
33 where I
: ParallelIterator
41 impl<I
, F
, R
> ParallelIterator
for Map
<I
, F
>
42 where I
: ParallelIterator
,
43 F
: Fn(I
::Item
) -> R
+ Sync
+ Send
,
46 type Item
= F
::Output
;
48 fn drive_unindexed
<C
>(self, consumer
: C
) -> C
::Result
49 where C
: UnindexedConsumer
<Self::Item
>
51 let consumer1
= MapConsumer
::new(consumer
, &self.map_op
);
52 self.base
.drive_unindexed(consumer1
)
55 fn opt_len(&self) -> Option
<usize> {
60 impl<I
, F
, R
> IndexedParallelIterator
for Map
<I
, F
>
61 where I
: IndexedParallelIterator
,
62 F
: Fn(I
::Item
) -> R
+ Sync
+ Send
,
65 fn drive
<C
>(self, consumer
: C
) -> C
::Result
66 where C
: Consumer
<Self::Item
>
68 let consumer1
= MapConsumer
::new(consumer
, &self.map_op
);
69 self.base
.drive(consumer1
)
72 fn len(&self) -> usize {
76 fn with_producer
<CB
>(self, callback
: CB
) -> CB
::Output
77 where CB
: ProducerCallback
<Self::Item
>
79 return self.base
.with_producer(Callback
{
84 struct Callback
<CB
, F
> {
89 impl<T
, F
, R
, CB
> ProducerCallback
<T
> for Callback
<CB
, F
>
90 where CB
: ProducerCallback
<R
>,
94 type Output
= CB
::Output
;
96 fn callback
<P
>(self, base
: P
) -> CB
::Output
97 where P
: Producer
<Item
= T
>
99 let producer
= MapProducer
{
101 map_op
: &self.map_op
,
103 self.callback
.callback(producer
)
109 /// ////////////////////////////////////////////////////////////////////////
111 struct MapProducer
<'f
, P
, F
: 'f
> {
116 impl<'f
, P
, F
, R
> Producer
for MapProducer
<'f
, P
, F
>
118 F
: Fn(P
::Item
) -> R
+ Sync
,
121 type Item
= F
::Output
;
122 type IntoIter
= iter
::Map
<P
::IntoIter
, &'f F
>;
124 fn into_iter(self) -> Self::IntoIter
{
125 self.base
.into_iter().map(self.map_op
)
128 fn min_len(&self) -> usize {
131 fn max_len(&self) -> usize {
135 fn split_at(self, index
: usize) -> (Self, Self) {
136 let (left
, right
) = self.base
.split_at(index
);
147 fn fold_with
<G
>(self, folder
: G
) -> G
148 where G
: Folder
<Self::Item
>
150 let folder1
= MapFolder { base: folder, map_op: self.map_op }
;
151 self.base
.fold_with(folder1
).base
156 /// ////////////////////////////////////////////////////////////////////////
157 /// Consumer implementation
159 struct MapConsumer
<'f
, C
, F
: 'f
> {
164 impl<'f
, C
, F
> MapConsumer
<'f
, C
, F
> {
165 fn new(base
: C
, map_op
: &'f F
) -> Self {
173 impl<'f
, T
, R
, C
, F
> Consumer
<T
> for MapConsumer
<'f
, C
, F
>
174 where C
: Consumer
<F
::Output
>,
175 F
: Fn(T
) -> R
+ Sync
,
178 type Folder
= MapFolder
<'f
, C
::Folder
, F
>;
179 type Reducer
= C
::Reducer
;
180 type Result
= C
::Result
;
182 fn split_at(self, index
: usize) -> (Self, Self, Self::Reducer
) {
183 let (left
, right
, reducer
) = self.base
.split_at(index
);
184 (MapConsumer
::new(left
, self.map_op
), MapConsumer
::new(right
, self.map_op
), reducer
)
187 fn into_folder(self) -> Self::Folder
{
189 base
: self.base
.into_folder(),
194 fn full(&self) -> bool
{
199 impl<'f
, T
, R
, C
, F
> UnindexedConsumer
<T
> for MapConsumer
<'f
, C
, F
>
200 where C
: UnindexedConsumer
<F
::Output
>,
201 F
: Fn(T
) -> R
+ Sync
,
204 fn split_off_left(&self) -> Self {
205 MapConsumer
::new(self.base
.split_off_left(), &self.map_op
)
208 fn to_reducer(&self) -> Self::Reducer
{
209 self.base
.to_reducer()
213 struct MapFolder
<'f
, C
, F
: 'f
> {
218 impl<'f
, T
, R
, C
, F
> Folder
<T
> for MapFolder
<'f
, C
, F
>
219 where C
: Folder
<F
::Output
>,
222 type Result
= C
::Result
;
224 fn consume(self, item
: T
) -> Self {
225 let mapped_item
= (self.map_op
)(item
);
227 base
: self.base
.consume(mapped_item
),
232 fn complete(self) -> C
::Result
{
236 fn full(&self) -> bool
{