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