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
use vortex_dtype::NativePType;
use vortex_error::VortexResult;

use crate::accessor::ArrayAccessor;
use crate::array::primitive::PrimitiveArray;
use crate::validity::ArrayValidity;

impl<T: NativePType> ArrayAccessor<T> for PrimitiveArray {
    fn with_iterator<F, R>(&self, f: F) -> VortexResult<R>
    where
        F: for<'a> FnOnce(&mut (dyn Iterator<Item = Option<&'a T>>)) -> R,
    {
        match self.logical_validity().to_null_buffer()? {
            None => {
                let mut iter = self.maybe_null_slice::<T>().iter().map(Some);
                Ok(f(&mut iter))
            }
            Some(nulls) => {
                let mut iter = self
                    .maybe_null_slice::<T>()
                    .iter()
                    .zip(nulls.iter())
                    .map(|(value, valid)| valid.then_some(value));
                Ok(f(&mut iter))
            }
        }
    }
}