pub trait Visit {
// Required methods
fn visit_post<'a, F>(&'a self, f: &mut F)
where F: FnMut(&'a Self);
fn visit_mut_post<F>(&mut self, f: &mut F)
where F: FnMut(&mut Self);
fn try_visit_post<'a, F, E>(&'a self, f: &mut F) -> Result<(), E>
where F: FnMut(&'a Self) -> Result<(), E>;
fn try_visit_mut_post<F, E>(&mut self, f: &mut F) -> Result<(), E>
where F: FnMut(&mut Self) -> Result<(), E>;
fn visit_pre<'a, F>(&'a self, f: &mut F)
where F: FnMut(&'a Self);
fn visit_pre_with_context<'a, Context, AccFun, Visitor>(
&'a self,
init: Context,
acc_fun: &mut AccFun,
visitor: &mut Visitor,
)
where Context: Clone,
AccFun: FnMut(Context, &'a Self) -> Context,
Visitor: FnMut(&Context, &'a Self);
fn visit_mut_pre<F>(&mut self, f: &mut F)
where F: FnMut(&mut Self);
fn try_visit_pre<'a, F, E>(&'a self, f: &mut F) -> Result<(), E>
where F: FnMut(&'a Self) -> Result<(), E>;
fn try_visit_mut_pre<F, E>(&mut self, f: &mut F) -> Result<(), E>
where F: FnMut(&mut Self) -> Result<(), E>;
fn visit_pre_post<'a, F1, F2>(&'a self, pre: &mut F1, post: &mut F2)
where F1: FnMut(&'a Self) -> Option<Vec<&'a Self>>,
F2: FnMut(&'a Self);
fn visit_mut_pre_post<F1, F2>(&mut self, pre: &mut F1, post: &mut F2)
where F1: FnMut(&mut Self) -> Option<Vec<&mut Self>>,
F2: FnMut(&mut Self);
}Expand description
A trait for types that can recursively visit their children of the same type.
This trait is automatically implemented for all implementors of
VisitChildren.
All methods provided by this trait are iterative.
The immutable visitors hand the callback references borrowed for the whole
lifetime of &self, so a callback may keep them: accumulate them, or embed
them in its error type. This is what lets a bottom-up fold report an error
that points at the offending node.
The mutable visitors deliberately do not do this. Their callbacks are
higher-ranked over the reference lifetime, which prevents a callback from
retaining what it is handed. For the post-order and pre-post visitors that
is load-bearing: they rebuild &mut Self from raw pointers while a parent’s
pointer stays on the traversal stack, so retaining a child reference, were
that permitted, would alias &mut references to a parent and its child.
See VisitMutAction for the full argument. The pre-order visitors keep
plain &mut Self on their stack and consume a parent before visiting its
children, so for them the borrow checker would reject the generalization
rather than accept unsound code.
NB that any visitor with mutable post-traversal uses unsafe code. It is critical
that VisitChildren::children_mut be written using safe code, i.e., no aliasing
of children or access to parents.
Required Methods§
Sourcefn visit_post<'a, F>(&'a self, f: &mut F)where
F: FnMut(&'a Self),
fn visit_post<'a, F>(&'a self, f: &mut F)where
F: FnMut(&'a Self),
Post-order immutable infallible visitor for self.
Sourcefn visit_mut_post<F>(&mut self, f: &mut F)where
F: FnMut(&mut Self),
fn visit_mut_post<F>(&mut self, f: &mut F)where
F: FnMut(&mut Self),
Post-order mutable infallible visitor for self.
Sourcefn try_visit_post<'a, F, E>(&'a self, f: &mut F) -> Result<(), E>
fn try_visit_post<'a, F, E>(&'a self, f: &mut F) -> Result<(), E>
Post-order immutable fallible visitor for self.
Sourcefn try_visit_mut_post<F, E>(&mut self, f: &mut F) -> Result<(), E>
fn try_visit_mut_post<F, E>(&mut self, f: &mut F) -> Result<(), E>
Post-order mutable fallible visitor for self.
Sourcefn visit_pre<'a, F>(&'a self, f: &mut F)where
F: FnMut(&'a Self),
fn visit_pre<'a, F>(&'a self, f: &mut F)where
F: FnMut(&'a Self),
Pre-order immutable infallible visitor for self.
Sourcefn visit_pre_with_context<'a, Context, AccFun, Visitor>(
&'a self,
init: Context,
acc_fun: &mut AccFun,
visitor: &mut Visitor,
)
fn visit_pre_with_context<'a, Context, AccFun, Visitor>( &'a self, init: Context, acc_fun: &mut AccFun, visitor: &mut Visitor, )
Pre-order immutable infallible visitor for self, which also accumulates context
information along the path from the root to the current node’s parent.
acc_fun is a similar closure as in fold. The accumulated context is passed to the
visitor, along with the current node.
For example, one can use this on a MirScalarExpr to tell the visitor whether the current
subexpression has a negation somewhere above it.
When using it on a MirRelationExpr, one has to be mindful that Let bindings are not
followed, i.e., the context won’t include what happens with a Let binding in some other
MirRelationExpr where the binding occurs in a Get.
Sourcefn visit_mut_pre<F>(&mut self, f: &mut F)where
F: FnMut(&mut Self),
fn visit_mut_pre<F>(&mut self, f: &mut F)where
F: FnMut(&mut Self),
Pre-order mutable infallible visitor for self.
Sourcefn try_visit_pre<'a, F, E>(&'a self, f: &mut F) -> Result<(), E>
fn try_visit_pre<'a, F, E>(&'a self, f: &mut F) -> Result<(), E>
Pre-order immutable fallible visitor for self.
Sourcefn try_visit_mut_pre<F, E>(&mut self, f: &mut F) -> Result<(), E>
fn try_visit_mut_pre<F, E>(&mut self, f: &mut F) -> Result<(), E>
Pre-order mutable fallible visitor for self.
Sourcefn visit_pre_post<'a, F1, F2>(&'a self, pre: &mut F1, post: &mut F2)
fn visit_pre_post<'a, F1, F2>(&'a self, pre: &mut F1, post: &mut F2)
A generalization of Visit::visit_pre and Visit::visit_post.
The function pre runs on self before it runs on any of the children.
The function post runs on children first before the parent.
Optionally, pre can return which children, if any, should be visited
(default is to visit all children).
Sourcefn visit_mut_pre_post<F1, F2>(&mut self, pre: &mut F1, post: &mut F2)
fn visit_mut_pre_post<F1, F2>(&mut self, pre: &mut F1, post: &mut F2)
A generalization of Visit::visit_mut_pre and Visit::visit_mut_post.
The function pre runs on self before it runs on any of the children.
The function post runs on children first before the parent.
Optionally, pre can return which children, if any, should be visited
(default is to visit all children).
It is important for safety that pre is (a) safe code and (b) returns children only.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".