1 //! Minimal support for `rustc-rayon`, not intended for general use. 2 3 use crate::vec::Vec; 4 use crate::{Bucket, Entries, IndexMap, IndexSet}; 5 6 use rustc_rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; 7 use rustc_rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; 8 9 mod map { 10 use super::*; 11 12 impl<K, V, S> IntoParallelIterator for IndexMap<K, V, S> 13 where 14 K: Send, 15 V: Send, 16 { 17 type Item = (K, V); 18 type Iter = IntoParIter<K, V>; 19 into_par_iter(self) -> Self::Iter20 fn into_par_iter(self) -> Self::Iter { 21 IntoParIter { 22 entries: self.into_entries(), 23 } 24 } 25 } 26 27 pub struct IntoParIter<K, V> { 28 entries: Vec<Bucket<K, V>>, 29 } 30 31 impl<K: Send, V: Send> ParallelIterator for IntoParIter<K, V> { 32 type Item = (K, V); 33 34 parallel_iterator_methods!(Bucket::key_value); 35 } 36 37 impl<K: Send, V: Send> IndexedParallelIterator for IntoParIter<K, V> { 38 indexed_parallel_iterator_methods!(Bucket::key_value); 39 } 40 41 impl<'a, K, V, S> IntoParallelIterator for &'a IndexMap<K, V, S> 42 where 43 K: Sync, 44 V: Sync, 45 { 46 type Item = (&'a K, &'a V); 47 type Iter = ParIter<'a, K, V>; 48 into_par_iter(self) -> Self::Iter49 fn into_par_iter(self) -> Self::Iter { 50 ParIter { 51 entries: self.as_entries(), 52 } 53 } 54 } 55 56 pub struct ParIter<'a, K, V> { 57 entries: &'a [Bucket<K, V>], 58 } 59 60 impl<'a, K: Sync, V: Sync> ParallelIterator for ParIter<'a, K, V> { 61 type Item = (&'a K, &'a V); 62 63 parallel_iterator_methods!(Bucket::refs); 64 } 65 66 impl<K: Sync, V: Sync> IndexedParallelIterator for ParIter<'_, K, V> { 67 indexed_parallel_iterator_methods!(Bucket::refs); 68 } 69 70 impl<'a, K, V, S> IntoParallelIterator for &'a mut IndexMap<K, V, S> 71 where 72 K: Sync + Send, 73 V: Send, 74 { 75 type Item = (&'a K, &'a mut V); 76 type Iter = ParIterMut<'a, K, V>; 77 into_par_iter(self) -> Self::Iter78 fn into_par_iter(self) -> Self::Iter { 79 ParIterMut { 80 entries: self.as_entries_mut(), 81 } 82 } 83 } 84 85 pub struct ParIterMut<'a, K, V> { 86 entries: &'a mut [Bucket<K, V>], 87 } 88 89 impl<'a, K: Sync + Send, V: Send> ParallelIterator for ParIterMut<'a, K, V> { 90 type Item = (&'a K, &'a mut V); 91 92 parallel_iterator_methods!(Bucket::ref_mut); 93 } 94 95 impl<K: Sync + Send, V: Send> IndexedParallelIterator for ParIterMut<'_, K, V> { 96 indexed_parallel_iterator_methods!(Bucket::ref_mut); 97 } 98 } 99 100 mod set { 101 use super::*; 102 103 impl<T, S> IntoParallelIterator for IndexSet<T, S> 104 where 105 T: Send, 106 { 107 type Item = T; 108 type Iter = IntoParIter<T>; 109 into_par_iter(self) -> Self::Iter110 fn into_par_iter(self) -> Self::Iter { 111 IntoParIter { 112 entries: self.into_entries(), 113 } 114 } 115 } 116 117 pub struct IntoParIter<T> { 118 entries: Vec<Bucket<T, ()>>, 119 } 120 121 impl<T: Send> ParallelIterator for IntoParIter<T> { 122 type Item = T; 123 124 parallel_iterator_methods!(Bucket::key); 125 } 126 127 impl<T: Send> IndexedParallelIterator for IntoParIter<T> { 128 indexed_parallel_iterator_methods!(Bucket::key); 129 } 130 131 impl<'a, T, S> IntoParallelIterator for &'a IndexSet<T, S> 132 where 133 T: Sync, 134 { 135 type Item = &'a T; 136 type Iter = ParIter<'a, T>; 137 into_par_iter(self) -> Self::Iter138 fn into_par_iter(self) -> Self::Iter { 139 ParIter { 140 entries: self.as_entries(), 141 } 142 } 143 } 144 145 pub struct ParIter<'a, T> { 146 entries: &'a [Bucket<T, ()>], 147 } 148 149 impl<'a, T: Sync> ParallelIterator for ParIter<'a, T> { 150 type Item = &'a T; 151 152 parallel_iterator_methods!(Bucket::key_ref); 153 } 154 155 impl<T: Sync> IndexedParallelIterator for ParIter<'_, T> { 156 indexed_parallel_iterator_methods!(Bucket::key_ref); 157 } 158 } 159