Repository: rayon-rs/rayon
Pull request #1023 (2023-02-23 22:28:22)

derive(Clone) for SkipAny and TakeAny


Patch:
diff --git a/src/iter/skip_any.rs b/src/iter/skip_any.rs
index 2ce0a75..0660a56 100644
--- a/src/iter/skip_any.rs
+++ b/src/iter/skip_any.rs
@@ -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>
@@ -20,19 +20,15 @@ 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
@@ -40,7 +36,7 @@ where
     {
         let consumer1 = SkipAnyConsumer {
             base: consumer,
-            count: &self.count,
+            count: &AtomicUsize::new(self.count),
         };
         self.base.drive_unindexed(consumer1)
     }
diff --git a/src/iter/take_any.rs b/src/iter/take_any.rs
index ce4631c..e3992b3 100644
--- a/src/iter/take_any.rs
+++ b/src/iter/take_any.rs
@@ -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>
@@ -20,19 +20,15 @@ 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
@@ -40,7 +36,7 @@ where
     {
         let consumer1 = TakeAnyConsumer {
             base: consumer,
-            count: &self.count,
+            count: &AtomicUsize::new(self.count),
         };
         self.base.drive_unindexed(consumer1)
     }


Test patch:
diff --git a/tests/clones.rs b/tests/clones.rs
index 2b78f09..d600d22 100644
--- a/tests/clones.rs
+++ b/tests/clones.rs
@@ -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;
@@ -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));
@@ -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>());
diff --git a/tests/debug.rs b/tests/debug.rs
index 1cbf4e6..8fe8c64 100644
--- a/tests/debug.rs
+++ b/tests/debug.rs
@@ -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));