1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use super::ParallelIterator;
use super::len::ParallelLen;
use super::state::ParallelIteratorState;
use std::marker::PhantomData;
pub struct Map<M, MAP_OP> {
base: M,
map_op: MAP_OP,
}
impl<M, MAP_OP> Map<M, MAP_OP> {
pub fn new(base: M, map_op: MAP_OP) -> Map<M, MAP_OP> {
Map { base: base, map_op: map_op }
}
}
impl<M, MAP_OP, R> ParallelIterator for Map<M, MAP_OP>
where M: ParallelIterator,
MAP_OP: Fn(M::Item) -> R + Sync,
R: Send,
{
type Item = R;
type Shared = MapShared<M, MAP_OP>;
type State = MapState<M, MAP_OP>;
fn state(self) -> (Self::Shared, Self::State) {
let (base_shared, base_state) = self.base.state();
(MapShared { base: base_shared, map_op: self.map_op },
MapState { base: base_state, map_op: PhantomMapOp::new() })
}
}
pub struct MapShared<M, MAP_OP>
where M: ParallelIterator,
{
base: M::Shared,
map_op: MAP_OP,
}
pub struct MapState<M, MAP_OP>
where M: ParallelIterator,
{
base: M::State,
map_op: PhantomMapOp<MAP_OP>,
}
struct PhantomMapOp<MAP_OP>(PhantomData<*const MAP_OP>);
impl<MAP_OP> PhantomMapOp<MAP_OP> {
fn new() -> PhantomMapOp<MAP_OP> {
PhantomMapOp(PhantomData)
}
}
unsafe impl<MAP_OP: Sync> Send for PhantomMapOp<MAP_OP> { }
unsafe impl<M, MAP_OP, R> ParallelIteratorState for MapState<M, MAP_OP>
where M: ParallelIterator,
MAP_OP: Fn(M::Item) -> R + Sync,
{
type Item = R;
type Shared = MapShared<M, MAP_OP>;
fn len(&mut self, shared: &Self::Shared) -> ParallelLen {
self.base.len(&shared.base)
}
fn split_at(self, index: usize) -> (Self, Self) {
let (left, right) = self.base.split_at(index);
(MapState { base: left, map_op: PhantomMapOp::new() },
MapState { base: right, map_op: PhantomMapOp::new() })
}
fn next(&mut self, shared: &Self::Shared) -> Option<Self::Item> {
self.base.next(&shared.base)
.map(|base| (shared.map_op)(base))
}
}