flux_common/
iter.rs

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::ops::Try;

use rustc_errors::ErrorGuaranteed;

use crate::result::ErrorCollector;

pub trait IterExt: Iterator {
    fn try_collect_vec<T, E>(self) -> Result<Vec<T>, E>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
    {
        self.collect()
    }

    fn collect_errors<T, E, C>(self, collector: &mut C) -> CollectErrors<Self, C>
    where
        Self: Iterator<Item = Result<T, E>> + Sized,
        C: ErrorCollector<E>,
    {
        CollectErrors { iter: self, collector }
    }

    fn try_collect_exhaust<T, V>(self) -> Result<V, ErrorGuaranteed>
    where
        V: FromIterator<T>,
        Self: Iterator<Item = Result<T, ErrorGuaranteed>> + Sized,
    {
        let mut acc: Option<ErrorGuaranteed> = None;
        let v = self.collect_errors(&mut acc).collect();
        match acc {
            Some(e) => Err(e),
            None => Ok(v),
        }
    }

    fn try_for_each_exhaust<T, F>(self, mut f: F) -> Result<(), ErrorGuaranteed>
    where
        Self: Iterator<Item = T> + Sized,
        F: FnMut(T) -> Result<(), ErrorGuaranteed>,
    {
        let mut acc: Option<ErrorGuaranteed> = None;
        for v in self {
            if let Err(e) = f(v) {
                acc = Some(e).or(acc);
            }
        }
        match acc {
            Some(e) => Err(e),
            None => Ok(()),
        }
    }

    fn map_take_while<F, R>(&mut self, f: F) -> MapTakeWhile<Self, F>
    where
        Self: Clone,
        F: FnMut(&Self::Item) -> Option<R>,
    {
        MapTakeWhile { iter: self, f }
    }
}

impl<I: ?Sized> IterExt for I where I: Iterator {}

pub struct CollectErrors<'a, I, C> {
    iter: I,
    collector: &'a mut C,
}

impl<I, T, E, F> Iterator for CollectErrors<'_, I, F>
where
    I: Iterator<Item = Result<T, E>>,
    F: ErrorCollector<E>,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.find(|_| true)
    }

    fn try_fold<B, F2, R2>(&mut self, init: B, mut f: F2) -> R2
    where
        F2: FnMut(B, Self::Item) -> R2,
        R2: Try<Output = B>,
    {
        self.iter.try_fold(init, |acc, x| {
            match x {
                Ok(x) => f(acc, x),
                Err(e) => {
                    self.collector.collect(e);
                    try { acc }
                }
            }
        })
    }

    fn fold<B, F2>(mut self, init: B, fold: F2) -> B
    where
        Self: Sized,
        F2: FnMut(B, Self::Item) -> B,
    {
        #[inline]
        fn ok<B, T>(mut f: impl FnMut(B, T) -> B) -> impl FnMut(B, T) -> Result<B, !> {
            move |acc, x| Ok(f(acc, x))
        }

        self.try_fold(init, ok(fold)).unwrap()
    }
}

pub struct MapTakeWhile<'a, I: 'a, F> {
    iter: &'a mut I,
    f: F,
}

impl<'a, I, F, R> Iterator for MapTakeWhile<'a, I, F>
where
    I: 'a + Iterator + Clone,
    F: FnMut(&I::Item) -> Option<R>,
{
    type Item = R;

    fn next(&mut self) -> Option<Self::Item> {
        let old = self.iter.clone();
        match self.iter.next() {
            None => None,
            Some(elt) => {
                if let Some(elt) = (self.f)(&elt) {
                    Some(elt)
                } else {
                    *self.iter = old;
                    None
                }
            }
        }
    }
}