polars_arrow/array/union/
iterator.rs

1use super::UnionArray;
2use crate::scalar::Scalar;
3use crate::trusted_len::TrustedLen;
4
5#[derive(Debug, Clone)]
6pub struct UnionIter<'a> {
7    array: &'a UnionArray,
8    current: usize,
9}
10
11impl<'a> UnionIter<'a> {
12    #[inline]
13    pub fn new(array: &'a UnionArray) -> Self {
14        Self { array, current: 0 }
15    }
16}
17
18impl Iterator for UnionIter<'_> {
19    type Item = Box<dyn Scalar>;
20
21    #[inline]
22    fn next(&mut self) -> Option<Self::Item> {
23        if self.current == self.array.len() {
24            None
25        } else {
26            let old = self.current;
27            self.current += 1;
28            Some(unsafe { self.array.value_unchecked(old) })
29        }
30    }
31
32    #[inline]
33    fn size_hint(&self) -> (usize, Option<usize>) {
34        let len = self.array.len() - self.current;
35        (len, Some(len))
36    }
37}
38
39impl<'a> IntoIterator for &'a UnionArray {
40    type Item = Box<dyn Scalar>;
41    type IntoIter = UnionIter<'a>;
42
43    #[inline]
44    fn into_iter(self) -> Self::IntoIter {
45        self.iter()
46    }
47}
48
49impl<'a> UnionArray {
50    /// constructs a new iterator
51    #[inline]
52    pub fn iter(&'a self) -> UnionIter<'a> {
53        UnionIter::new(self)
54    }
55}
56
57impl std::iter::ExactSizeIterator for UnionIter<'_> {}
58
59unsafe impl TrustedLen for UnionIter<'_> {}