rayon/iter/
empty.rs

1use crate::iter::plumbing::*;
2use crate::iter::*;
3
4use std::fmt;
5use 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/// ```
24pub fn empty<T: Send>() -> Empty<T> {
25    Empty {
26        marker: PhantomData,
27    }
28}
29
30/// Iterator adaptor for [the `empty()` function].
31///
32/// [the `empty()` function]: empty()
33pub struct Empty<T> {
34    marker: PhantomData<T>,
35}
36
37impl<T> Clone for Empty<T> {
38    fn clone(&self) -> Self {
39        Empty {
40            marker: PhantomData,
41        }
42    }
43}
44
45impl<T: Send> fmt::Debug for Empty<T> {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        f.pad("Empty")
48    }
49}
50
51impl<T: Send> ParallelIterator for Empty<T> {
52    type Item = T;
53
54    fn drive_unindexed<C>(self, consumer: C) -> C::Result
55    where
56        C: UnindexedConsumer<Self::Item>,
57    {
58        self.drive(consumer)
59    }
60
61    fn opt_len(&self) -> Option<usize> {
62        Some(0)
63    }
64}
65
66impl<T: Send> IndexedParallelIterator for Empty<T> {
67    fn drive<C>(self, consumer: C) -> C::Result
68    where
69        C: Consumer<Self::Item>,
70    {
71        consumer.into_folder().complete()
72    }
73
74    fn len(&self) -> usize {
75        0
76    }
77
78    fn with_producer<CB>(self, callback: CB) -> CB::Output
79    where
80        CB: ProducerCallback<Self::Item>,
81    {
82        callback.callback(EmptyProducer(PhantomData))
83    }
84}
85
86/// Private empty producer
87struct EmptyProducer<T: Send>(PhantomData<T>);
88
89impl<T: Send> Producer for EmptyProducer<T> {
90    type Item = T;
91    type IntoIter = std::iter::Empty<T>;
92
93    fn into_iter(self) -> Self::IntoIter {
94        std::iter::empty()
95    }
96
97    fn split_at(self, index: usize) -> (Self, Self) {
98        debug_assert_eq!(index, 0);
99        (self, EmptyProducer(PhantomData))
100    }
101
102    fn fold_with<F>(self, folder: F) -> F
103    where
104        F: Folder<Self::Item>,
105    {
106        folder
107    }
108}