|
| 1 | +use super::plumbing::*; |
| 2 | +use super::*; |
| 3 | + |
| 4 | +use std::iter; |
| 5 | + |
| 6 | +/// `Copied` is an iterator that copies the elements of an underlying iterator. |
| 7 | +/// |
| 8 | +/// This struct is created by the [`copied()`] method on [`ParallelIterator`] |
| 9 | +/// |
| 10 | +/// [`copied()`]: trait.ParallelIterator.html#method.copied |
| 11 | +/// [`ParallelIterator`]: trait.ParallelIterator.html |
| 12 | +#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 13 | +#[derive(Debug, Clone)] |
| 14 | +pub struct Copied<I: ParallelIterator> { |
| 15 | + base: I, |
| 16 | +} |
| 17 | + |
| 18 | +impl<I> Copied<I> |
| 19 | +where |
| 20 | + I: ParallelIterator, |
| 21 | +{ |
| 22 | + /// Create a new `Copied` iterator. |
| 23 | + pub(super) fn new(base: I) -> Self { |
| 24 | + Copied { base } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<'a, T, I> ParallelIterator for Copied<I> |
| 29 | +where |
| 30 | + I: ParallelIterator<Item = &'a T>, |
| 31 | + T: 'a + Copy + Send + Sync, |
| 32 | +{ |
| 33 | + type Item = T; |
| 34 | + |
| 35 | + fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 36 | + where |
| 37 | + C: UnindexedConsumer<Self::Item>, |
| 38 | + { |
| 39 | + let consumer1 = CopiedConsumer::new(consumer); |
| 40 | + self.base.drive_unindexed(consumer1) |
| 41 | + } |
| 42 | + |
| 43 | + fn opt_len(&self) -> Option<usize> { |
| 44 | + self.base.opt_len() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<'a, T, I> IndexedParallelIterator for Copied<I> |
| 49 | +where |
| 50 | + I: IndexedParallelIterator<Item = &'a T>, |
| 51 | + T: 'a + Copy + Send + Sync, |
| 52 | +{ |
| 53 | + fn drive<C>(self, consumer: C) -> C::Result |
| 54 | + where |
| 55 | + C: Consumer<Self::Item>, |
| 56 | + { |
| 57 | + let consumer1 = CopiedConsumer::new(consumer); |
| 58 | + self.base.drive(consumer1) |
| 59 | + } |
| 60 | + |
| 61 | + fn len(&self) -> usize { |
| 62 | + self.base.len() |
| 63 | + } |
| 64 | + |
| 65 | + fn with_producer<CB>(self, callback: CB) -> CB::Output |
| 66 | + where |
| 67 | + CB: ProducerCallback<Self::Item>, |
| 68 | + { |
| 69 | + return self.base.with_producer(Callback { callback }); |
| 70 | + |
| 71 | + struct Callback<CB> { |
| 72 | + callback: CB, |
| 73 | + } |
| 74 | + |
| 75 | + impl<'a, T, CB> ProducerCallback<&'a T> for Callback<CB> |
| 76 | + where |
| 77 | + CB: ProducerCallback<T>, |
| 78 | + T: 'a + Copy + Send, |
| 79 | + { |
| 80 | + type Output = CB::Output; |
| 81 | + |
| 82 | + fn callback<P>(self, base: P) -> CB::Output |
| 83 | + where |
| 84 | + P: Producer<Item = &'a T>, |
| 85 | + { |
| 86 | + let producer = CopiedProducer { base }; |
| 87 | + self.callback.callback(producer) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// //////////////////////////////////////////////////////////////////////// |
| 94 | +
|
| 95 | +struct CopiedProducer<P> { |
| 96 | + base: P, |
| 97 | +} |
| 98 | + |
| 99 | +impl<'a, T, P> Producer for CopiedProducer<P> |
| 100 | +where |
| 101 | + P: Producer<Item = &'a T>, |
| 102 | + T: 'a + Copy, |
| 103 | +{ |
| 104 | + type Item = T; |
| 105 | + type IntoIter = iter::Map<P::IntoIter, fn(&T) -> T>; |
| 106 | + |
| 107 | + fn into_iter(self) -> Self::IntoIter { |
| 108 | + // FIXME: use `Iterator::copied()` when Rust 1.36 is our minimum. |
| 109 | + self.base.into_iter().map(|&x| x) |
| 110 | + } |
| 111 | + |
| 112 | + fn min_len(&self) -> usize { |
| 113 | + self.base.min_len() |
| 114 | + } |
| 115 | + |
| 116 | + fn max_len(&self) -> usize { |
| 117 | + self.base.max_len() |
| 118 | + } |
| 119 | + |
| 120 | + fn split_at(self, index: usize) -> (Self, Self) { |
| 121 | + let (left, right) = self.base.split_at(index); |
| 122 | + ( |
| 123 | + CopiedProducer { base: left }, |
| 124 | + CopiedProducer { base: right }, |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + fn fold_with<F>(self, folder: F) -> F |
| 129 | + where |
| 130 | + F: Folder<Self::Item>, |
| 131 | + { |
| 132 | + self.base.fold_with(CopiedFolder { base: folder }).base |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// //////////////////////////////////////////////////////////////////////// |
| 137 | +/// Consumer implementation |
| 138 | +
|
| 139 | +struct CopiedConsumer<C> { |
| 140 | + base: C, |
| 141 | +} |
| 142 | + |
| 143 | +impl<C> CopiedConsumer<C> { |
| 144 | + fn new(base: C) -> Self { |
| 145 | + CopiedConsumer { base } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl<'a, T, C> Consumer<&'a T> for CopiedConsumer<C> |
| 150 | +where |
| 151 | + C: Consumer<T>, |
| 152 | + T: 'a + Copy, |
| 153 | +{ |
| 154 | + type Folder = CopiedFolder<C::Folder>; |
| 155 | + type Reducer = C::Reducer; |
| 156 | + type Result = C::Result; |
| 157 | + |
| 158 | + fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) { |
| 159 | + let (left, right, reducer) = self.base.split_at(index); |
| 160 | + ( |
| 161 | + CopiedConsumer::new(left), |
| 162 | + CopiedConsumer::new(right), |
| 163 | + reducer, |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + fn into_folder(self) -> Self::Folder { |
| 168 | + CopiedFolder { |
| 169 | + base: self.base.into_folder(), |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + fn full(&self) -> bool { |
| 174 | + self.base.full() |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +impl<'a, T, C> UnindexedConsumer<&'a T> for CopiedConsumer<C> |
| 179 | +where |
| 180 | + C: UnindexedConsumer<T>, |
| 181 | + T: 'a + Copy, |
| 182 | +{ |
| 183 | + fn split_off_left(&self) -> Self { |
| 184 | + CopiedConsumer::new(self.base.split_off_left()) |
| 185 | + } |
| 186 | + |
| 187 | + fn to_reducer(&self) -> Self::Reducer { |
| 188 | + self.base.to_reducer() |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +struct CopiedFolder<F> { |
| 193 | + base: F, |
| 194 | +} |
| 195 | + |
| 196 | +impl<'a, T, F> Folder<&'a T> for CopiedFolder<F> |
| 197 | +where |
| 198 | + F: Folder<T>, |
| 199 | + T: 'a + Copy, |
| 200 | +{ |
| 201 | + type Result = F::Result; |
| 202 | + |
| 203 | + fn consume(self, &item: &'a T) -> Self { |
| 204 | + CopiedFolder { |
| 205 | + base: self.base.consume(item), |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + fn consume_iter<I>(mut self, iter: I) -> Self |
| 210 | + where |
| 211 | + I: IntoIterator<Item = &'a T>, |
| 212 | + { |
| 213 | + // FIXME: use `Iterator::copied()` when Rust 1.36 is our minimum. |
| 214 | + self.base = self.base.consume_iter(iter.into_iter().map(|&x| x)); |
| 215 | + self |
| 216 | + } |
| 217 | + |
| 218 | + fn complete(self) -> F::Result { |
| 219 | + self.base.complete() |
| 220 | + } |
| 221 | + |
| 222 | + fn full(&self) -> bool { |
| 223 | + self.base.full() |
| 224 | + } |
| 225 | +} |
0 commit comments