1use crate::iter::plumbing::*;
20use crate::iter::*;
21use std::ops::RangeInclusive;
22
23#[derive(Debug, Clone)]
45pub struct Iter<T> {
46 range: RangeInclusive<T>,
47}
48
49impl<T> Iter<T>
50where
51 RangeInclusive<T>: Eq,
52 T: Ord + Copy,
53{
54 fn bounds(&self) -> Option<(T, T)> {
60 let start = *self.range.start();
61 let end = *self.range.end();
62 if start <= end && self.range == (start..=end) {
63 Some((start, end))
67 } else {
68 None
69 }
70 }
71}
72
73impl<T> IntoParallelIterator for RangeInclusive<T>
75where
76 Iter<T>: ParallelIterator,
77{
78 type Item = <Iter<T> as ParallelIterator>::Item;
79 type Iter = Iter<T>;
80
81 fn into_par_iter(self) -> Self::Iter {
82 Iter { range: self }
83 }
84}
85
86mod private {
94 use super::*;
95
96 pub trait RangeInteger: Sized + Send {
98 private_decl! {}
99
100 fn drive_unindexed<C>(iter: Iter<Self>, consumer: C) -> C::Result
101 where
102 C: UnindexedConsumer<Self>;
103
104 fn opt_len(iter: &Iter<Self>) -> Option<usize>;
105 }
106
107 pub trait IndexedRangeInteger: RangeInteger {
109 private_decl! {}
110
111 fn drive<C>(iter: Iter<Self>, consumer: C) -> C::Result
112 where
113 C: Consumer<Self>;
114
115 fn len(iter: &Iter<Self>) -> usize;
116
117 fn with_producer<CB>(iter: Iter<Self>, callback: CB) -> CB::Output
118 where
119 CB: ProducerCallback<Self>;
120 }
121}
122use private::{IndexedRangeInteger, RangeInteger};
123
124impl<T: RangeInteger> ParallelIterator for Iter<T> {
125 type Item = T;
126
127 fn drive_unindexed<C>(self, consumer: C) -> C::Result
128 where
129 C: UnindexedConsumer<T>,
130 {
131 T::drive_unindexed(self, consumer)
132 }
133
134 #[inline]
135 fn opt_len(&self) -> Option<usize> {
136 T::opt_len(self)
137 }
138}
139
140impl<T: IndexedRangeInteger> IndexedParallelIterator for Iter<T> {
141 fn drive<C>(self, consumer: C) -> C::Result
142 where
143 C: Consumer<T>,
144 {
145 T::drive(self, consumer)
146 }
147
148 #[inline]
149 fn len(&self) -> usize {
150 T::len(self)
151 }
152
153 fn with_producer<CB>(self, callback: CB) -> CB::Output
154 where
155 CB: ProducerCallback<T>,
156 {
157 T::with_producer(self, callback)
158 }
159}
160
161macro_rules! convert {
162 ( $iter:ident . $method:ident ( $( $arg:expr ),* ) ) => {
163 if let Some((start, end)) = $iter.bounds() {
164 if let Some(end) = end.checked_add(1) {
165 (start..end).into_par_iter().$method($( $arg ),*)
166 } else {
167 (start..end).into_par_iter().chain(once(end)).$method($( $arg ),*)
168 }
169 } else {
170 empty::<Self>().$method($( $arg ),*)
171 }
172 };
173}
174
175macro_rules! parallel_range_impl {
176 ( $t:ty ) => {
177 impl RangeInteger for $t {
178 private_impl! {}
179
180 fn drive_unindexed<C>(iter: Iter<$t>, consumer: C) -> C::Result
181 where
182 C: UnindexedConsumer<$t>,
183 {
184 convert!(iter.drive_unindexed(consumer))
185 }
186
187 fn opt_len(iter: &Iter<$t>) -> Option<usize> {
188 convert!(iter.opt_len())
189 }
190 }
191 };
192}
193
194macro_rules! indexed_range_impl {
195 ( $t:ty ) => {
196 parallel_range_impl! { $t }
197
198 impl IndexedRangeInteger for $t {
199 private_impl! {}
200
201 fn drive<C>(iter: Iter<$t>, consumer: C) -> C::Result
202 where
203 C: Consumer<$t>,
204 {
205 convert!(iter.drive(consumer))
206 }
207
208 fn len(iter: &Iter<$t>) -> usize {
209 iter.range.len()
210 }
211
212 fn with_producer<CB>(iter: Iter<$t>, callback: CB) -> CB::Output
213 where
214 CB: ProducerCallback<$t>,
215 {
216 convert!(iter.with_producer(callback))
217 }
218 }
219 };
220}
221
222indexed_range_impl! {u8}
224indexed_range_impl! {u16}
225indexed_range_impl! {i8}
226indexed_range_impl! {i16}
227
228parallel_range_impl! {usize}
230parallel_range_impl! {isize}
231parallel_range_impl! {u32}
232parallel_range_impl! {i32}
233parallel_range_impl! {u64}
234parallel_range_impl! {i64}
235parallel_range_impl! {u128}
236parallel_range_impl! {i128}
237
238macro_rules! convert_char {
240 ( $self:ident . $method:ident ( $( $arg:expr ),* ) ) => {
241 if let Some((start, end)) = $self.bounds() {
242 let start = start as u32;
243 let end = end as u32;
244 if start < 0xD800 && 0xE000 <= end {
245 (start..0xD800)
247 .into_par_iter()
248 .chain(0xE000..end + 1) .map(|codepoint| unsafe { char::from_u32_unchecked(codepoint) })
250 .$method($( $arg ),*)
251 } else {
252 (start..end + 1) .into_par_iter()
255 .map(|codepoint| unsafe { char::from_u32_unchecked(codepoint) })
256 .$method($( $arg ),*)
257 }
258 } else {
259 empty::<char>().$method($( $arg ),*)
260 }
261 };
262}
263
264impl ParallelIterator for Iter<char> {
265 type Item = char;
266
267 fn drive_unindexed<C>(self, consumer: C) -> C::Result
268 where
269 C: UnindexedConsumer<Self::Item>,
270 {
271 convert_char!(self.drive(consumer))
272 }
273
274 fn opt_len(&self) -> Option<usize> {
275 Some(self.len())
276 }
277}
278
279impl IndexedParallelIterator for Iter<char> {
281 fn drive<C>(self, consumer: C) -> C::Result
283 where
284 C: Consumer<Self::Item>,
285 {
286 convert_char!(self.drive(consumer))
287 }
288
289 fn len(&self) -> usize {
290 if let Some((start, end)) = self.bounds() {
291 let start = start as u32;
293 let end = end as u32;
294 let mut count = end - start;
295 if start < 0xD800 && 0xE000 <= end {
296 count -= 0x800
297 }
298 (count + 1) as usize } else {
300 0
301 }
302 }
303
304 fn with_producer<CB>(self, callback: CB) -> CB::Output
305 where
306 CB: ProducerCallback<Self::Item>,
307 {
308 convert_char!(self.with_producer(callback))
309 }
310}
311
312#[test]
313#[cfg(target_pointer_width = "64")]
314fn test_u32_opt_len() {
315 assert_eq!(Some(101), (0..=100u32).into_par_iter().opt_len());
316 assert_eq!(
317 Some(u32::MAX as usize),
318 (0..=u32::MAX - 1).into_par_iter().opt_len()
319 );
320 assert_eq!(
321 Some(u32::MAX as usize + 1),
322 (0..=u32::MAX).into_par_iter().opt_len()
323 );
324}
325
326#[test]
327fn test_u64_opt_len() {
328 assert_eq!(Some(101), (0..=100u64).into_par_iter().opt_len());
329 assert_eq!(
330 Some(usize::MAX),
331 (0..=usize::MAX as u64 - 1).into_par_iter().opt_len()
332 );
333 assert_eq!(None, (0..=usize::MAX as u64).into_par_iter().opt_len());
334 assert_eq!(None, (0..=u64::MAX).into_par_iter().opt_len());
335}
336
337#[test]
338fn test_u128_opt_len() {
339 assert_eq!(Some(101), (0..=100u128).into_par_iter().opt_len());
340 assert_eq!(
341 Some(usize::MAX),
342 (0..=usize::MAX as u128 - 1).into_par_iter().opt_len()
343 );
344 assert_eq!(None, (0..=usize::MAX as u128).into_par_iter().opt_len());
345 assert_eq!(None, (0..=u128::MAX).into_par_iter().opt_len());
346}
347
348#[test]
351#[cfg(target_pointer_width = "64")]
352fn test_usize_i64_overflow() {
353 use crate::ThreadPoolBuilder;
354
355 let iter = (-2..=i64::MAX).into_par_iter();
356 assert_eq!(iter.opt_len(), Some(i64::MAX as usize + 3));
357
358 let pool = ThreadPoolBuilder::new().num_threads(8).build().unwrap();
360 pool.install(|| assert_eq!(iter.find_last(|_| true), Some(i64::MAX)));
361}
362
363#[test]
364fn test_issue_833() {
365 fn is_even(n: i64) -> bool {
366 n % 2 == 0
367 }
368
369 let v: Vec<_> = (1..=100).into_par_iter().filter(|&x| is_even(x)).collect();
371 assert!(v.into_iter().eq((2..=100).step_by(2)));
372
373 let pos = (0..=100).into_par_iter().position_any(|x| x == 50i16);
375 assert_eq!(pos, Some(50usize));
376
377 assert!((0..=100)
378 .into_par_iter()
379 .zip(0..=100)
380 .all(|(a, b)| i16::eq(&a, &b)));
381}