| 1 | #[cfg(feature = "parallel")] |
| 2 | mod steppedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/ { |
| 3 | use craterust-analyzer cargo gix-features 0.48.0 crate/::parallelrust-analyzer cargo gix-features 0.48.0 parallel/::num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().; |
| 4 | |
| 5 | /// An iterator adaptor to allow running computations using [`in_parallel()`][crate::parallel::in_parallel()] in a step-wise manner, see the [module docs][crate::parallel] |
| 6 | /// for details. |
| 7 | pub struct Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<Reduce: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#> { |
| 8 | /// This field is first to assure it's dropped first and cause threads that are dropped next to stop their loops |
| 9 | /// as sending results fails when the receiver is dropped. |
| 10 | receive_resultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#receive_result.: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::Receiverrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Receiver#<Reduce::Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input#>, |
| 11 | /// `join()` will be called on these guards to assure every thread tries to send through a closed channel. When |
| 12 | /// that happens, they break out of their loops. |
| 13 | threadsrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#threads.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::JoinHandlerust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/JoinHandle#<()>>, |
| 14 | /// The reducer is called only in the thread using the iterator, dropping it has no side effects. |
| 15 | reducerrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#reducer.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Reduce>, |
| 16 | } |
| 17 | |
| 18 | impl<Reduce: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<Reduce> { |
| 19 | fn droprust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`][Drop]drop().(&mut self) { |
| 20 | let (_, sink) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::channelrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/channel().(); |
| 21 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/replace().(&mut self.receive_resultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#receive_result., sink)); |
| 22 | |
| 23 | let mut last_err = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 24 | for handle in stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/take().(&mut self.threadsrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#threads.) { |
| 25 | if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = handle.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/join_handle/impl#[`JoinHandle<T>`]join().() { |
| 26 | last_err = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(err); |
| 27 | }; |
| 28 | } |
| 29 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(thread_err) = last_err { |
| 30 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/::resume_unwindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std panic/resume_unwind().(thread_err); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl<Reduce: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#> Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<Reduce> { |
| 36 | /// Instantiate a new iterator and start working in threads. |
| 37 | /// For a description of parameters, see [`in_parallel()`][crate::parallel::in_parallel()]. |
| 38 | pub fn newrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`]new().<InputIter, ThreadStateFn, ConsumeFn, I, O, S>( |
| 39 | input: InputIter, |
| 40 | thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, |
| 41 | new_thread_state: ThreadStateFn, |
| 42 | consume: ConsumeFn, |
| 43 | reducer: Reduce, |
| 44 | ) -> Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`] |
| 45 | where |
| 46 | InputIter: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I> + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static, |
| 47 | ThreadStateFn: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(usize) -> S + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + 'static, |
| 48 | ConsumeFn: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(I, &mut S) -> O + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + 'static, |
| 49 | Reduce: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O> + 'static, |
| 50 | I: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static, |
| 51 | O: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static, |
| 52 | { |
| 53 | let num_threads = num_threadsrust-analyzer cargo gix-features 0.48.0 parallel/num_threads().(thread_limit); |
| 54 | let mut threads = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(num_threads rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 55 | let receive_result = { |
| 56 | let (send_input, receive_input) = crossbeam_channelrust-analyzer cargo crossbeam-channel 0.5.15 crate/::boundedrust-analyzer cargo crossbeam-channel 0.5.15 channel/bounded().::<I>(num_threads); |
| 57 | let (send_result, receive_result) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::sync_channelrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/sync_channel().::<O>(num_threads); |
| 58 | for thread_id in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#num_threads { |
| 59 | let handle = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().({ |
| 60 | let send_result = send_result.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`][Clone]clone().(); |
| 61 | let receive_input = receive_input.clonerust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Receiver<T>`][Clone]clone().(); |
| 62 | let new_thread_state = new_thread_state.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().(); |
| 63 | let consume = consume.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().(); |
| 64 | move || { |
| 65 | let mut state = new_thread_state(thread_id); |
| 66 | for item in receive_input { |
| 67 | if send_result.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`]send().(consume(item, &mut state)).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | threads.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(handle); |
| 74 | } |
| 75 | threads.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(move || { |
| 76 | for item in input { |
| 77 | if send_input.sendrust-analyzer cargo crossbeam-channel 0.5.15 channel/impl#[`Sender<T>`]send().(item).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | })); |
| 82 | receive_result |
| 83 | }; |
| 84 | Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise# { |
| 85 | threadsrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#threads., |
| 86 | receive_resultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#receive_result., |
| 87 | reducerrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#reducer.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(reducer), |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Consume the iterator by finishing its iteration and calling [`Reduce::finalize()`][crate::parallel::Reduce::finalize()]. |
| 92 | pub fn finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`]finalize().(mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Reduce::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, Reduce::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#> { |
| 93 | for value in self.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().() { |
| 94 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(value?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 95 | } |
| 96 | self.reducerrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#reducer. |
| 97 | .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().() |
| 98 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("this is the last call before consumption") |
| 99 | .finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#finalize().() |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl<Reduce: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<Reduce> { |
| 104 | type Itemrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`][Iterator]Item# = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Reduce::FeedProducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#FeedProduce#, Reduce::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>; |
| 105 | |
| 106 | fn nextrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<<Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`][Iterator] as Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 107 | self.receive_resultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#receive_result. |
| 108 | .recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]recv().() |
| 109 | .okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().() |
| 110 | .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|input| self.reducerrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#reducer..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|r| r.feedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#feed().(input))) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl<R: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#> superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize# for Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<R> { |
| 115 | type Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<R>`][Finalize]Reduce# = R; |
| 116 | |
| 117 | fn finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<R>`][Finalize]finalize().( |
| 118 | self, |
| 119 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#< |
| 120 | <<Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<R>`][Finalize] as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#>::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#Reduce# as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, |
| 121 | <<Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<R>`][Finalize] as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#>::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#Reduce# as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#, |
| 122 | > { |
| 123 | Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#::finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/impl#[`Stepwise<Reduce>`]finalize().(self) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #[cfg(not(feature = "parallel"))] |
| 129 | mod steppedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/ { |
| 130 | /// An iterator adaptor to allow running computations using [`in_parallel()`][crate::parallel::in_parallel()] in a step-wise manner, see the [module docs][crate::parallel] |
| 131 | /// for details. |
| 132 | pub struct Stepwise<InputIter, ConsumeFn, ThreadState, Reduce> { |
| 133 | input: InputIter, |
| 134 | consume: ConsumeFn, |
| 135 | thread_state: ThreadState, |
| 136 | reducer: Reduce, |
| 137 | } |
| 138 | |
| 139 | impl<InputIter, ConsumeFn, Reduce, I, O, S> Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<InputIter, ConsumeFn, S, Reduce> |
| 140 | where |
| 141 | InputIter: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I>, |
| 142 | ConsumeFn: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(I, &mut S) -> O, |
| 143 | Reduce: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O>, |
| 144 | { |
| 145 | /// Instantiate a new iterator. |
| 146 | /// For a description of parameters, see [`in_parallel()`][crate::parallel::in_parallel()]. |
| 147 | pub fn new<ThreadStateFn>( |
| 148 | input: InputIter, |
| 149 | _thread_limit: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, |
| 150 | new_thread_state: ThreadStateFn, |
| 151 | consume: ConsumeFn, |
| 152 | reducer: Reduce, |
| 153 | ) -> Self |
| 154 | where |
| 155 | ThreadStateFn: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(usize) -> S, |
| 156 | { |
| 157 | Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise# { |
| 158 | input, |
| 159 | consume, |
| 160 | thread_state: new_thread_state(0), |
| 161 | reducer, |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Consume the iterator by finishing its iteration and calling [`Reduce::finalize()`][crate::parallel::Reduce::finalize()]. |
| 166 | pub fn finalize(mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Reduce::Output, Reduce::Error> { |
| 167 | for value in selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/.by_ref() { |
| 168 | droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(value?); |
| 169 | } |
| 170 | selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/.reducer.finalize() |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | impl<InputIter, ConsumeFn, ThreadState, Reduce, I, O> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<InputIter, ConsumeFn, ThreadState, Reduce> |
| 175 | where |
| 176 | InputIter: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I>, |
| 177 | ConsumeFn: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(I, &mut ThreadState) -> O, |
| 178 | Reduce: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O>, |
| 179 | { |
| 180 | type Item = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Reduce::FeedProduce, Reduce::Error>; |
| 181 | |
| 182 | fn next(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<<Self as Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 183 | selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/.input |
| 184 | .next() |
| 185 | .map(|input| selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/.reducer.feed((selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/.consume)(input, &mut selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/.thread_state))) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | impl<InputIter, ConsumeFn, R, I, O, S> superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize# for Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#<InputIter, ConsumeFn, S, R> |
| 190 | where |
| 191 | InputIter: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = I>, |
| 192 | ConsumeFn: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(I, &mut S) -> O, |
| 193 | R: superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#<Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input# = O>, |
| 194 | { |
| 195 | type Reduce = R; |
| 196 | |
| 197 | fn finalize( |
| 198 | self, |
| 199 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#< |
| 200 | <<Self as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#>::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#Reduce# as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, |
| 201 | <<Self as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#>::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#Reduce# as superrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#, |
| 202 | > { |
| 203 | Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#::finalize(selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/) |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#; |
| 209 | |
| 210 | pub use steppedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/::Stepwiserust-analyzer cargo gix-features 0.48.0 parallel/reduce/stepped/Stepwise#; |
| 211 | |
| 212 | /// An trait for aggregating items commonly produced in threads into a single result, without itself |
| 213 | /// needing to be thread safe. |
| 214 | pub trait Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce# { |
| 215 | /// The type fed to the reducer in the [`feed()`][Reduce::feed()] method. |
| 216 | /// |
| 217 | /// It's produced by a function that may run on multiple threads. |
| 218 | type Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input#; |
| 219 | /// The type produced in Ok(…) by [`feed()`][Reduce::feed()]. |
| 220 | /// Most reducers by nature use `()` here as the value is in the aggregation. |
| 221 | /// However, some may use it to collect statistics only and return their Input |
| 222 | /// in some form as a result here for [`Stepwise`] to be useful. |
| 223 | type FeedProducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#FeedProduce#; |
| 224 | /// The type produced once by the [`finalize()`][Reduce::finalize()] method. |
| 225 | /// |
| 226 | /// For traditional reducers, this is the value produced by the entire operation. |
| 227 | /// For those made for step-wise iteration this may be aggregated statistics. |
| 228 | type Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#; |
| 229 | /// The error type to use for all methods of this trait. |
| 230 | type Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#; |
| 231 | /// Called each time a new `item` was produced in order to aggregate it into the final result. |
| 232 | /// |
| 233 | /// If an `Error` is returned, the entire operation will be stopped. |
| 234 | fn feedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#feed().(&mut self, item: Self::Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self::FeedProducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#FeedProduce#, Self::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>; |
| 235 | /// Called once for all items that were passed to `feed()`, producing the final `Output` of the operation or an `Error`. |
| 236 | fn finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#finalize().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, Self::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>; |
| 237 | } |
| 238 | |
| 239 | /// An identity reducer for those who want to use [`Stepwise`] or [`in_parallel()`][crate::parallel::in_parallel()] |
| 240 | /// without the use of non-threaded reduction of products created in threads. |
| 241 | pub struct IdentityWithResultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult#<Input, Error> { |
| 242 | _inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult#_input.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<Input>, |
| 243 | _errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult#_error.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<Error>, |
| 244 | } |
| 245 | |
| 246 | impl<Input, Error> Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for IdentityWithResultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult#<Input, Error> { |
| 247 | fn defaultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Default]default().() -> Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Default] { |
| 248 | IdentityWithResultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult# { |
| 249 | _inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult#_input.: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/impl#[`PhantomData<T>`][Default]default().(), |
| 250 | _errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult#_error.: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/impl#[`PhantomData<T>`][Default]default().(), |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | impl<Input, Error> Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce# for IdentityWithResultrust-analyzer cargo gix-features 0.48.0 parallel/reduce/IdentityWithResult#<Input, Error> { |
| 256 | type Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]Input# = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Input, Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>; |
| 257 | type FeedProducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]FeedProduce# = Input; |
| 258 | type Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]Output# = (); |
| 259 | type Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]Error# = Error; |
| 260 | |
| 261 | fn feedrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]feed().(&mut self, item: Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]::Inputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Input#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]::FeedProducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#FeedProduce#, Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#> { |
| 262 | item |
| 263 | } |
| 264 | |
| 265 | fn finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]finalize().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, Selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/impl#[`IdentityWithResult<Input, Error>`][Reduce]::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#> { |
| 266 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /// A trait reflecting the `finalize()` method of [`Reduce`] implementations |
| 271 | pub trait Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize# { |
| 272 | /// An implementation of [`Reduce`] |
| 273 | type Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#Reduce#: selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#; |
| 274 | |
| 275 | /// Similar to the [`Reduce::finalize()`] method |
| 276 | fn finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#finalize().( |
| 277 | self, |
| 278 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<<<Self as Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#>::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#Reduce# as selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Outputrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Output#, <<Self as Finalizerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#>::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Finalize#Reduce# as selfrust-analyzer cargo gix-features 0.48.0 parallel/reduce/::Reducerust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#>::Errorrust-analyzer cargo gix-features 0.48.0 parallel/reduce/Reduce#Error#>; |
| 279 | } |