Skip to content

Commit

Permalink
Reduce genericity in more iterator closures
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jul 11, 2019
1 parent 885d932 commit 431c4ba
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 115 deletions.
6 changes: 5 additions & 1 deletion src/iter/find.rs
Expand Up @@ -87,10 +87,14 @@ where
where
I: IntoIterator<Item = T>,
{
fn not_full<T>(found: &AtomicBool) -> impl Fn(&T) -> bool + '_ {
move |_| !found.load(Ordering::Relaxed)
}

self.item = iter
.into_iter()
// stop iterating if another thread has found something
.take_while(|_| !self.full())
.take_while(not_full(&self.found))
.find(self.find_op);
if self.item.is_some() {
self.found.store(true, Ordering::Relaxed)
Expand Down
6 changes: 5 additions & 1 deletion src/iter/flatten.rs
Expand Up @@ -35,6 +35,10 @@ where
where
C: UnindexedConsumer<Self::Item>,
{
self.base.flat_map(|x| x).drive_unindexed(consumer)
fn id<T>(x: T) -> T {
x
}

self.base.flat_map(id).drive_unindexed(consumer)
}
}
9 changes: 8 additions & 1 deletion src/iter/fold.rs
Expand Up @@ -147,11 +147,18 @@ where
where
I: IntoIterator<Item = T>,
{
fn not_full<C, ID, T>(base: &C) -> impl Fn(&T) -> bool + '_
where
C: Folder<ID>,
{
move |_| !base.full()
}

let base = self.base;
let item = iter
.into_iter()
// stop iterating if another thread has finished
.take_while(|_| !base.full())
.take_while(not_full(&base))
.fold(self.item, self.fold_op);

FoldFolder {
Expand Down
2 changes: 1 addition & 1 deletion src/iter/for_each.rs
Expand Up @@ -52,7 +52,7 @@ where
where
I: IntoIterator<Item = T>,
{
iter.into_iter().fold((), |_, item| (self.op)(item));
iter.into_iter().for_each(self.op);
self
}

Expand Down
3 changes: 2 additions & 1 deletion src/iter/from_par_iter.rs
@@ -1,3 +1,4 @@
use super::noop::NoopConsumer;
use super::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};

use std::borrow::Cow;
Expand Down Expand Up @@ -222,6 +223,6 @@ impl FromParallelIterator<()> for () {
where
I: IntoParallelIterator<Item = ()>,
{
par_iter.into_par_iter().for_each(|()| {})
par_iter.into_par_iter().drive_unindexed(NoopConsumer)
}
}
7 changes: 5 additions & 2 deletions src/iter/interleave.rs
Expand Up @@ -205,11 +205,14 @@ where
/// should yield the next element, otherwise, if `j` should yield
/// the next element, set a = index/2 and b = (index/2)+1
fn split_at(self, index: usize) -> (Self, Self) {
#[inline]
fn odd_offset(flag: bool) -> usize {
(!flag) as usize
}

let even = index % 2 == 0;
let idx = index >> 1;

let odd_offset = |flag| if flag { 0 } else { 1 };

// desired split
let (i_idx, j_idx) = (
idx + odd_offset(even || self.i_next),
Expand Down
11 changes: 8 additions & 3 deletions src/iter/map_with.rs
Expand Up @@ -310,10 +310,15 @@ where
where
I: IntoIterator<Item = T>,
{
fn with<'f, T, U, R>(
item: &'f mut U,
map_op: impl Fn(&mut U, T) -> R + 'f,
) -> impl FnMut(T) -> R + 'f {
move |x| map_op(item, x)
}

{
let map_op = self.map_op;
let item = &mut self.item;
let mapped_iter = iter.into_iter().map(|x| map_op(item, x));
let mapped_iter = iter.into_iter().map(with(&mut self.item, self.map_op));
self.base = self.base.consume_iter(mapped_iter);
}
self
Expand Down

0 comments on commit 431c4ba

Please sign in to comment.