33
44use crate :: ArrayBufferVisitor ;
55use crate :: ArrayChildVisitor ;
6+ use crate :: ArrayChildVisitorUnnamed ;
67use crate :: ArrayRef ;
78use crate :: buffer:: BufferHandle ;
89use crate :: vtable:: VTable ;
@@ -44,18 +45,36 @@ pub trait VisitorVTable<V: VTable> {
4445 /// Visit the children of the array.
4546 fn visit_children ( array : & V :: Array , visitor : & mut dyn ArrayChildVisitor ) ;
4647
48+ /// Visit the children of the array without names.
49+ ///
50+ /// This is more efficient than [`visit_children`] when you don't need the
51+ /// child names (e.g., for counting or accessing by index). The default
52+ /// implementation wraps the named visitor, but array types can override
53+ /// this to avoid allocating names.
54+ fn visit_children_unnamed ( array : & V :: Array , visitor : & mut dyn ArrayChildVisitorUnnamed ) {
55+ struct UnnamedWrapper < ' a > ( & ' a mut dyn ArrayChildVisitorUnnamed ) ;
56+
57+ impl ArrayChildVisitor for UnnamedWrapper < ' _ > {
58+ fn visit_child ( & mut self , _name : & str , array : & ArrayRef ) {
59+ self . 0 . visit_child ( array) ;
60+ }
61+ }
62+
63+ <V :: VisitorVTable as VisitorVTable < V > >:: visit_children ( array, & mut UnnamedWrapper ( visitor) ) ;
64+ }
65+
4766 /// Count the number of children in the array.
4867 fn nchildren ( array : & V :: Array ) -> usize {
4968 struct NChildren ( usize ) ;
5069
51- impl ArrayChildVisitor for NChildren {
52- fn visit_child ( & mut self , _name : & str , _array : & ArrayRef ) {
70+ impl ArrayChildVisitorUnnamed for NChildren {
71+ fn visit_child ( & mut self , _array : & ArrayRef ) {
5372 self . 0 += 1 ;
5473 }
5574 }
5675
5776 let mut visitor = NChildren ( 0 ) ;
58- <V :: VisitorVTable as VisitorVTable < V > >:: visit_children ( array, & mut visitor) ;
77+ <V :: VisitorVTable as VisitorVTable < V > >:: visit_children_unnamed ( array, & mut visitor) ;
5978 visitor. 0
6079 }
6180
@@ -69,8 +88,8 @@ pub trait VisitorVTable<V: VTable> {
6988 result : Option < ArrayRef > ,
7089 }
7190
72- impl ArrayChildVisitor for NthChildVisitor {
73- fn visit_child ( & mut self , _name : & str , array : & ArrayRef ) {
91+ impl ArrayChildVisitorUnnamed for NthChildVisitor {
92+ fn visit_child ( & mut self , array : & ArrayRef ) {
7493 if self . current_idx == self . target_idx && self . result . is_none ( ) {
7594 self . result = Some ( array. clone ( ) ) ;
7695 }
@@ -83,7 +102,7 @@ pub trait VisitorVTable<V: VTable> {
83102 current_idx : 0 ,
84103 result : None ,
85104 } ;
86- <V :: VisitorVTable as VisitorVTable < V > >:: visit_children ( array, & mut visitor) ;
105+ <V :: VisitorVTable as VisitorVTable < V > >:: visit_children_unnamed ( array, & mut visitor) ;
87106 visitor. result
88107 }
89108}
0 commit comments