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
use super::ParallelIterator;
use super::len::*;
use super::internal::*;
use super::noop::*;

pub fn for_each<PAR_ITER, OP, T>(pi: PAR_ITER, op: &OP)
    where PAR_ITER: ParallelIterator<Item = T>,
          OP: Fn(T) + Sync,
          T: Send
{
    let consumer = ForEachConsumer { op: op };
    pi.drive_unindexed(consumer)
}

struct ForEachConsumer<'f, OP: 'f> {
    op: &'f OP,
}

impl<'f, OP, ITEM> Consumer<ITEM> for ForEachConsumer<'f, OP>
    where OP: Fn(ITEM) + Sync
{
    type Folder = ForEachConsumer<'f, OP>;
    type Reducer = NoopReducer;
    type Result = ();

    fn cost(&mut self, cost: f64) -> f64 {
        // This isn't quite right, as we will do more than O(n) reductions, but whatever.
        cost * FUNC_ADJUSTMENT
    }

    fn split_at(self, _index: usize) -> (Self, Self, NoopReducer) {
        (self.split_off(), self.split_off(), NoopReducer)
    }

    fn into_folder(self) -> Self {
        self
    }
}

impl<'f, OP, ITEM> Folder<ITEM> for ForEachConsumer<'f, OP>
    where OP: Fn(ITEM) + Sync
{
    type Result = ();

    fn consume(self, item: ITEM) -> Self {
        (self.op)(item);
        self
    }

    fn complete(self) {}
}

impl<'f, OP, ITEM> UnindexedConsumer<ITEM> for ForEachConsumer<'f, OP>
    where OP: Fn(ITEM) + Sync
{
    fn split_off(&self) -> Self {
        ForEachConsumer { op: self.op }
    }

    fn to_reducer(&self) -> NoopReducer {
        NoopReducer
    }
}