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