Skip to content

Commit 97cc314

Browse files
committed
Reduce genericity in more iterator closures
1 parent ba41dab commit 97cc314

19 files changed

+259
-115
lines changed

src/iter/find.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ where
8787
where
8888
I: IntoIterator<Item = T>,
8989
{
90+
fn not_full<T>(found: &AtomicBool) -> impl Fn(&T) -> bool + '_ {
91+
move |_| !found.load(Ordering::Relaxed)
92+
}
93+
9094
self.item = iter
9195
.into_iter()
9296
// stop iterating if another thread has found something
93-
.take_while(|_| !self.full())
97+
.take_while(not_full(&self.found))
9498
.find(self.find_op);
9599
if self.item.is_some() {
96100
self.found.store(true, Ordering::Relaxed)

src/iter/flatten.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ where
3535
where
3636
C: UnindexedConsumer<Self::Item>,
3737
{
38-
self.base.flat_map(|x| x).drive_unindexed(consumer)
38+
fn id<T>(x: T) -> T {
39+
x
40+
}
41+
42+
self.base.flat_map(id).drive_unindexed(consumer)
3943
}
4044
}

src/iter/fold.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,18 @@ where
147147
where
148148
I: IntoIterator<Item = T>,
149149
{
150+
fn not_full<C, ID, T>(base: &C) -> impl Fn(&T) -> bool + '_
151+
where
152+
C: Folder<ID>,
153+
{
154+
move |_| !base.full()
155+
}
156+
150157
let base = self.base;
151158
let item = iter
152159
.into_iter()
153160
// stop iterating if another thread has finished
154-
.take_while(|_| !base.full())
161+
.take_while(not_full(&base))
155162
.fold(self.item, self.fold_op);
156163

157164
FoldFolder {

src/iter/for_each.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ where
5252
where
5353
I: IntoIterator<Item = T>,
5454
{
55-
iter.into_iter().fold((), |_, item| (self.op)(item));
55+
iter.into_iter().for_each(self.op);
5656
self
5757
}
5858

src/iter/from_par_iter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::noop::NoopConsumer;
12
use super::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
23

34
use std::borrow::Cow;
@@ -222,6 +223,6 @@ impl FromParallelIterator<()> for () {
222223
where
223224
I: IntoParallelIterator<Item = ()>,
224225
{
225-
par_iter.into_par_iter().for_each(|()| {})
226+
par_iter.into_par_iter().drive_unindexed(NoopConsumer)
226227
}
227228
}

src/iter/interleave.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,14 @@ where
205205
/// should yield the next element, otherwise, if `j` should yield
206206
/// the next element, set a = index/2 and b = (index/2)+1
207207
fn split_at(self, index: usize) -> (Self, Self) {
208+
#[inline]
209+
fn odd_offset(flag: bool) -> usize {
210+
(!flag) as usize
211+
}
212+
208213
let even = index % 2 == 0;
209214
let idx = index >> 1;
210215

211-
let odd_offset = |flag| if flag { 0 } else { 1 };
212-
213216
// desired split
214217
let (i_idx, j_idx) = (
215218
idx + odd_offset(even || self.i_next),

src/iter/map_with.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,15 @@ where
310310
where
311311
I: IntoIterator<Item = T>,
312312
{
313+
fn with<'f, T, U, R>(
314+
item: &'f mut U,
315+
map_op: impl Fn(&mut U, T) -> R + 'f,
316+
) -> impl FnMut(T) -> R + 'f {
317+
move |x| map_op(item, x)
318+
}
319+
313320
{
314-
let map_op = self.map_op;
315-
let item = &mut self.item;
316-
let mapped_iter = iter.into_iter().map(|x| map_op(item, x));
321+
let mapped_iter = iter.into_iter().map(with(&mut self.item, self.map_op));
317322
self.base = self.base.consume_iter(mapped_iter);
318323
}
319324
self

0 commit comments

Comments
 (0)