| 1 | /// Evaluate any iterator in their own thread. |
| 2 | /// |
| 3 | /// This is particularly useful if the wrapped iterator performs IO and/or heavy computations. |
| 4 | /// Use [`EagerIter::new()`] for instantiation. |
| 5 | pub struct EagerIterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#> { |
| 6 | receiverrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#receiver.: 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#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>>, |
| 7 | chunkrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#chunk.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::IntoIterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/IntoIter#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>>, |
| 8 | size_hintrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#size_hint.: (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>), |
| 9 | } |
| 10 | |
| 11 | impl<I> EagerIterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#<I> |
| 12 | where |
| 13 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static, |
| 14 | <I 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#: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#, |
| 15 | { |
| 16 | /// Return a new `EagerIter` which evaluates `iter` in its own thread, |
| 17 | /// with a given `chunk_size` allowing a maximum `chunks_in_flight`. |
| 18 | /// |
| 19 | /// * `chunk_size` describes how many items returned by `iter` will be a single item of this `EagerIter`. |
| 20 | /// This helps to reduce the overhead imposed by transferring many small items. |
| 21 | /// If this number is 1, each item will become a single chunk. 0 is invalid. |
| 22 | /// * `chunks_in_flight` describes how many chunks can be kept in memory in case the consumer of the `EagerIter`s items |
| 23 | /// isn't consuming them fast enough. Setting this number to 0 effectively turns off any caching, but blocks `EagerIter` |
| 24 | /// if its items aren't consumed fast enough. |
| 25 | pub fn newrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`]new().(iter: I, chunk_size: usize, chunks_in_flight: usize) -> Selfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`] { |
| 26 | let (sender, receiver) = 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().(chunks_in_flight); |
| 27 | let size_hint = iter.size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#size_hint().(); |
| 28 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(chunk_size >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 0, "non-zero chunk size is needed"); |
| 29 | |
| 30 | 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 || { |
| 31 | let mut out = 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().(chunk_size); |
| 32 | for item in iter { |
| 33 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(item); |
| 34 | if out.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().chunk_size { |
| 35 | if sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`]send().(out).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 36 | return; |
| 37 | } |
| 38 | out = 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().(chunk_size); |
| 39 | } |
| 40 | } |
| 41 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().out.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 42 | sender.sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`]send().(out).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().(); |
| 43 | } |
| 44 | }); |
| 45 | EagerIterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter# { |
| 46 | receiverrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#receiver., |
| 47 | chunkrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#chunk.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 48 | size_hintrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#size_hint., |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn fill_buf_and_poprust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`]fill_buf_and_pop().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 53 | self.chunkrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#chunk. = self.receiverrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#receiver..recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]recv().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|v| { |
| 54 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().v.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().()); |
| 55 | v.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() |
| 56 | }); |
| 57 | self.chunkrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#chunk..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#::nextrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/impl#[`IntoIter<T, A>`][Iterator]next().) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for EagerIterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#<I> |
| 62 | where |
| 63 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static, |
| 64 | <I 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#: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#, |
| 65 | { |
| 66 | type Itemrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`][Iterator]Item# = I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; |
| 67 | |
| 68 | fn nextrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`][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/eager_iter/impl#[`EagerIter<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 69 | match self.chunkrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#chunk..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_mut().() { |
| 70 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(chunk) => chunk.nextrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/impl#[`IntoIter<T, A>`][Iterator]next().().or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| self.fill_buf_and_poprust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`]fill_buf_and_pop().()), |
| 71 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => self.fill_buf_and_poprust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`]fill_buf_and_pop().(), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn size_hintrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 76 | self.size_hintrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#size_hint. |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// An conditional `EagerIter`, which may become a just-in-time iterator running in the main thread depending on a condition. |
| 81 | pub enum EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#> { |
| 82 | /// A separate thread will eagerly evaluate iterator `I`. |
| 83 | Eagerrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#Eager#(EagerIterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#<I>), |
| 84 | /// The current thread evaluates `I`. |
| 85 | OnDemandrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#OnDemand#(I), |
| 86 | } |
| 87 | |
| 88 | impl<I> EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#<I> |
| 89 | where |
| 90 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static, |
| 91 | <I 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#: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#, |
| 92 | { |
| 93 | /// Return a new `EagerIterIf` if `condition()` returns true. |
| 94 | /// |
| 95 | /// For all other parameters, please see [`EagerIter::new()`]. |
| 96 | pub fn newrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIterIf<I>`]new().(condition: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> bool, iter: I, chunk_size: usize, chunks_in_flight: usize) -> Selfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIterIf<I>`] { |
| 97 | if condition() { |
| 98 | EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#::Eagerrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#Eager#(EagerIterrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIter#::newrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`]new().(iter, chunk_size, chunks_in_flight)) |
| 99 | } else { |
| 100 | EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#::OnDemandrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#OnDemand#(iter) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#<I> |
| 105 | where |
| 106 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + 'static, |
| 107 | <I 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#: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#, |
| 108 | { |
| 109 | type Itemrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIterIf<I>`][Iterator]Item# = I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; |
| 110 | |
| 111 | fn nextrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIterIf<I>`][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/eager_iter/impl#[`EagerIterIf<I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 112 | match self { |
| 113 | EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#::OnDemandrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#OnDemand#(i) => i.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().(), |
| 114 | EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#::Eagerrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#Eager#(i) => i.nextrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`][Iterator]next().(), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | fn size_hintrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIterIf<I>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 119 | match self { |
| 120 | EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#::OnDemandrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#OnDemand#(i) => i.size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#size_hint().(), |
| 121 | EagerIterIfrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#::Eagerrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/EagerIterIf#Eager#(i) => i.size_hintrust-analyzer cargo gix-features 0.48.0 parallel/eager_iter/impl#[`EagerIter<I>`][Iterator]size_hint().(), |
| 122 | } |
| 123 | } |
| 124 | } |