/rust/registry/src/index.crates.io-6f17d22bba15001f/rayon-1.10.0/src/iter/sum.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::plumbing::*; |
2 | | use super::ParallelIterator; |
3 | | |
4 | | use std::iter::{self, Sum}; |
5 | | use std::marker::PhantomData; |
6 | | |
7 | 0 | pub(super) fn sum<PI, S>(pi: PI) -> S |
8 | 0 | where |
9 | 0 | PI: ParallelIterator, |
10 | 0 | S: Send + Sum<PI::Item> + Sum, |
11 | 0 | { |
12 | 0 | pi.drive_unindexed(SumConsumer::new()) |
13 | 0 | } |
14 | | |
15 | 0 | fn add<T: Sum>(left: T, right: T) -> T { |
16 | 0 | [left, right].into_iter().sum() |
17 | 0 | } |
18 | | |
19 | | struct SumConsumer<S: Send> { |
20 | | _marker: PhantomData<*const S>, |
21 | | } |
22 | | |
23 | | unsafe impl<S: Send> Send for SumConsumer<S> {} |
24 | | |
25 | | impl<S: Send> SumConsumer<S> { |
26 | 0 | fn new() -> SumConsumer<S> { |
27 | 0 | SumConsumer { |
28 | 0 | _marker: PhantomData, |
29 | 0 | } |
30 | 0 | } |
31 | | } |
32 | | |
33 | | impl<S, T> Consumer<T> for SumConsumer<S> |
34 | | where |
35 | | S: Send + Sum<T> + Sum, |
36 | | { |
37 | | type Folder = SumFolder<S>; |
38 | | type Reducer = Self; |
39 | | type Result = S; |
40 | | |
41 | 0 | fn split_at(self, _index: usize) -> (Self, Self, Self) { |
42 | 0 | (SumConsumer::new(), SumConsumer::new(), SumConsumer::new()) |
43 | 0 | } |
44 | | |
45 | 0 | fn into_folder(self) -> Self::Folder { |
46 | 0 | SumFolder { |
47 | 0 | sum: iter::empty::<T>().sum(), |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | 0 | fn full(&self) -> bool { |
52 | 0 | false |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl<S, T> UnindexedConsumer<T> for SumConsumer<S> |
57 | | where |
58 | | S: Send + Sum<T> + Sum, |
59 | | { |
60 | 0 | fn split_off_left(&self) -> Self { |
61 | 0 | SumConsumer::new() |
62 | 0 | } |
63 | | |
64 | 0 | fn to_reducer(&self) -> Self::Reducer { |
65 | 0 | SumConsumer::new() |
66 | 0 | } |
67 | | } |
68 | | |
69 | | impl<S> Reducer<S> for SumConsumer<S> |
70 | | where |
71 | | S: Send + Sum, |
72 | | { |
73 | 0 | fn reduce(self, left: S, right: S) -> S { |
74 | 0 | add(left, right) |
75 | 0 | } |
76 | | } |
77 | | |
78 | | struct SumFolder<S> { |
79 | | sum: S, |
80 | | } |
81 | | |
82 | | impl<S, T> Folder<T> for SumFolder<S> |
83 | | where |
84 | | S: Sum<T> + Sum, |
85 | | { |
86 | | type Result = S; |
87 | | |
88 | 0 | fn consume(self, item: T) -> Self { |
89 | 0 | SumFolder { |
90 | 0 | sum: add(self.sum, iter::once(item).sum()), |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | 0 | fn consume_iter<I>(self, iter: I) -> Self |
95 | 0 | where |
96 | 0 | I: IntoIterator<Item = T>, |
97 | 0 | { |
98 | 0 | SumFolder { |
99 | 0 | sum: add(self.sum, iter.into_iter().sum()), |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | 0 | fn complete(self) -> S { |
104 | 0 | self.sum |
105 | 0 | } |
106 | | |
107 | 0 | fn full(&self) -> bool { |
108 | 0 | false |
109 | 0 | } |
110 | | } |