Debian Codegraph @

debian/rust-vendor/rayon/src/iter/flat_map_iter.rs

raw
1 use super::plumbing::*;
2 use super::*;
3
4 use std::fmt::{self, Debug};
5
6 /// `FlatMapIter` maps each element to a serial iterator, then flattens these iterators together.
7 /// This struct is created by the [`flat_map_iter()`] method on [`ParallelIterator`]
8 ///
9 /// [`flat_map_iter()`]: ParallelIterator::flat_map_iter()
10 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
11 #[derive(Clone)]
12 pub struct FlatMapIter<I, F> {
13 base: I,
14 map_op: F,
15 }
16
17 impl<I: Debug, F> Debug for FlatMapIter<I, F> {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 f.debug_struct("FlatMapIter")
20 .field("base", &self.base)
21 .finish()
22 }
23 }
24
25 impl<I, F> FlatMapIter<I, F> {
26 /// Creates a new `FlatMapIter` iterator.
27 pub(super) fn new(base: I, map_op: F) -> Self {
28 FlatMapIter { base, map_op }
29 }
30 }
31
32 impl<I, F, SI> ParallelIterator for FlatMapIter<I, F>
33 where
34 I: ParallelIterator,
35 F: Fn(I::Item) -> SI + Sync + Send,
36 SI: IntoIterator<Item: Send>,
37 {
38 type Item = SI::Item;
39
40 fn drive_unindexed<C>(self, consumer: C) -> C::Result
41 where
42 C: UnindexedConsumer<Self::Item>,
43 {
44 let consumer = FlatMapIterConsumer::new(consumer, &self.map_op);
45 self.base.drive_unindexed(consumer)
46 }
47 }
48
49 // ////////////////////////////////////////////////////////////////////////
50 // Consumer implementation
51
52 struct FlatMapIterConsumer<'f, C, F> {
53 base: C,
54 map_op: &'f F,
55 }
56
57 impl<'f, C, F> FlatMapIterConsumer<'f, C, F> {
58 fn new(base: C, map_op: &'f F) -> Self {
59 FlatMapIterConsumer { base, map_op }
60 }
61 }
62
63 impl<'f, T, U, C, F> Consumer<T> for FlatMapIterConsumer<'f, C, F>
64 where
65 C: UnindexedConsumer<U::Item>,
66 F: Fn(T) -> U + Sync,
67 U: IntoIterator,
68 {
69 type Folder = FlatMapIterFolder<'f, C::Folder, F>;
70 type Reducer = C::Reducer;
71 type Result = C::Result;
72
73 fn split_at(self, index: usize) -> (Self, Self, C::Reducer) {
74 let (left, right, reducer) = self.base.split_at(index);
75 (
76 FlatMapIterConsumer::new(left, self.map_op),
77 FlatMapIterConsumer::new(right, self.map_op),
78 reducer,
79 )
80 }
81
82 fn into_folder(self) -> Self::Folder {
83 FlatMapIterFolder {
84 base: self.base.into_folder(),
85 map_op: self.map_op,
86 }
87 }
88
89 fn full(&self) -> bool {
90 self.base.full()
91 }
92 }
93
94 impl<'f, T, U, C, F> UnindexedConsumer<T> for FlatMapIterConsumer<'f, C, F>
95 where
96 C: UnindexedConsumer<U::Item>,
97 F: Fn(T) -> U + Sync,
98 U: IntoIterator,
99 {
100 fn split_off_left(&self) -> Self {
101 FlatMapIterConsumer::new(self.base.split_off_left(), self.map_op)
102 }
103
104 fn to_reducer(&self) -> Self::Reducer {
105 self.base.to_reducer()
106 }
107 }
108
109 struct FlatMapIterFolder<'f, C, F> {
110 base: C,
111 map_op: &'f F,
112 }
113
114 impl<'f, T, U, C, F> Folder<T> for FlatMapIterFolder<'f, C, F>
115 where
116 C: Folder<U::Item>,
117 F: Fn(T) -> U,
118 U: IntoIterator,
119 {
120 type Result = C::Result;
121
122 fn consume(self, item: T) -> Self {
123 let map_op = self.map_op;
124 let base = self.base.consume_iter(map_op(item));
125 FlatMapIterFolder { base, map_op }
126 }
127
128 fn consume_iter<I>(self, iter: I) -> Self
129 where
130 I: IntoIterator<Item = T>,
131 {
132 let map_op = self.map_op;
133 let iter = iter.into_iter().flat_map(map_op);
134 let base = self.base.consume_iter(iter);
135 FlatMapIterFolder { base, map_op }
136 }
137
138 fn complete(self) -> Self::Result {
139 self.base.complete()
140 }
141
142 fn full(&self) -> bool {
143 self.base.full()
144 }
145 }