]>
git.proxmox.com Git - rustc.git/blob - vendor/rustc-rayon/src/iter/try_reduce_with.rs
1 use super::plumbing
::*;
2 use super::ParallelIterator
;
4 use super::private
::Try
;
5 use std
::sync
::atomic
::{AtomicBool, Ordering}
;
7 pub(super) fn try_reduce_with
<PI
, R
, T
>(pi
: PI
, reduce_op
: R
) -> Option
<T
>
9 PI
: ParallelIterator
<Item
= T
>,
10 R
: Fn(T
::Ok
, T
::Ok
) -> T
+ Sync
,
13 let full
= AtomicBool
::new(false);
14 let consumer
= TryReduceWithConsumer
{
15 reduce_op
: &reduce_op
,
18 pi
.drive_unindexed(consumer
)
21 struct TryReduceWithConsumer
<'r
, R
: 'r
> {
26 impl<'r
, R
> Copy
for TryReduceWithConsumer
<'r
, R
> {}
28 impl<'r
, R
> Clone
for TryReduceWithConsumer
<'r
, R
> {
29 fn clone(&self) -> Self {
34 impl<'r
, R
, T
> Consumer
<T
> for TryReduceWithConsumer
<'r
, R
>
36 R
: Fn(T
::Ok
, T
::Ok
) -> T
+ Sync
,
39 type Folder
= TryReduceWithFolder
<'r
, R
, T
>;
41 type Result
= Option
<T
>;
43 fn split_at(self, _index
: usize) -> (Self, Self, Self) {
47 fn into_folder(self) -> Self::Folder
{
49 reduce_op
: self.reduce_op
,
55 fn full(&self) -> bool
{
56 self.full
.load(Ordering
::Relaxed
)
60 impl<'r
, R
, T
> UnindexedConsumer
<T
> for TryReduceWithConsumer
<'r
, R
>
62 R
: Fn(T
::Ok
, T
::Ok
) -> T
+ Sync
,
65 fn split_off_left(&self) -> Self {
69 fn to_reducer(&self) -> Self::Reducer
{
74 impl<'r
, R
, T
> Reducer
<Option
<T
>> for TryReduceWithConsumer
<'r
, R
>
76 R
: Fn(T
::Ok
, T
::Ok
) -> T
+ Sync
,
79 fn reduce(self, left
: Option
<T
>, right
: Option
<T
>) -> Option
<T
> {
80 let reduce_op
= self.reduce_op
;
82 (None
, x
) | (x
, None
) => x
,
83 (Some(a
), Some(b
)) => match (a
.into_result(), b
.into_result()) {
84 (Ok(a
), Ok(b
)) => Some(reduce_op(a
, b
)),
85 (Err(e
), _
) | (_
, Err(e
)) => Some(T
::from_error(e
)),
91 struct TryReduceWithFolder
<'r
, R
: 'r
, T
: Try
> {
93 opt_result
: Option
<Result
<T
::Ok
, T
::Error
>>,
97 impl<'r
, R
, T
> Folder
<T
> for TryReduceWithFolder
<'r
, R
, T
>
99 R
: Fn(T
::Ok
, T
::Ok
) -> T
,
102 type Result
= Option
<T
>;
104 fn consume(self, item
: T
) -> Self {
105 let reduce_op
= self.reduce_op
;
106 let result
= match self.opt_result
{
107 None
=> item
.into_result(),
108 Some(Ok(a
)) => match item
.into_result() {
109 Ok(b
) => reduce_op(a
, b
).into_result(),
112 Some(Err(e
)) => Err(e
),
115 self.full
.store(true, Ordering
::Relaxed
)
117 TryReduceWithFolder
{
118 opt_result
: Some(result
),
123 fn complete(self) -> Option
<T
> {
124 let result
= self.opt_result?
;
126 Ok(ok
) => T
::from_ok(ok
),
127 Err(error
) => T
::from_error(error
),
131 fn full(&self) -> bool
{
132 self.full
.load(Ordering
::Relaxed
)