Coverage Report

Created: 2025-11-11 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/range.rs
Line
Count
Source
1
//! Parallel iterator types for [ranges],
2
//! the type for values created by `a..b` expressions
3
//!
4
//! You will rarely need to interact with this module directly unless you have
5
//! need to name one of the iterator types.
6
//!
7
//! ```
8
//! use rayon::prelude::*;
9
//!
10
//! let r = (0..100u64).into_par_iter()
11
//!                    .sum();
12
//!
13
//! // compare result with sequential calculation
14
//! assert_eq!((0..100).sum::<u64>(), r);
15
//! ```
16
//!
17
//! [ranges]: std::ops::Range
18
19
use crate::iter::plumbing::*;
20
use crate::iter::*;
21
use std::ops::Range;
22
23
/// Parallel iterator over a range, implemented for all integer types and `char`.
24
///
25
/// **Note:** The `zip` operation requires `IndexedParallelIterator`
26
/// which is not implemented for `u64`, `i64`, `u128`, or `i128`.
27
///
28
/// ```
29
/// use rayon::prelude::*;
30
///
31
/// let p = (0..25usize).into_par_iter()
32
///                   .zip(0..25usize)
33
///                   .filter(|&(x, y)| x % 5 == 0 || y % 5 == 0)
34
///                   .map(|(x, y)| x * y)
35
///                   .sum::<usize>();
36
///
37
/// let s = (0..25usize).zip(0..25)
38
///                   .filter(|&(x, y)| x % 5 == 0 || y % 5 == 0)
39
///                   .map(|(x, y)| x * y)
40
///                   .sum();
41
///
42
/// assert_eq!(p, s);
43
/// ```
44
#[derive(Debug, Clone)]
45
pub struct Iter<T> {
46
    range: Range<T>,
47
}
48
49
/// Implemented for ranges of all primitive integer types and `char`.
50
impl<T> IntoParallelIterator for Range<T>
51
where
52
    Iter<T>: ParallelIterator,
53
{
54
    type Item = <Iter<T> as ParallelIterator>::Item;
55
    type Iter = Iter<T>;
56
57
0
    fn into_par_iter(self) -> Self::Iter {
58
0
        Iter { range: self }
59
0
    }
Unexecuted instantiation: <core::ops::range::Range<i8> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<u8> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<isize> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<usize> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<i32> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<u32> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<i128> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<u128> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<i16> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<u16> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<i64> as rayon::iter::IntoParallelIterator>::into_par_iter
Unexecuted instantiation: <core::ops::range::Range<u64> as rayon::iter::IntoParallelIterator>::into_par_iter
60
}
61
62
struct IterProducer<T> {
63
    range: Range<T>,
64
}
65
66
impl<T> IntoIterator for IterProducer<T>
67
where
68
    Range<T>: Iterator,
69
{
70
    type Item = <Range<T> as Iterator>::Item;
71
    type IntoIter = Range<T>;
72
73
0
    fn into_iter(self) -> Self::IntoIter {
74
0
        self.range
75
0
    }
76
}
77
78
/// These traits help drive integer type inference. Without them, an unknown `{integer}` type only
79
/// has constraints on `Iter<{integer}>`, which will probably give up and use `i32`. By adding
80
/// these traits on the item type, the compiler can see a more direct constraint to infer like
81
/// `{integer}: RangeInteger`, which works better. See `test_issue_833` for an example.
82
///
83
/// They have to be `pub` since they're seen in the public `impl ParallelIterator` constraints, but
84
/// we put them in a private modules so they're not actually reachable in our public API.
85
mod private {
86
    use super::*;
87
88
    /// Implementation details of `ParallelIterator for Iter<Self>`
89
    pub trait RangeInteger: Sized + Send {
90
        private_decl! {}
91
92
        fn drive_unindexed<C>(iter: Iter<Self>, consumer: C) -> C::Result
93
        where
94
            C: UnindexedConsumer<Self>;
95
96
        fn opt_len(iter: &Iter<Self>) -> Option<usize>;
97
    }
98
99
    /// Implementation details of `IndexedParallelIterator for Iter<Self>`
100
    pub trait IndexedRangeInteger: RangeInteger {
101
        private_decl! {}
102
103
        fn drive<C>(iter: Iter<Self>, consumer: C) -> C::Result
104
        where
105
            C: Consumer<Self>;
106
107
        fn len(iter: &Iter<Self>) -> usize;
108
109
        fn with_producer<CB>(iter: Iter<Self>, callback: CB) -> CB::Output
110
        where
111
            CB: ProducerCallback<Self>;
112
    }
113
}
114
use private::{IndexedRangeInteger, RangeInteger};
115
116
impl<T: RangeInteger> ParallelIterator for Iter<T> {
117
    type Item = T;
118
119
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
120
0
    where
121
0
        C: UnindexedConsumer<T>,
122
    {
123
0
        T::drive_unindexed(self, consumer)
124
0
    }
125
126
    #[inline]
127
0
    fn opt_len(&self) -> Option<usize> {
128
0
        T::opt_len(self)
129
0
    }
Unexecuted instantiation: <rayon::range::Iter<i8> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<u8> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<isize> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<usize> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<i32> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<u32> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<i128> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<u128> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<i16> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<u16> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<i64> as rayon::iter::ParallelIterator>::opt_len
Unexecuted instantiation: <rayon::range::Iter<u64> as rayon::iter::ParallelIterator>::opt_len
130
}
131
132
impl<T: IndexedRangeInteger> IndexedParallelIterator for Iter<T> {
133
0
    fn drive<C>(self, consumer: C) -> C::Result
134
0
    where
135
0
        C: Consumer<T>,
136
    {
137
0
        T::drive(self, consumer)
138
0
    }
139
140
    #[inline]
141
0
    fn len(&self) -> usize {
142
0
        T::len(self)
143
0
    }
144
145
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
146
0
    where
147
0
        CB: ProducerCallback<T>,
148
    {
149
0
        T::with_producer(self, callback)
150
0
    }
151
}
152
153
macro_rules! indexed_range_impl {
154
    ( $t:ty ) => {
155
        impl RangeInteger for $t {
156
            private_impl! {}
157
158
0
            fn drive_unindexed<C>(iter: Iter<$t>, consumer: C) -> C::Result
159
0
            where
160
0
                C: UnindexedConsumer<$t>,
161
            {
162
0
                bridge(iter, consumer)
163
0
            }
Unexecuted instantiation: <u8 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <u16 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <u32 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <usize as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <i8 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <i16 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <i32 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <isize as rayon::range::private::RangeInteger>::drive_unindexed::<_>
164
165
0
            fn opt_len(iter: &Iter<$t>) -> Option<usize> {
166
0
                Some(iter.range.len())
167
0
            }
Unexecuted instantiation: <u8 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <u16 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <u32 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <usize as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <i8 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <i16 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <i32 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <isize as rayon::range::private::RangeInteger>::opt_len
168
        }
169
170
        impl IndexedRangeInteger for $t {
171
            private_impl! {}
172
173
0
            fn drive<C>(iter: Iter<$t>, consumer: C) -> C::Result
174
0
            where
175
0
                C: Consumer<$t>,
176
            {
177
0
                bridge(iter, consumer)
178
0
            }
Unexecuted instantiation: <u8 as rayon::range::private::IndexedRangeInteger>::drive::<_>
Unexecuted instantiation: <u16 as rayon::range::private::IndexedRangeInteger>::drive::<_>
Unexecuted instantiation: <u32 as rayon::range::private::IndexedRangeInteger>::drive::<_>
Unexecuted instantiation: <usize as rayon::range::private::IndexedRangeInteger>::drive::<_>
Unexecuted instantiation: <i8 as rayon::range::private::IndexedRangeInteger>::drive::<_>
Unexecuted instantiation: <i16 as rayon::range::private::IndexedRangeInteger>::drive::<_>
Unexecuted instantiation: <i32 as rayon::range::private::IndexedRangeInteger>::drive::<_>
Unexecuted instantiation: <isize as rayon::range::private::IndexedRangeInteger>::drive::<_>
179
180
0
            fn len(iter: &Iter<$t>) -> usize {
181
0
                iter.range.len()
182
0
            }
Unexecuted instantiation: <u8 as rayon::range::private::IndexedRangeInteger>::len
Unexecuted instantiation: <u16 as rayon::range::private::IndexedRangeInteger>::len
Unexecuted instantiation: <u32 as rayon::range::private::IndexedRangeInteger>::len
Unexecuted instantiation: <usize as rayon::range::private::IndexedRangeInteger>::len
Unexecuted instantiation: <i8 as rayon::range::private::IndexedRangeInteger>::len
Unexecuted instantiation: <i16 as rayon::range::private::IndexedRangeInteger>::len
Unexecuted instantiation: <i32 as rayon::range::private::IndexedRangeInteger>::len
Unexecuted instantiation: <isize as rayon::range::private::IndexedRangeInteger>::len
183
184
0
            fn with_producer<CB>(iter: Iter<$t>, callback: CB) -> CB::Output
185
0
            where
186
0
                CB: ProducerCallback<$t>,
187
            {
188
0
                callback.callback(IterProducer { range: iter.range })
189
0
            }
Unexecuted instantiation: <u8 as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
Unexecuted instantiation: <u16 as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
Unexecuted instantiation: <u32 as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
Unexecuted instantiation: <usize as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
Unexecuted instantiation: <i8 as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
Unexecuted instantiation: <i16 as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
Unexecuted instantiation: <i32 as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
Unexecuted instantiation: <isize as rayon::range::private::IndexedRangeInteger>::with_producer::<_>
190
        }
191
192
        impl Producer for IterProducer<$t> {
193
            type Item = <Range<$t> as Iterator>::Item;
194
            type IntoIter = Range<$t>;
195
0
            fn into_iter(self) -> Self::IntoIter {
196
0
                self.range
197
0
            }
Unexecuted instantiation: <rayon::range::IterProducer<u8> as rayon::iter::plumbing::Producer>::into_iter
Unexecuted instantiation: <rayon::range::IterProducer<u16> as rayon::iter::plumbing::Producer>::into_iter
Unexecuted instantiation: <rayon::range::IterProducer<u32> as rayon::iter::plumbing::Producer>::into_iter
Unexecuted instantiation: <rayon::range::IterProducer<usize> as rayon::iter::plumbing::Producer>::into_iter
Unexecuted instantiation: <rayon::range::IterProducer<i8> as rayon::iter::plumbing::Producer>::into_iter
Unexecuted instantiation: <rayon::range::IterProducer<i16> as rayon::iter::plumbing::Producer>::into_iter
Unexecuted instantiation: <rayon::range::IterProducer<i32> as rayon::iter::plumbing::Producer>::into_iter
Unexecuted instantiation: <rayon::range::IterProducer<isize> as rayon::iter::plumbing::Producer>::into_iter
198
199
0
            fn split_at(self, index: usize) -> (Self, Self) {
200
0
                assert!(index <= self.range.len());
201
                // For signed $t, the length and requested index could be greater than $t::MAX, and
202
                // then `index as $t` could wrap to negative, so wrapping_add is necessary.
203
0
                let mid = self.range.start.wrapping_add(index as $t);
204
0
                let left = self.range.start..mid;
205
0
                let right = mid..self.range.end;
206
0
                (IterProducer { range: left }, IterProducer { range: right })
207
0
            }
Unexecuted instantiation: <rayon::range::IterProducer<u8> as rayon::iter::plumbing::Producer>::split_at
Unexecuted instantiation: <rayon::range::IterProducer<u16> as rayon::iter::plumbing::Producer>::split_at
Unexecuted instantiation: <rayon::range::IterProducer<u32> as rayon::iter::plumbing::Producer>::split_at
Unexecuted instantiation: <rayon::range::IterProducer<usize> as rayon::iter::plumbing::Producer>::split_at
Unexecuted instantiation: <rayon::range::IterProducer<i8> as rayon::iter::plumbing::Producer>::split_at
Unexecuted instantiation: <rayon::range::IterProducer<i16> as rayon::iter::plumbing::Producer>::split_at
Unexecuted instantiation: <rayon::range::IterProducer<i32> as rayon::iter::plumbing::Producer>::split_at
Unexecuted instantiation: <rayon::range::IterProducer<isize> as rayon::iter::plumbing::Producer>::split_at
208
        }
209
    };
210
}
211
212
trait UnindexedRangeLen<L> {
213
    fn unindexed_len(&self) -> L;
214
}
215
216
macro_rules! unindexed_range_impl {
217
    ( $t:ty, $len_t:ty ) => {
218
        impl UnindexedRangeLen<$len_t> for Range<$t> {
219
0
            fn unindexed_len(&self) -> $len_t {
220
0
                let &Range { start, end } = self;
221
0
                if end > start {
222
0
                    end.wrapping_sub(start) as $len_t
223
                } else {
224
0
                    0
225
                }
226
0
            }
Unexecuted instantiation: <core::ops::range::Range<u128> as rayon::range::UnindexedRangeLen<u128>>::unindexed_len
Unexecuted instantiation: <core::ops::range::Range<i128> as rayon::range::UnindexedRangeLen<u128>>::unindexed_len
Unexecuted instantiation: <core::ops::range::Range<u64> as rayon::range::UnindexedRangeLen<u64>>::unindexed_len
Unexecuted instantiation: <core::ops::range::Range<i64> as rayon::range::UnindexedRangeLen<u64>>::unindexed_len
227
        }
228
229
        impl RangeInteger for $t {
230
            private_impl! {}
231
232
0
            fn drive_unindexed<C>(iter: Iter<$t>, consumer: C) -> C::Result
233
0
            where
234
0
                C: UnindexedConsumer<$t>,
235
            {
236
                #[inline]
237
0
                fn offset(start: $t) -> impl Fn(usize) -> $t {
238
0
                    move |i| start.wrapping_add(i as $t)
Unexecuted instantiation: <u128 as rayon::range::private::RangeInteger>::drive_unindexed::offset::{closure#0}
Unexecuted instantiation: <i128 as rayon::range::private::RangeInteger>::drive_unindexed::offset::{closure#0}
Unexecuted instantiation: <u64 as rayon::range::private::RangeInteger>::drive_unindexed::offset::{closure#0}
Unexecuted instantiation: <i64 as rayon::range::private::RangeInteger>::drive_unindexed::offset::{closure#0}
239
0
                }
Unexecuted instantiation: <u128 as rayon::range::private::RangeInteger>::drive_unindexed::offset
Unexecuted instantiation: <i128 as rayon::range::private::RangeInteger>::drive_unindexed::offset
Unexecuted instantiation: <u64 as rayon::range::private::RangeInteger>::drive_unindexed::offset
Unexecuted instantiation: <i64 as rayon::range::private::RangeInteger>::drive_unindexed::offset
240
241
0
                if let Some(len) = iter.opt_len() {
242
                    // Drive this in indexed mode for better `collect`.
243
0
                    (0..len)
244
0
                        .into_par_iter()
245
0
                        .map(offset(iter.range.start))
246
0
                        .drive(consumer)
247
                } else {
248
0
                    bridge_unindexed(IterProducer { range: iter.range }, consumer)
249
                }
250
0
            }
Unexecuted instantiation: <u128 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <i128 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <u64 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
Unexecuted instantiation: <i64 as rayon::range::private::RangeInteger>::drive_unindexed::<_>
251
252
0
            fn opt_len(iter: &Iter<$t>) -> Option<usize> {
253
0
                usize::try_from(iter.range.unindexed_len()).ok()
254
0
            }
Unexecuted instantiation: <u128 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <i128 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <u64 as rayon::range::private::RangeInteger>::opt_len
Unexecuted instantiation: <i64 as rayon::range::private::RangeInteger>::opt_len
255
        }
256
257
        impl UnindexedProducer for IterProducer<$t> {
258
            type Item = $t;
259
260
0
            fn split(mut self) -> (Self, Option<Self>) {
261
0
                let index = self.range.unindexed_len() / 2;
262
0
                if index > 0 {
263
0
                    let mid = self.range.start.wrapping_add(index as $t);
264
0
                    let right = mid..self.range.end;
265
0
                    self.range.end = mid;
266
0
                    (self, Some(IterProducer { range: right }))
267
                } else {
268
0
                    (self, None)
269
                }
270
0
            }
Unexecuted instantiation: <rayon::range::IterProducer<u128> as rayon::iter::plumbing::UnindexedProducer>::split
Unexecuted instantiation: <rayon::range::IterProducer<i128> as rayon::iter::plumbing::UnindexedProducer>::split
Unexecuted instantiation: <rayon::range::IterProducer<u64> as rayon::iter::plumbing::UnindexedProducer>::split
Unexecuted instantiation: <rayon::range::IterProducer<i64> as rayon::iter::plumbing::UnindexedProducer>::split
271
272
0
            fn fold_with<F>(self, folder: F) -> F
273
0
            where
274
0
                F: Folder<Self::Item>,
275
            {
276
0
                folder.consume_iter(self)
277
0
            }
Unexecuted instantiation: <rayon::range::IterProducer<u128> as rayon::iter::plumbing::UnindexedProducer>::fold_with::<_>
Unexecuted instantiation: <rayon::range::IterProducer<i128> as rayon::iter::plumbing::UnindexedProducer>::fold_with::<_>
Unexecuted instantiation: <rayon::range::IterProducer<u64> as rayon::iter::plumbing::UnindexedProducer>::fold_with::<_>
Unexecuted instantiation: <rayon::range::IterProducer<i64> as rayon::iter::plumbing::UnindexedProducer>::fold_with::<_>
278
        }
279
    };
280
}
281
282
// all Range<T> with ExactSizeIterator
283
indexed_range_impl! {u8}
284
indexed_range_impl! {u16}
285
indexed_range_impl! {u32}
286
indexed_range_impl! {usize}
287
indexed_range_impl! {i8}
288
indexed_range_impl! {i16}
289
indexed_range_impl! {i32}
290
indexed_range_impl! {isize}
291
292
// other Range<T> with just Iterator
293
unindexed_range_impl! {u64, u64}
294
unindexed_range_impl! {i64, u64}
295
unindexed_range_impl! {u128, u128}
296
unindexed_range_impl! {i128, u128}
297
298
// char is special because of the surrogate range hole
299
macro_rules! convert_char {
300
    ( $self:ident . $method:ident ( $( $arg:expr ),* ) ) => {{
301
        let start = $self.range.start as u32;
302
        let end = $self.range.end as u32;
303
        if start < 0xD800 && 0xE000 < end {
304
            // chain the before and after surrogate range fragments
305
            (start..0xD800)
306
                .into_par_iter()
307
                .chain(0xE000..end)
308
0
                .map(|codepoint| unsafe { char::from_u32_unchecked(codepoint) })
Unexecuted instantiation: <rayon::range::Iter<char> as rayon::iter::ParallelIterator>::drive_unindexed::<_>::{closure#0}
Unexecuted instantiation: <rayon::range::Iter<char> as rayon::iter::IndexedParallelIterator>::with_producer::<_>::{closure#0}
Unexecuted instantiation: <rayon::range::Iter<char> as rayon::iter::IndexedParallelIterator>::drive::<_>::{closure#0}
309
                .$method($( $arg ),*)
310
        } else {
311
            // no surrogate range to worry about
312
            (start..end)
313
                .into_par_iter()
314
0
                .map(|codepoint| unsafe { char::from_u32_unchecked(codepoint) })
Unexecuted instantiation: <rayon::range::Iter<char> as rayon::iter::ParallelIterator>::drive_unindexed::<_>::{closure#1}
Unexecuted instantiation: <rayon::range::Iter<char> as rayon::iter::IndexedParallelIterator>::with_producer::<_>::{closure#1}
Unexecuted instantiation: <rayon::range::Iter<char> as rayon::iter::IndexedParallelIterator>::drive::<_>::{closure#1}
315
                .$method($( $arg ),*)
316
        }
317
    }};
318
}
319
320
impl ParallelIterator for Iter<char> {
321
    type Item = char;
322
323
0
    fn drive_unindexed<C>(self, consumer: C) -> C::Result
324
0
    where
325
0
        C: UnindexedConsumer<Self::Item>,
326
    {
327
0
        convert_char!(self.drive(consumer))
328
0
    }
329
330
0
    fn opt_len(&self) -> Option<usize> {
331
0
        Some(self.len())
332
0
    }
333
}
334
335
impl IndexedParallelIterator for Iter<char> {
336
    // Split at the surrogate range first if we're allowed to
337
0
    fn drive<C>(self, consumer: C) -> C::Result
338
0
    where
339
0
        C: Consumer<Self::Item>,
340
    {
341
0
        convert_char!(self.drive(consumer))
342
0
    }
343
344
0
    fn len(&self) -> usize {
345
        // Taken from <char as Step>::steps_between
346
0
        let start = self.range.start as u32;
347
0
        let end = self.range.end as u32;
348
0
        if start < end {
349
0
            let mut count = end - start;
350
0
            if start < 0xD800 && 0xE000 <= end {
351
0
                count -= 0x800
352
0
            }
353
0
            count as usize
354
        } else {
355
0
            0
356
        }
357
0
    }
358
359
0
    fn with_producer<CB>(self, callback: CB) -> CB::Output
360
0
    where
361
0
        CB: ProducerCallback<Self::Item>,
362
    {
363
0
        convert_char!(self.with_producer(callback))
364
0
    }
365
}
366
367
#[test]
368
fn check_range_split_at_overflow() {
369
    // Note, this split index overflows i8!
370
    let producer = IterProducer { range: -100i8..100 };
371
    let (left, right) = producer.split_at(150);
372
    let r1: i32 = left.range.map(i32::from).sum();
373
    let r2: i32 = right.range.map(i32::from).sum();
374
    assert_eq!(r1 + r2, -100);
375
}
376
377
#[test]
378
fn test_i128_len_doesnt_overflow() {
379
    // Using parse because some versions of rust don't allow long literals
380
    let octillion: i128 = "1000000000000000000000000000".parse().unwrap();
381
    let producer = IterProducer {
382
        range: 0..octillion,
383
    };
384
385
    assert_eq!(octillion as u128, producer.range.unindexed_len());
386
    assert_eq!(octillion as u128, (0..octillion).unindexed_len());
387
    assert_eq!(
388
        2 * octillion as u128,
389
        (-octillion..octillion).unindexed_len()
390
    );
391
392
    assert_eq!(u128::MAX, (i128::MIN..i128::MAX).unindexed_len());
393
}
394
395
#[test]
396
fn test_u64_opt_len() {
397
    assert_eq!(Some(100), (0..100u64).into_par_iter().opt_len());
398
    assert_eq!(
399
        Some(usize::MAX),
400
        (0..usize::MAX as u64).into_par_iter().opt_len()
401
    );
402
    if (usize::MAX as u64) < u64::MAX {
403
        assert_eq!(
404
            None,
405
            (0..(usize::MAX as u64).wrapping_add(1))
406
                .into_par_iter()
407
                .opt_len()
408
        );
409
        assert_eq!(None, (0..u64::MAX).into_par_iter().opt_len());
410
    }
411
}
412
413
#[test]
414
fn test_u128_opt_len() {
415
    assert_eq!(Some(100), (0..100u128).into_par_iter().opt_len());
416
    assert_eq!(
417
        Some(usize::MAX),
418
        (0..usize::MAX as u128).into_par_iter().opt_len()
419
    );
420
    assert_eq!(None, (0..1 + usize::MAX as u128).into_par_iter().opt_len());
421
    assert_eq!(None, (0..u128::MAX).into_par_iter().opt_len());
422
}
423
424
// `usize as i64` can overflow, so make sure to wrap it appropriately
425
// when using the `opt_len` "indexed" mode.
426
#[test]
427
#[cfg(target_pointer_width = "64")]
428
fn test_usize_i64_overflow() {
429
    use crate::ThreadPoolBuilder;
430
431
    let iter = (-2..i64::MAX).into_par_iter();
432
    assert_eq!(iter.opt_len(), Some(i64::MAX as usize + 2));
433
434
    // always run with multiple threads to split into, or this will take forever...
435
    let pool = ThreadPoolBuilder::new().num_threads(8).build().unwrap();
436
    pool.install(|| assert_eq!(iter.find_last(|_| true), Some(i64::MAX - 1)));
437
}
438
439
#[test]
440
fn test_issue_833() {
441
    fn is_even(n: i64) -> bool {
442
        n % 2 == 0
443
    }
444
445
    // The integer type should be inferred from `is_even`
446
    let v: Vec<_> = (1..100).into_par_iter().filter(|&x| is_even(x)).collect();
447
    assert!(v.into_iter().eq((2..100).step_by(2)));
448
449
    // Try examples with indexed iterators too
450
    let pos = (0..100).into_par_iter().position_any(|x| x == 50i16);
451
    assert_eq!(pos, Some(50usize));
452
453
    assert!((0..100)
454
        .into_par_iter()
455
        .zip(0..100)
456
        .all(|(a, b)| i16::eq(&a, &b)));
457
}