par_iter/iter/
empty.rs

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