]>
git.proxmox.com Git - rustc.git/blob - src/vendor/rustc-rayon/src/iter/find.rs
1 use std
::sync
::atomic
::{AtomicBool, Ordering}
;
2 use super::plumbing
::*;
5 pub fn find
<I
, P
>(pi
: I
, find_op
: P
) -> Option
<I
::Item
>
6 where I
: ParallelIterator
,
7 P
: Fn(&I
::Item
) -> bool
+ Sync
9 let found
= AtomicBool
::new(false);
10 let consumer
= FindConsumer
::new(&find_op
, &found
);
11 pi
.drive_unindexed(consumer
)
14 struct FindConsumer
<'p
, P
: 'p
> {
16 found
: &'p AtomicBool
,
19 impl<'p
, P
> FindConsumer
<'p
, P
> {
20 fn new(find_op
: &'p P
, found
: &'p AtomicBool
) -> Self {
28 impl<'p
, T
, P
: 'p
> Consumer
<T
> for FindConsumer
<'p
, P
>
30 P
: Fn(&T
) -> bool
+ Sync
32 type Folder
= FindFolder
<'p
, T
, P
>;
33 type Reducer
= FindReducer
;
34 type Result
= Option
<T
>;
36 fn split_at(self, _index
: usize) -> (Self, Self, Self::Reducer
) {
37 (self.split_off_left(), self, FindReducer
)
40 fn into_folder(self) -> Self::Folder
{
42 find_op
: self.find_op
,
48 fn full(&self) -> bool
{
49 self.found
.load(Ordering
::Relaxed
)
54 impl<'p
, T
, P
: 'p
> UnindexedConsumer
<T
> for FindConsumer
<'p
, P
>
56 P
: Fn(&T
) -> bool
+ Sync
58 fn split_off_left(&self) -> Self {
59 FindConsumer
::new(self.find_op
, self.found
)
62 fn to_reducer(&self) -> Self::Reducer
{
68 struct FindFolder
<'p
, T
, P
: 'p
> {
70 found
: &'p AtomicBool
,
74 impl<'p
, T
, P
> Folder
<T
> for FindFolder
<'p
, T
, P
>
75 where P
: Fn(&T
) -> bool
+ 'p
77 type Result
= Option
<T
>;
79 fn consume(mut self, item
: T
) -> Self {
80 if (self.find_op
)(&item
) {
81 self.found
.store(true, Ordering
::Relaxed
);
82 self.item
= Some(item
);
87 fn complete(self) -> Self::Result
{
91 fn full(&self) -> bool
{
92 self.found
.load(Ordering
::Relaxed
)
99 impl<T
> Reducer
<Option
<T
>> for FindReducer
{
100 fn reduce(self, left
: Option
<T
>, right
: Option
<T
>) -> Option
<T
> {