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
use crate::{Fragment, Growth, SplitVec};
use alloc::vec::Vec;
use core::iter::FusedIterator;

impl<T, G: Growth> IntoIterator for SplitVec<T, G> {
    type Item = T;
    type IntoIter = IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter::new(self.fragments)
    }
}

/// An iterator that moves out of a vector.
///
/// This struct is created by the `into_iter` method on `SplitVec` (provided by the `IntoIterator` trait).
pub struct IntoIter<T> {
    outer: alloc::vec::IntoIter<Fragment<T>>,
    inner: alloc::vec::IntoIter<T>,
}

impl<T> IntoIter<T> {
    pub(crate) fn new(fragments: Vec<Fragment<T>>) -> Self {
        let mut outer = fragments.into_iter();
        let inner = outer
            .next()
            .map(|f| f.data.into_iter())
            .unwrap_or(Vec::new().into_iter());

        Self { outer, inner }
    }

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

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

impl<T> Iterator for IntoIter<T> {
    type Item = 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()
        }
    }
}

impl<T> FusedIterator for IntoIter<T> {}