aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/run/iter.rs
blob: d18ad49892a4ddf201fef9d1514e0eef9e03c65f (plain) (blame)
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
use super::FinalRunResult;

pub use iter::*;

pub trait CoalescingRunner<'c> {
    /// Run all the commands in this iterator, and coalesce the results into
    /// one error (if any individual commands failed)
    fn run_and_coalesce(self) -> Vec<FinalRunResult<'c>>;
}

#[cfg(not(feature = "rayon"))]
mod iter {
    use super::*;
    use crate::{argument_parsing::Globals, cargo_command::*, run::run_and_convert};

    pub fn into_iter<T: IntoIterator>(var: T) -> impl Iterator<Item = T::Item> {
        var.into_iter()
    }

    impl<'g, 'c, I> CoalescingRunner<'c> for I
    where
        I: Iterator<Item = (&'g Globals, CargoCommand<'c>, bool)>,
    {
        fn run_and_coalesce(self) -> Vec<FinalRunResult<'c>> {
            self.map(run_and_convert).collect()
        }
    }
}

#[cfg(feature = "rayon")]
mod iter {
    use super::*;
    use crate::{argument_parsing::Globals, cargo_command::*, run::run_and_convert};
    use rayon::prelude::*;

    pub fn into_iter<T: IntoParallelIterator>(var: T) -> impl ParallelIterator<Item = T::Item> {
        var.into_par_iter()
    }

    impl<'g, 'c, I> CoalescingRunner<'c> for I
    where
        I: ParallelIterator<Item = (&'g Globals, CargoCommand<'c>, bool)>,
    {
        fn run_and_coalesce(self) -> Vec<FinalRunResult<'c>> {
            self.map(run_and_convert).collect()
        }
    }
}