vortex_array/vtable/
visitor.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_buffer::ByteBuffer;
5
6use crate::vtable::VTable;
7use crate::{Array, ArrayBufferVisitor, ArrayChildVisitor};
8
9pub trait VisitorVTable<V: VTable> {
10    /// Visit the buffers of the array.
11    fn visit_buffers(array: &V::Array, visitor: &mut dyn ArrayBufferVisitor);
12
13    /// Count the number of buffers in the array.
14    fn nbuffers(array: &V::Array) -> usize {
15        struct NBuffers(usize);
16
17        impl ArrayBufferVisitor for NBuffers {
18            fn visit_buffer(&mut self, _buffer: &ByteBuffer) {
19                self.0 += 1;
20            }
21        }
22
23        let mut visitor = NBuffers(0);
24        <V::VisitorVTable as VisitorVTable<V>>::visit_buffers(array, &mut visitor);
25        visitor.0
26    }
27
28    /// Visit the children of the array.
29    fn visit_children(array: &V::Array, visitor: &mut dyn ArrayChildVisitor);
30
31    /// Count the number of children in the array.
32    fn nchildren(array: &V::Array) -> usize {
33        struct NChildren(usize);
34
35        impl ArrayChildVisitor for NChildren {
36            fn visit_child(&mut self, _name: &str, _array: &dyn Array) {
37                self.0 += 1;
38            }
39        }
40
41        let mut visitor = NChildren(0);
42        <V::VisitorVTable as VisitorVTable<V>>::visit_children(array, &mut visitor);
43        visitor.0
44    }
45}