Coverage Report

Created: 2025-02-21 07:11

/rust/registry/src/index.crates.io-6f17d22bba15001f/rayon-1.10.0/src/result.rs
Line
Count
Source (jump to first uncovered line)
1
//! Parallel iterator types for [results][std::result]
2
//!
3
//! You will rarely need to interact with this module directly unless you need
4
//! to name one of the iterator types.
5
//!
6
//! [std::result]: https://doc.rust-lang.org/stable/std/result/
7
8
use crate::iter::plumbing::*;
9
use crate::iter::*;
10
use std::sync::Mutex;
11
12
use crate::option;
13
14
/// Parallel iterator over a result
15
#[derive(Debug, Clone)]
16
pub struct IntoIter<T: Send> {
17
    inner: option::IntoIter<T>,
18
}
19
20
impl<T: Send, E> IntoParallelIterator for Result<T, E> {
21
    type Item = T;
22
    type Iter = IntoIter<T>;
23
24
0
    fn into_par_iter(self) -> Self::Iter {
25
0
        IntoIter {
26
0
            inner: self.ok().into_par_iter(),
27
0
        }
28
0
    }
29
}
30
31
delegate_indexed_iterator! {
32
    IntoIter<T> => T,
33
    impl<T: Send>
34
}
35
36
/// Parallel iterator over an immutable reference to a result
37
#[derive(Debug)]
38
pub struct Iter<'a, T: Sync> {
39
    inner: option::IntoIter<&'a T>,
40
}
41
42
impl<'a, T: Sync> Clone for Iter<'a, T> {
43
0
    fn clone(&self) -> Self {
44
0
        Iter {
45
0
            inner: self.inner.clone(),
46
0
        }
47
0
    }
48
}
49
50
impl<'a, T: Sync, E> IntoParallelIterator for &'a Result<T, E> {
51
    type Item = &'a T;
52
    type Iter = Iter<'a, T>;
53
54
0
    fn into_par_iter(self) -> Self::Iter {
55
0
        Iter {
56
0
            inner: self.as_ref().ok().into_par_iter(),
57
0
        }
58
0
    }
59
}
60
61
delegate_indexed_iterator! {
62
    Iter<'a, T> => &'a T,
63
    impl<'a, T: Sync + 'a>
64
}
65
66
/// Parallel iterator over a mutable reference to a result
67
#[derive(Debug)]
68
pub struct IterMut<'a, T: Send> {
69
    inner: option::IntoIter<&'a mut T>,
70
}
71
72
impl<'a, T: Send, E> IntoParallelIterator for &'a mut Result<T, E> {
73
    type Item = &'a mut T;
74
    type Iter = IterMut<'a, T>;
75
76
0
    fn into_par_iter(self) -> Self::Iter {
77
0
        IterMut {
78
0
            inner: self.as_mut().ok().into_par_iter(),
79
0
        }
80
0
    }
81
}
82
83
delegate_indexed_iterator! {
84
    IterMut<'a, T> => &'a mut T,
85
    impl<'a, T: Send + 'a>
86
}
87
88
/// Collect an arbitrary `Result`-wrapped collection.
89
///
90
/// If any item is `Err`, then all previous `Ok` items collected are
91
/// discarded, and it returns that error.  If there are multiple errors, the
92
/// one returned is not deterministic.
93
impl<C, T, E> FromParallelIterator<Result<T, E>> for Result<C, E>
94
where
95
    C: FromParallelIterator<T>,
96
    T: Send,
97
    E: Send,
98
{
99
0
    fn from_par_iter<I>(par_iter: I) -> Self
100
0
    where
101
0
        I: IntoParallelIterator<Item = Result<T, E>>,
102
0
    {
103
0
        fn ok<T, E>(saved: &Mutex<Option<E>>) -> impl Fn(Result<T, E>) -> Option<T> + '_ {
104
0
            move |item| match item {
105
0
                Ok(item) => Some(item),
106
0
                Err(error) => {
107
                    // We don't need a blocking `lock()`, as anybody
108
                    // else holding the lock will also be writing
109
                    // `Some(error)`, and then ours is irrelevant.
110
0
                    if let Ok(mut guard) = saved.try_lock() {
111
0
                        if guard.is_none() {
112
0
                            *guard = Some(error);
113
0
                        }
114
0
                    }
115
0
                    None
116
                }
117
0
            }
118
0
        }
119
120
0
        let saved_error = Mutex::new(None);
121
0
        let collection = par_iter
122
0
            .into_par_iter()
123
0
            .map(ok(&saved_error))
124
0
            .while_some()
125
0
            .collect();
126
0
127
0
        match saved_error.into_inner().unwrap() {
128
0
            Some(error) => Err(error),
129
0
            None => Ok(collection),
130
        }
131
0
    }
132
}