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
use super::reductions;
use crate::fragment::fragment_struct::Fragment;
use core::iter::FusedIterator;

/// Iterator over the `SplitVec`.
///
/// This struct is created by `SplitVec::iter()` method.
#[derive(Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Iter<'a, T> {
    outer: core::slice::Iter<'a, Fragment<T>>,
    inner: core::slice::Iter<'a, T>,
}

impl<'a, T> Iter<'a, T> {
    pub(crate) fn new(fragments: &'a [Fragment<T>]) -> Self {
        let mut outer = fragments.iter();
        let inner = outer.next().map(|x| x.iter()).unwrap_or([].iter());
        Self { outer, inner }
    }

    fn next_fragment(&mut self) -> Option<&'a T> {
        match self.outer.next() {
            Some(f) => {
                self.inner = f.iter();
                self.next()
            }
            None => None,
        }
    }
}

impl<'a, T> Clone for Iter<'a, T> {
    fn clone(&self) -> Self {
        Self {
            outer: self.outer.clone(),
            inner: self.inner.clone(),
        }
    }
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        let next_element = self.inner.next();
        if next_element.is_some() {
            next_element
        } else {
            self.next_fragment()
        }
    }

    // reductions
    fn all<F>(&mut self, f: F) -> bool
    where
        Self: Sized,
        F: FnMut(Self::Item) -> bool,
    {
        reductions::all(&mut self.outer, &mut self.inner, f)
    }

    fn any<F>(&mut self, f: F) -> bool
    where
        Self: Sized,
        F: FnMut(Self::Item) -> bool,
    {
        reductions::any(&mut self.outer, &mut self.inner, f)
    }

    fn fold<B, F>(mut self, init: B, f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        reductions::fold(&mut self.outer, &mut self.inner, init, f)
    }
}

impl<T> FusedIterator for Iter<'_, T> {}