1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use iter::*;
use iter::internal::*;
use std::sync::Mutex;
use option;
impl<T: Send, E> IntoParallelIterator for Result<T, E> {
type Item = T;
type Iter = IntoIter<T>;
fn into_par_iter(self) -> Self::Iter {
IntoIter { inner: self.ok().into_par_iter() }
}
}
impl<'a, T: Sync, E> IntoParallelIterator for &'a Result<T, E> {
type Item = &'a T;
type Iter = Iter<'a, T>;
fn into_par_iter(self) -> Self::Iter {
Iter { inner: self.as_ref().ok().into_par_iter() }
}
}
impl<'a, T: Send, E> IntoParallelIterator for &'a mut Result<T, E> {
type Item = &'a mut T;
type Iter = IterMut<'a, T>;
fn into_par_iter(self) -> Self::Iter {
IterMut { inner: self.as_mut().ok().into_par_iter() }
}
}
delegate_indexed_iterator!{
#[doc = "Parallel iterator over a result"]
IntoIter<T> => option::IntoIter<T>,
impl<T: Send>
}
delegate_indexed_iterator!{
#[doc = "Parallel iterator over an immutable reference to a result"]
Iter<'a, T> => option::IntoIter<&'a T>,
impl<'a, T: Sync + 'a>
}
delegate_indexed_iterator!{
#[doc = "Parallel iterator over a mutable reference to a result"]
IterMut<'a, T> => option::IntoIter<&'a mut T>,
impl<'a, T: Send + 'a>
}
impl<'a, C, T, E> FromParallelIterator<Result<T, E>> for Result<C, E>
where C: FromParallelIterator<T>,
T: Send,
E: Send
{
fn from_par_iter<I>(par_iter: I) -> Self
where I: IntoParallelIterator<Item = Result<T, E>>
{
let saved_error = Mutex::new(None);
let collection = par_iter
.into_par_iter()
.map(|item| match item {
Ok(item) => Some(item),
Err(error) => {
if let Ok(mut guard) = saved_error.lock() {
*guard = Some(error);
}
None
}
})
.while_some()
.collect();
match saved_error.into_inner().unwrap() {
Some(error) => Err(error),
None => Ok(collection),
}
}
}