polars_arrow/array/union/
iterator.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
use super::UnionArray;
use crate::scalar::Scalar;
use crate::trusted_len::TrustedLen;

#[derive(Debug, Clone)]
pub struct UnionIter<'a> {
    array: &'a UnionArray,
    current: usize,
}

impl<'a> UnionIter<'a> {
    #[inline]
    pub fn new(array: &'a UnionArray) -> Self {
        Self { array, current: 0 }
    }
}

impl Iterator for UnionIter<'_> {
    type Item = Box<dyn Scalar>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.current == self.array.len() {
            None
        } else {
            let old = self.current;
            self.current += 1;
            Some(unsafe { self.array.value_unchecked(old) })
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.array.len() - self.current;
        (len, Some(len))
    }
}

impl<'a> IntoIterator for &'a UnionArray {
    type Item = Box<dyn Scalar>;
    type IntoIter = UnionIter<'a>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a> UnionArray {
    /// constructs a new iterator
    #[inline]
    pub fn iter(&'a self) -> UnionIter<'a> {
        UnionIter::new(self)
    }
}

impl std::iter::ExactSizeIterator for UnionIter<'_> {}

unsafe impl TrustedLen for UnionIter<'_> {}