@@ -28,107 +28,34 @@ pub use visitor_derive::Traversable;
2828/// See [`TraversableMut`].
2929pub use visitor_derive:: TraversableMut ;
3030
31- pub use self :: impl_visitor:: * ;
32-
33- /// Whether the visitor is entering or exiting a node.
34- #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
35- pub enum Event {
36- /// The visitor is entering a node.
37- Enter ,
38- /// The visitor is exiting a node.
39- Exit ,
40- }
31+ pub mod function;
4132
4233/// A visitor that can be used to traverse a data structure.
4334pub trait Visitor {
44- /// Called when the visitor is traversing a node.
45- fn visit ( & mut self , this : & dyn core:: any:: Any , event : Event ) ;
35+ /// Called when the visitor is entering a node.
36+ ///
37+ /// Default implementation does nothing.
38+ #[ expect( unused_variables) ]
39+ fn enter ( & mut self , this : & dyn core:: any:: Any ) { }
40+ /// Called when the visitor is leaving a node.
41+ ///
42+ /// Default implementation does nothing.
43+ #[ expect( unused_variables) ]
44+ fn leave ( & mut self , this : & dyn core:: any:: Any ) { }
4645}
4746
4847/// A visitor that can be used to traverse a mutable data structure.
4948pub trait VisitorMut {
50- /// Called when the visitor is traversing a mutable node.
51- fn visit_mut ( & mut self , this : & mut dyn core:: any:: Any , event : Event ) ;
52- }
53-
54- mod impl_visitor {
55- use core:: any:: Any ;
56- use core:: marker:: PhantomData ;
57-
58- use super :: * ;
59-
60- /// Type returned by [`visitor_fn`].
61- pub struct FnVisitor < T , F > {
62- f : F ,
63- m : PhantomData < T > ,
64- }
65-
66- impl < T : Any , F : FnMut ( & T , Event ) > Visitor for FnVisitor < T , F > {
67- fn visit ( & mut self , this : & dyn Any , event : Event ) {
68- if let Some ( item) = <dyn Any >:: downcast_ref :: < T > ( this) {
69- ( self . f ) ( item, event) ;
70- }
71- }
72- }
73-
74- impl < T : Any , F : FnMut ( & mut T , Event ) > VisitorMut for FnVisitor < T , F > {
75- fn visit_mut ( & mut self , this : & mut dyn Any , event : Event ) {
76- if let Some ( item) = <dyn Any >:: downcast_mut :: < T > ( this) {
77- ( self . f ) ( item, event) ;
78- }
79- }
80- }
81-
82- /// Create a visitor that only visits items of some specific type from a function or a closure.
83- pub fn visitor_fn < T , F : FnMut ( & T , Event ) > ( f : F ) -> FnVisitor < T , F > {
84- FnVisitor { f, m : PhantomData }
85- }
86-
87- /// Similar to [`visitor_fn`], but the closure will only be called on [`Event::Enter`].
88- pub fn visitor_enter_fn < T , F : FnMut ( & T ) > ( mut f : F ) -> FnVisitor < T , impl FnMut ( & T , Event ) > {
89- visitor_fn ( move |item, event| {
90- if let Event :: Enter = event {
91- f ( item) ;
92- }
93- } )
94- }
95-
96- /// Similar to [`visitor_fn`], but the closure will only be called on [`Event::Exit`].
97- pub fn visitor_exit_fn < T , F : FnMut ( & T ) > ( mut f : F ) -> FnVisitor < T , impl FnMut ( & T , Event ) > {
98- visitor_fn ( move |item, event| {
99- if let Event :: Exit = event {
100- f ( item) ;
101- }
102- } )
103- }
104-
105- /// Create a visitor that only visits mutable items of some specific type from a function or a
106- /// closure.
107- pub fn visitor_fn_mut < T , F : FnMut ( & mut T , Event ) > ( f : F ) -> FnVisitor < T , F > {
108- FnVisitor { f, m : PhantomData }
109- }
110-
111- /// Similar to [`visitor_fn_mut`], but the closure will only be called on [`Event::Enter`].
112- pub fn visitor_enter_fn_mut < T , F : FnMut ( & mut T ) > (
113- mut f : F ,
114- ) -> FnVisitor < T , impl FnMut ( & mut T , Event ) > {
115- visitor_fn_mut ( move |item, event| {
116- if let Event :: Enter = event {
117- f ( item) ;
118- }
119- } )
120- }
121-
122- /// Similar to [`visitor_fn_mut`], but the closure will only be called on [`Event::Exit`].
123- pub fn visitor_exit_fn_mut < T , F : FnMut ( & mut T ) > (
124- mut f : F ,
125- ) -> FnVisitor < T , impl FnMut ( & mut T , Event ) > {
126- visitor_fn_mut ( move |item, event| {
127- if let Event :: Exit = event {
128- f ( item) ;
129- }
130- } )
131- }
49+ /// Called when the visitor is entering a mutable node.
50+ ///
51+ /// Default implementation does nothing.
52+ #[ expect( unused_variables) ]
53+ fn enter_mut ( & mut self , this : & mut dyn core:: any:: Any ) { }
54+ /// Called when the visitor is leaving a mutable node.
55+ ///
56+ /// Default implementation does nothing.
57+ #[ expect( unused_variables) ]
58+ fn leave_mut ( & mut self , this : & mut dyn core:: any:: Any ) { }
13259}
13360
13461/// A trait for types that can be traversed by a visitor.
@@ -161,15 +88,15 @@ macro_rules! trivial_traverse_impl {
16188 ( $type: ty ) => {
16289 impl Traversable for $type {
16390 fn traverse<V : Visitor >( & self , visitor: & mut V ) {
164- visitor. visit ( self , Event :: Enter ) ;
165- visitor. visit ( self , Event :: Exit ) ;
91+ visitor. enter ( self ) ;
92+ visitor. leave ( self ) ;
16693 }
16794 }
16895
16996 impl TraversableMut for $type {
17097 fn traverse_mut<V : VisitorMut >( & mut self , visitor: & mut V ) {
171- visitor. visit_mut ( self , Event :: Enter ) ;
172- visitor. visit_mut ( self , Event :: Exit ) ;
98+ visitor. enter_mut ( self ) ;
99+ visitor. leave_mut ( self ) ;
173100 }
174101 }
175102 } ;
0 commit comments