rayon/iter/
take_any_while.rs1use super::plumbing::*;
2use super::*;
3use std::fmt;
4use std::sync::atomic::{AtomicBool, Ordering};
5
6#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
12#[derive(Clone)]
13pub struct TakeAnyWhile<I, P> {
14 base: I,
15 predicate: P,
16}
17
18impl<I: fmt::Debug, P> fmt::Debug for TakeAnyWhile<I, P> {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 f.debug_struct("TakeAnyWhile")
21 .field("base", &self.base)
22 .finish()
23 }
24}
25
26impl<I, P> TakeAnyWhile<I, P> {
27 pub(super) fn new(base: I, predicate: P) -> Self {
29 TakeAnyWhile { base, predicate }
30 }
31}
32
33impl<I, P> ParallelIterator for TakeAnyWhile<I, P>
34where
35 I: ParallelIterator,
36 P: Fn(&I::Item) -> bool + Sync + Send,
37{
38 type Item = I::Item;
39
40 fn drive_unindexed<C>(self, consumer: C) -> C::Result
41 where
42 C: UnindexedConsumer<Self::Item>,
43 {
44 let consumer1 = TakeAnyWhileConsumer {
45 base: consumer,
46 predicate: &self.predicate,
47 taking: &AtomicBool::new(true),
48 };
49 self.base.drive_unindexed(consumer1)
50 }
51}
52
53struct TakeAnyWhileConsumer<'p, C, P> {
57 base: C,
58 predicate: &'p P,
59 taking: &'p AtomicBool,
60}
61
62impl<'p, T, C, P> Consumer<T> for TakeAnyWhileConsumer<'p, C, P>
63where
64 C: Consumer<T>,
65 P: Fn(&T) -> bool + Sync,
66{
67 type Folder = TakeAnyWhileFolder<'p, C::Folder, P>;
68 type Reducer = C::Reducer;
69 type Result = C::Result;
70
71 fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
72 let (left, right, reducer) = self.base.split_at(index);
73 (
74 TakeAnyWhileConsumer { base: left, ..self },
75 TakeAnyWhileConsumer {
76 base: right,
77 ..self
78 },
79 reducer,
80 )
81 }
82
83 fn into_folder(self) -> Self::Folder {
84 TakeAnyWhileFolder {
85 base: self.base.into_folder(),
86 predicate: self.predicate,
87 taking: self.taking,
88 }
89 }
90
91 fn full(&self) -> bool {
92 !self.taking.load(Ordering::Relaxed) || self.base.full()
93 }
94}
95
96impl<'p, T, C, P> UnindexedConsumer<T> for TakeAnyWhileConsumer<'p, C, P>
97where
98 C: UnindexedConsumer<T>,
99 P: Fn(&T) -> bool + Sync,
100{
101 fn split_off_left(&self) -> Self {
102 TakeAnyWhileConsumer {
103 base: self.base.split_off_left(),
104 ..*self
105 }
106 }
107
108 fn to_reducer(&self) -> Self::Reducer {
109 self.base.to_reducer()
110 }
111}
112
113struct TakeAnyWhileFolder<'p, C, P> {
114 base: C,
115 predicate: &'p P,
116 taking: &'p AtomicBool,
117}
118
119fn take<T>(item: &T, taking: &AtomicBool, predicate: &impl Fn(&T) -> bool) -> bool {
120 if !taking.load(Ordering::Relaxed) {
121 return false;
122 }
123 if predicate(item) {
124 return true;
125 }
126 taking.store(false, Ordering::Relaxed);
127 false
128}
129
130impl<'p, T, C, P> Folder<T> for TakeAnyWhileFolder<'p, C, P>
131where
132 C: Folder<T>,
133 P: Fn(&T) -> bool + 'p,
134{
135 type Result = C::Result;
136
137 fn consume(mut self, item: T) -> Self {
138 if take(&item, self.taking, self.predicate) {
139 self.base = self.base.consume(item);
140 }
141 self
142 }
143
144 fn consume_iter<I>(mut self, iter: I) -> Self
145 where
146 I: IntoIterator<Item = T>,
147 {
148 self.base = self.base.consume_iter(
149 iter.into_iter()
150 .take_while(move |x| take(x, self.taking, self.predicate)),
151 );
152 self
153 }
154
155 fn complete(self) -> C::Result {
156 self.base.complete()
157 }
158
159 fn full(&self) -> bool {
160 !self.taking.load(Ordering::Relaxed) || self.base.full()
161 }
162}