|
| 1 | +use super::ParallelIterator; |
| 2 | +use super::plumbing::*; |
| 3 | + |
| 4 | +use super::private::Try; |
| 5 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 6 | + |
| 7 | +pub fn try_reduce_with<PI, R, T>(pi: PI, reduce_op: R) -> Option<T> |
| 8 | + where PI: ParallelIterator<Item = T>, |
| 9 | + R: Fn(T::Ok, T::Ok) -> T + Sync, |
| 10 | + T: Try + Send |
| 11 | +{ |
| 12 | + let full = AtomicBool::new(false); |
| 13 | + let consumer = TryReduceWithConsumer { |
| 14 | + reduce_op: &reduce_op, |
| 15 | + full: &full, |
| 16 | + }; |
| 17 | + pi.drive_unindexed(consumer) |
| 18 | +} |
| 19 | + |
| 20 | +struct TryReduceWithConsumer<'r, R: 'r> { |
| 21 | + reduce_op: &'r R, |
| 22 | + full: &'r AtomicBool, |
| 23 | +} |
| 24 | + |
| 25 | +impl<'r, R> Copy for TryReduceWithConsumer<'r, R> {} |
| 26 | + |
| 27 | +impl<'r, R> Clone for TryReduceWithConsumer<'r, R> { |
| 28 | + fn clone(&self) -> Self { |
| 29 | + *self |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<'r, R, T> Consumer<T> for TryReduceWithConsumer<'r, R> |
| 34 | + where R: Fn(T::Ok, T::Ok) -> T + Sync, |
| 35 | + T: Try + Send |
| 36 | +{ |
| 37 | + type Folder = TryReduceWithFolder<'r, R, T>; |
| 38 | + type Reducer = Self; |
| 39 | + type Result = Option<T>; |
| 40 | + |
| 41 | + fn split_at(self, _index: usize) -> (Self, Self, Self) { |
| 42 | + (self, self, self) |
| 43 | + } |
| 44 | + |
| 45 | + fn into_folder(self) -> Self::Folder { |
| 46 | + TryReduceWithFolder { |
| 47 | + reduce_op: self.reduce_op, |
| 48 | + opt_result: None, |
| 49 | + full: self.full, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn full(&self) -> bool { |
| 54 | + self.full.load(Ordering::Relaxed) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<'r, R, T> UnindexedConsumer<T> for TryReduceWithConsumer<'r, R> |
| 59 | + where R: Fn(T::Ok, T::Ok) -> T + Sync, |
| 60 | + T: Try + Send |
| 61 | +{ |
| 62 | + fn split_off_left(&self) -> Self { |
| 63 | + *self |
| 64 | + } |
| 65 | + |
| 66 | + fn to_reducer(&self) -> Self::Reducer { |
| 67 | + *self |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<'r, R, T> Reducer<Option<T>> for TryReduceWithConsumer<'r, R> |
| 72 | + where R: Fn(T::Ok, T::Ok) -> T + Sync, |
| 73 | + T: Try |
| 74 | +{ |
| 75 | + fn reduce(self, left: Option<T>, right: Option<T>) -> Option<T> { |
| 76 | + let reduce_op = self.reduce_op; |
| 77 | + match (left, right) { |
| 78 | + (None, x) | (x, None) => x, |
| 79 | + (Some(a), Some(b)) => { |
| 80 | + match (a.into_result(), b.into_result()) { |
| 81 | + (Ok(a), Ok(b)) => Some(reduce_op(a, b)), |
| 82 | + (Err(e), _) | (_, Err(e)) => Some(T::from_error(e)), |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +struct TryReduceWithFolder<'r, R: 'r, T: Try> { |
| 90 | + reduce_op: &'r R, |
| 91 | + opt_result: Option<Result<T::Ok, T::Error>>, |
| 92 | + full: &'r AtomicBool, |
| 93 | +} |
| 94 | + |
| 95 | +impl<'r, R, T> Folder<T> for TryReduceWithFolder<'r, R, T> |
| 96 | + where R: Fn(T::Ok, T::Ok) -> T, |
| 97 | + T: Try |
| 98 | +{ |
| 99 | + type Result = Option<T>; |
| 100 | + |
| 101 | + fn consume(self, item: T) -> Self { |
| 102 | + let reduce_op = self.reduce_op; |
| 103 | + let result = match self.opt_result { |
| 104 | + None => item.into_result(), |
| 105 | + Some(Ok(a)) => match item.into_result() { |
| 106 | + Ok(b) => reduce_op(a, b).into_result(), |
| 107 | + Err(e) => Err(e), |
| 108 | + }, |
| 109 | + Some(Err(e)) => Err(e), |
| 110 | + }; |
| 111 | + if result.is_err() { |
| 112 | + self.full.store(true, Ordering::Relaxed) |
| 113 | + } |
| 114 | + TryReduceWithFolder { |
| 115 | + opt_result: Some(result), |
| 116 | + ..self |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + fn complete(self) -> Option<T> { |
| 121 | + self.opt_result.map(|result| match result { |
| 122 | + Ok(ok) => T::from_ok(ok), |
| 123 | + Err(error) => T::from_error(error), |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + fn full(&self) -> bool { |
| 128 | + self.full.load(Ordering::Relaxed) |
| 129 | + } |
| 130 | +} |
0 commit comments