/rust/registry/src/index.crates.io-6f17d22bba15001f/rayon-1.10.0/src/option.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Parallel iterator types for [options][std::option] |
2 | | //! |
3 | | //! You will rarely need to interact with this module directly unless you need |
4 | | //! to name one of the iterator types. |
5 | | //! |
6 | | //! [std::option]: https://doc.rust-lang.org/stable/std/option/ |
7 | | |
8 | | use crate::iter::plumbing::*; |
9 | | use crate::iter::*; |
10 | | use std::sync::atomic::{AtomicBool, Ordering}; |
11 | | |
12 | | /// A parallel iterator over the value in [`Some`] variant of an [`Option`]. |
13 | | /// |
14 | | /// The iterator yields one value if the [`Option`] is a [`Some`], otherwise none. |
15 | | /// |
16 | | /// This `struct` is created by the [`into_par_iter`] function. |
17 | | /// |
18 | | /// [`Option`]: https://doc.rust-lang.org/std/option/enum.Option.html |
19 | | /// [`Some`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some |
20 | | /// [`into_par_iter`]: ../iter/trait.IntoParallelIterator.html#tymethod.into_par_iter |
21 | | #[derive(Debug, Clone)] |
22 | | pub struct IntoIter<T: Send> { |
23 | | opt: Option<T>, |
24 | | } |
25 | | |
26 | | impl<T: Send> IntoParallelIterator for Option<T> { |
27 | | type Item = T; |
28 | | type Iter = IntoIter<T>; |
29 | | |
30 | 0 | fn into_par_iter(self) -> Self::Iter { |
31 | 0 | IntoIter { opt: self } |
32 | 0 | } |
33 | | } |
34 | | |
35 | | impl<T: Send> ParallelIterator for IntoIter<T> { |
36 | | type Item = T; |
37 | | |
38 | 0 | fn drive_unindexed<C>(self, consumer: C) -> C::Result |
39 | 0 | where |
40 | 0 | C: UnindexedConsumer<Self::Item>, |
41 | 0 | { |
42 | 0 | self.drive(consumer) |
43 | 0 | } |
44 | | |
45 | 0 | fn opt_len(&self) -> Option<usize> { |
46 | 0 | Some(self.len()) |
47 | 0 | } |
48 | | } |
49 | | |
50 | | impl<T: Send> IndexedParallelIterator for IntoIter<T> { |
51 | 0 | fn drive<C>(self, consumer: C) -> C::Result |
52 | 0 | where |
53 | 0 | C: Consumer<Self::Item>, |
54 | 0 | { |
55 | 0 | let mut folder = consumer.into_folder(); |
56 | 0 | if let Some(item) = self.opt { |
57 | 0 | folder = folder.consume(item); |
58 | 0 | } |
59 | 0 | folder.complete() |
60 | 0 | } |
61 | | |
62 | 0 | fn len(&self) -> usize { |
63 | 0 | match self.opt { |
64 | 0 | Some(_) => 1, |
65 | 0 | None => 0, |
66 | | } |
67 | 0 | } |
68 | | |
69 | 0 | fn with_producer<CB>(self, callback: CB) -> CB::Output |
70 | 0 | where |
71 | 0 | CB: ProducerCallback<Self::Item>, |
72 | 0 | { |
73 | 0 | callback.callback(OptionProducer { opt: self.opt }) |
74 | 0 | } |
75 | | } |
76 | | |
77 | | /// A parallel iterator over a reference to the [`Some`] variant of an [`Option`]. |
78 | | /// |
79 | | /// The iterator yields one value if the [`Option`] is a [`Some`], otherwise none. |
80 | | /// |
81 | | /// This `struct` is created by the [`par_iter`] function. |
82 | | /// |
83 | | /// [`Option`]: https://doc.rust-lang.org/std/option/enum.Option.html |
84 | | /// [`Some`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some |
85 | | /// [`par_iter`]: ../iter/trait.IntoParallelRefIterator.html#tymethod.par_iter |
86 | | #[derive(Debug)] |
87 | | pub struct Iter<'a, T: Sync> { |
88 | | inner: IntoIter<&'a T>, |
89 | | } |
90 | | |
91 | | impl<'a, T: Sync> Clone for Iter<'a, T> { |
92 | 0 | fn clone(&self) -> Self { |
93 | 0 | Iter { |
94 | 0 | inner: self.inner.clone(), |
95 | 0 | } |
96 | 0 | } |
97 | | } |
98 | | |
99 | | impl<'a, T: Sync> IntoParallelIterator for &'a Option<T> { |
100 | | type Item = &'a T; |
101 | | type Iter = Iter<'a, T>; |
102 | | |
103 | 0 | fn into_par_iter(self) -> Self::Iter { |
104 | 0 | Iter { |
105 | 0 | inner: self.as_ref().into_par_iter(), |
106 | 0 | } |
107 | 0 | } |
108 | | } |
109 | | |
110 | | delegate_indexed_iterator! { |
111 | | Iter<'a, T> => &'a T, |
112 | | impl<'a, T: Sync + 'a> |
113 | | } |
114 | | |
115 | | /// A parallel iterator over a mutable reference to the [`Some`] variant of an [`Option`]. |
116 | | /// |
117 | | /// The iterator yields one value if the [`Option`] is a [`Some`], otherwise none. |
118 | | /// |
119 | | /// This `struct` is created by the [`par_iter_mut`] function. |
120 | | /// |
121 | | /// [`Option`]: https://doc.rust-lang.org/std/option/enum.Option.html |
122 | | /// [`Some`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some |
123 | | /// [`par_iter_mut`]: ../iter/trait.IntoParallelRefMutIterator.html#tymethod.par_iter_mut |
124 | | #[derive(Debug)] |
125 | | pub struct IterMut<'a, T: Send> { |
126 | | inner: IntoIter<&'a mut T>, |
127 | | } |
128 | | |
129 | | impl<'a, T: Send> IntoParallelIterator for &'a mut Option<T> { |
130 | | type Item = &'a mut T; |
131 | | type Iter = IterMut<'a, T>; |
132 | | |
133 | 0 | fn into_par_iter(self) -> Self::Iter { |
134 | 0 | IterMut { |
135 | 0 | inner: self.as_mut().into_par_iter(), |
136 | 0 | } |
137 | 0 | } |
138 | | } |
139 | | |
140 | | delegate_indexed_iterator! { |
141 | | IterMut<'a, T> => &'a mut T, |
142 | | impl<'a, T: Send + 'a> |
143 | | } |
144 | | |
145 | | /// Private producer for an option |
146 | | struct OptionProducer<T: Send> { |
147 | | opt: Option<T>, |
148 | | } |
149 | | |
150 | | impl<T: Send> Producer for OptionProducer<T> { |
151 | | type Item = T; |
152 | | type IntoIter = std::option::IntoIter<T>; |
153 | | |
154 | 0 | fn into_iter(self) -> Self::IntoIter { |
155 | 0 | self.opt.into_iter() |
156 | 0 | } |
157 | | |
158 | 0 | fn split_at(self, index: usize) -> (Self, Self) { |
159 | 0 | debug_assert!(index <= 1); |
160 | 0 | let none = OptionProducer { opt: None }; |
161 | 0 | if index == 0 { |
162 | 0 | (none, self) |
163 | | } else { |
164 | 0 | (self, none) |
165 | | } |
166 | 0 | } |
167 | | } |
168 | | |
169 | | /// Collect an arbitrary `Option`-wrapped collection. |
170 | | /// |
171 | | /// If any item is `None`, then all previous items collected are discarded, |
172 | | /// and it returns only `None`. |
173 | | impl<C, T> FromParallelIterator<Option<T>> for Option<C> |
174 | | where |
175 | | C: FromParallelIterator<T>, |
176 | | T: Send, |
177 | | { |
178 | 0 | fn from_par_iter<I>(par_iter: I) -> Self |
179 | 0 | where |
180 | 0 | I: IntoParallelIterator<Item = Option<T>>, |
181 | 0 | { |
182 | 0 | fn check<T>(found_none: &AtomicBool) -> impl Fn(&Option<T>) + '_ { |
183 | 0 | move |item| { |
184 | 0 | if item.is_none() { |
185 | 0 | found_none.store(true, Ordering::Relaxed); |
186 | 0 | } |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | 0 | let found_none = AtomicBool::new(false); |
191 | 0 | let collection = par_iter |
192 | 0 | .into_par_iter() |
193 | 0 | .inspect(check(&found_none)) |
194 | 0 | .while_some() |
195 | 0 | .collect(); |
196 | 0 |
|
197 | 0 | if found_none.load(Ordering::Relaxed) { |
198 | 0 | None |
199 | | } else { |
200 | 0 | Some(collection) |
201 | | } |
202 | 0 | } |
203 | | } |