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