File tree Expand file tree Collapse file tree 4 files changed +32
-24
lines changed Expand file tree Collapse file tree 4 files changed +32
-24
lines changed Original file line number Diff line number Diff line change @@ -8,10 +8,10 @@ use std::sync::atomic::{AtomicUsize, Ordering};
88/// [`skip_any()`]: trait.ParallelIterator.html#method.skip_any
99/// [`ParallelIterator`]: trait.ParallelIterator.html
1010#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
11- #[ derive( Debug ) ]
11+ #[ derive( Clone , Debug ) ]
1212pub struct SkipAny < I : ParallelIterator > {
1313 base : I ,
14- count : AtomicUsize ,
14+ count : usize ,
1515}
1616
1717impl < I > SkipAny < I >
@@ -20,27 +20,23 @@ where
2020{
2121 /// Creates a new `SkipAny` iterator.
2222 pub ( super ) fn new ( base : I , count : usize ) -> Self {
23- SkipAny {
24- base,
25- count : AtomicUsize :: new ( count) ,
26- }
23+ SkipAny { base, count }
2724 }
2825}
2926
30- impl < I , T > ParallelIterator for SkipAny < I >
27+ impl < I > ParallelIterator for SkipAny < I >
3128where
32- I : ParallelIterator < Item = T > ,
33- T : Send ,
29+ I : ParallelIterator ,
3430{
35- type Item = T ;
31+ type Item = I :: Item ;
3632
3733 fn drive_unindexed < C > ( self , consumer : C ) -> C :: Result
3834 where
3935 C : UnindexedConsumer < Self :: Item > ,
4036 {
4137 let consumer1 = SkipAnyConsumer {
4238 base : consumer,
43- count : & self . count ,
39+ count : & AtomicUsize :: new ( self . count ) ,
4440 } ;
4541 self . base . drive_unindexed ( consumer1)
4642 }
Original file line number Diff line number Diff line change @@ -8,10 +8,10 @@ use std::sync::atomic::{AtomicUsize, Ordering};
88/// [`take_any()`]: trait.ParallelIterator.html#method.take_any
99/// [`ParallelIterator`]: trait.ParallelIterator.html
1010#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
11- #[ derive( Debug ) ]
11+ #[ derive( Clone , Debug ) ]
1212pub struct TakeAny < I : ParallelIterator > {
1313 base : I ,
14- count : AtomicUsize ,
14+ count : usize ,
1515}
1616
1717impl < I > TakeAny < I >
@@ -20,27 +20,23 @@ where
2020{
2121 /// Creates a new `TakeAny` iterator.
2222 pub ( super ) fn new ( base : I , count : usize ) -> Self {
23- TakeAny {
24- base,
25- count : AtomicUsize :: new ( count) ,
26- }
23+ TakeAny { base, count }
2724 }
2825}
2926
30- impl < I , T > ParallelIterator for TakeAny < I >
27+ impl < I > ParallelIterator for TakeAny < I >
3128where
32- I : ParallelIterator < Item = T > ,
33- T : Send ,
29+ I : ParallelIterator ,
3430{
35- type Item = T ;
31+ type Item = I :: Item ;
3632
3733 fn drive_unindexed < C > ( self , consumer : C ) -> C :: Result
3834 where
3935 C : UnindexedConsumer < Self :: Item > ,
4036 {
4137 let consumer1 = TakeAnyConsumer {
4238 base : consumer,
43- count : & self . count ,
39+ count : & AtomicUsize :: new ( self . count ) ,
4440 } ;
4541 self . base . drive_unindexed ( consumer1)
4642 }
Original file line number Diff line number Diff line change 1010 assert_eq ! ( a, b) ;
1111}
1212
13+ fn check_count < I > ( iter : I )
14+ where
15+ I : ParallelIterator + Clone ,
16+ {
17+ assert_eq ! ( iter. clone( ) . count( ) , iter. count( ) ) ;
18+ }
19+
1320#[ test]
1421fn clone_binary_heap ( ) {
1522 use std:: collections:: BinaryHeap ;
@@ -150,8 +157,8 @@ fn clone_adaptors() {
150157 check ( v. par_iter ( ) . panic_fuse ( ) ) ;
151158 check ( v. par_iter ( ) . positions ( |_| true ) ) ;
152159 check ( v. par_iter ( ) . rev ( ) ) ;
153- check ( v. par_iter ( ) . skip ( 1 ) ) ;
154- check ( v. par_iter ( ) . take ( 1 ) ) ;
160+ check ( v. par_iter ( ) . skip ( 42 ) ) ;
161+ check ( v. par_iter ( ) . take ( 42 ) ) ;
155162 check ( v. par_iter ( ) . cloned ( ) . while_some ( ) ) ;
156163 check ( v. par_iter ( ) . with_max_len ( 1 ) ) ;
157164 check ( v. par_iter ( ) . with_min_len ( 1 ) ) ;
@@ -160,6 +167,13 @@ fn clone_adaptors() {
160167 check ( v. par_iter ( ) . step_by ( 2 ) ) ;
161168}
162169
170+ #[ test]
171+ fn clone_counted_adaptors ( ) {
172+ let v: Vec < _ > = ( 0 ..1000 ) . collect ( ) ;
173+ check_count ( v. par_iter ( ) . skip_any ( 42 ) ) ;
174+ check_count ( v. par_iter ( ) . take_any ( 42 ) ) ;
175+ }
176+
163177#[ test]
164178fn clone_empty ( ) {
165179 check ( rayon:: iter:: empty :: < i32 > ( ) ) ;
Original file line number Diff line number Diff line change @@ -172,7 +172,9 @@ fn debug_adaptors() {
172172 check ( v. par_iter ( ) . positions ( |_| true ) ) ;
173173 check ( v. par_iter ( ) . rev ( ) ) ;
174174 check ( v. par_iter ( ) . skip ( 1 ) ) ;
175+ check ( v. par_iter ( ) . skip_any ( 1 ) ) ;
175176 check ( v. par_iter ( ) . take ( 1 ) ) ;
177+ check ( v. par_iter ( ) . take_any ( 1 ) ) ;
176178 check ( v. par_iter ( ) . map ( Some ) . while_some ( ) ) ;
177179 check ( v. par_iter ( ) . with_max_len ( 1 ) ) ;
178180 check ( v. par_iter ( ) . with_min_len ( 1 ) ) ;
You can’t perform that action at this time.
0 commit comments