| 1 |
use crate::iter::plumbing::*; |
| 2 |
use crate::iter::*; |
| 3 |
|
| 4 |
use std::fmt; |
| 5 |
use std::marker::PhantomData; |
| 6 |
|
| 7 |
/// Creates a parallel iterator that produces nothing. |
| 8 |
/// |
| 9 |
/// This admits no parallelism on its own, but it could be used for code that |
| 10 |
/// deals with generic parallel iterators. |
| 11 |
/// |
| 12 |
/// # Examples |
| 13 |
/// |
| 14 |
/// ``` |
| 15 |
/// use rayon::prelude::*; |
| 16 |
/// use rayon::iter::empty; |
| 17 |
/// |
| 18 |
/// let pi = (0..1234).into_par_iter() |
| 19 |
/// .chain(empty()) |
| 20 |
/// .chain(1234..10_000); |
| 21 |
/// |
| 22 |
/// assert_eq!(pi.count(), 10_000); |
| 23 |
/// ``` |
| 24 |
pub fn empty<T: Send>() -> Empty<T> { |
| 25 |
Empty { |
| 26 |
marker: PhantomData, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/// Iterator adaptor for [the `empty()` function](fn.empty.html). |
| 31 |
pub struct Empty<T: Send> { |
| 32 |
marker: PhantomData<T>, |
| 33 |
} |
| 34 |
|
| 35 |
impl<T: Send> Clone for Empty<T> { |
| 36 |
fn clone(&self) -> Self { |
| 37 |
empty() |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl<T: Send> fmt::Debug for Empty<T> { |
| 42 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 43 |
f.pad("Empty") |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl<T: Send> ParallelIterator for Empty<T> { |
| 48 |
type Item = T; |
| 49 |
|
| 50 |
fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 51 |
where |
| 52 |
C: UnindexedConsumer<Self::Item>, |
| 53 |
{ |
| 54 |
self.drive(consumer) |
| 55 |
} |
| 56 |
|
| 57 |
fn opt_len(&self) -> Option<usize> { |
| 58 |
Some(0) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl<T: Send> IndexedParallelIterator for Empty<T> { |
| 63 |
fn drive<C>(self, consumer: C) -> C::Result |
| 64 |
where |
| 65 |
C: Consumer<Self::Item>, |
| 66 |
{ |
| 67 |
consumer.into_folder().complete() |
| 68 |
} |
| 69 |
|
| 70 |
fn len(&self) -> usize { |
| 71 |
0 |
| 72 |
} |
| 73 |
|
| 74 |
fn with_producer<CB>(self, callback: CB) -> CB::Output |
| 75 |
where |
| 76 |
CB: ProducerCallback<Self::Item>, |
| 77 |
{ |
| 78 |
callback.callback(EmptyProducer(PhantomData)) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/// Private empty producer |
| 83 |
struct EmptyProducer<T: Send>(PhantomData<T>); |
| 84 |
|
| 85 |
impl<T: Send> Producer for EmptyProducer<T> { |
| 86 |
type Item = T; |
| 87 |
type IntoIter = std::iter::Empty<T>; |
| 88 |
|
| 89 |
fn into_iter(self) -> Self::IntoIter { |
| 90 |
std::iter::empty() |
| 91 |
} |
| 92 |
|
| 93 |
fn split_at(self, index: usize) -> (Self, Self) { |
| 94 |
debug_assert_eq!(index, 0); |
| 95 |
(self, EmptyProducer(PhantomData)) |
| 96 |
} |
| 97 |
|
| 98 |
fn fold_with<F>(self, folder: F) -> F |
| 99 |
where |
| 100 |
F: Folder<Self::Item>, |
| 101 |
{ |
| 102 |
folder |
| 103 |
} |
| 104 |
} |