Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
1023: derive(Clone) for SkipAny and TakeAny r=cuviper a=cuviper



Co-authored-by: Josh Stone <[email protected]>
  • Loading branch information
bors[bot] and cuviper authored Feb 23, 2023
2 parents 0ceb177 + cf12948 commit a0d0a50
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
18 changes: 7 additions & 11 deletions src/iter/skip_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::sync::atomic::{AtomicUsize, Ordering};
/// [`skip_any()`]: trait.ParallelIterator.html#method.skip_any
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct SkipAny<I: ParallelIterator> {
base: I,
count: AtomicUsize,
count: usize,
}

impl<I> SkipAny<I>
Expand All @@ -20,27 +20,23 @@ where
{
/// Creates a new `SkipAny` iterator.
pub(super) fn new(base: I, count: usize) -> Self {
SkipAny {
base,
count: AtomicUsize::new(count),
}
SkipAny { base, count }
}
}

impl<I, T> ParallelIterator for SkipAny<I>
impl<I> ParallelIterator for SkipAny<I>
where
I: ParallelIterator<Item = T>,
T: Send,
I: ParallelIterator,
{
type Item = T;
type Item = I::Item;

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
let consumer1 = SkipAnyConsumer {
base: consumer,
count: &self.count,
count: &AtomicUsize::new(self.count),
};
self.base.drive_unindexed(consumer1)
}
Expand Down
18 changes: 7 additions & 11 deletions src/iter/take_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::sync::atomic::{AtomicUsize, Ordering};
/// [`take_any()`]: trait.ParallelIterator.html#method.take_any
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct TakeAny<I: ParallelIterator> {
base: I,
count: AtomicUsize,
count: usize,
}

impl<I> TakeAny<I>
Expand All @@ -20,27 +20,23 @@ where
{
/// Creates a new `TakeAny` iterator.
pub(super) fn new(base: I, count: usize) -> Self {
TakeAny {
base,
count: AtomicUsize::new(count),
}
TakeAny { base, count }
}
}

impl<I, T> ParallelIterator for TakeAny<I>
impl<I> ParallelIterator for TakeAny<I>
where
I: ParallelIterator<Item = T>,
T: Send,
I: ParallelIterator,
{
type Item = T;
type Item = I::Item;

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
let consumer1 = TakeAnyConsumer {
base: consumer,
count: &self.count,
count: &AtomicUsize::new(self.count),
};
self.base.drive_unindexed(consumer1)
}
Expand Down
18 changes: 16 additions & 2 deletions tests/clones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ where
assert_eq!(a, b);
}

fn check_count<I>(iter: I)
where
I: ParallelIterator + Clone,
{
assert_eq!(iter.clone().count(), iter.count());
}

#[test]
fn clone_binary_heap() {
use std::collections::BinaryHeap;
Expand Down Expand Up @@ -150,8 +157,8 @@ fn clone_adaptors() {
check(v.par_iter().panic_fuse());
check(v.par_iter().positions(|_| true));
check(v.par_iter().rev());
check(v.par_iter().skip(1));
check(v.par_iter().take(1));
check(v.par_iter().skip(42));
check(v.par_iter().take(42));
check(v.par_iter().cloned().while_some());
check(v.par_iter().with_max_len(1));
check(v.par_iter().with_min_len(1));
Expand All @@ -160,6 +167,13 @@ fn clone_adaptors() {
check(v.par_iter().step_by(2));
}

#[test]
fn clone_counted_adaptors() {
let v: Vec<_> = (0..1000).collect();
check_count(v.par_iter().skip_any(42));
check_count(v.par_iter().take_any(42));
}

#[test]
fn clone_empty() {
check(rayon::iter::empty::<i32>());
Expand Down
2 changes: 2 additions & 0 deletions tests/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ fn debug_adaptors() {
check(v.par_iter().positions(|_| true));
check(v.par_iter().rev());
check(v.par_iter().skip(1));
check(v.par_iter().skip_any(1));
check(v.par_iter().take(1));
check(v.par_iter().take_any(1));
check(v.par_iter().map(Some).while_some());
check(v.par_iter().with_max_len(1));
check(v.par_iter().with_min_len(1));
Expand Down

0 comments on commit a0d0a50

Please sign in to comment.