Skip to main content

vortex_array/arrays/varbin/
accessor.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::iter;
5
6use vortex_error::VortexExpect;
7
8#[expect(deprecated)]
9use crate::ToCanonical as _;
10use crate::accessor::ArrayAccessor;
11use crate::arrays::VarBinArray;
12use crate::arrays::varbin::VarBinArrayExt;
13use crate::match_each_integer_ptype;
14use crate::validity::Validity;
15
16impl ArrayAccessor<[u8]> for VarBinArray {
17    fn with_iterator<F, R>(&self, f: F) -> R
18    where
19        F: for<'a> FnOnce(&mut dyn Iterator<Item = Option<&'a [u8]>>) -> R,
20    {
21        #[expect(deprecated)]
22        let offsets = self.offsets().to_primitive();
23        let validity = self
24            .validity()
25            .vortex_expect("varbin validity should be derivable");
26
27        let bytes = self.bytes();
28        let bytes = bytes.as_slice();
29
30        match_each_integer_ptype!(offsets.ptype(), |T| {
31            let offsets = offsets.as_slice::<T>();
32
33            #[allow(clippy::cast_possible_truncation)]
34            match validity {
35                Validity::NonNullable | Validity::AllValid => {
36                    let mut iter = offsets
37                        .windows(2)
38                        .map(|w| Some(&bytes[w[0] as usize..w[1] as usize]));
39                    f(&mut iter)
40                }
41                Validity::AllInvalid => f(&mut iter::repeat_n(None, self.len())),
42                Validity::Array(v) => {
43                    #[expect(deprecated)]
44                    let validity = v.to_bool().into_bit_buffer();
45                    let mut iter = offsets
46                        .windows(2)
47                        .zip(validity.iter())
48                        .map(|(w, valid)| valid.then(|| &bytes[w[0] as usize..w[1] as usize]));
49                    f(&mut iter)
50                }
51            }
52        })
53    }
54}
55
56impl ArrayAccessor<[u8]> for &VarBinArray {
57    fn with_iterator<F, R>(&self, f: F) -> R
58    where
59        F: for<'a> FnOnce(&mut dyn Iterator<Item = Option<&'a [u8]>>) -> R,
60    {
61        <VarBinArray as ArrayAccessor<[u8]>>::with_iterator(*self, f)
62    }
63}