A visiting traversal mechanism for complex data structures that contain type information.
This is a read-only traversal of the data structure.
This traversal has limited flexibility. Only a small number of “types of
interest” within the complex data structures can receive custom
visitation. These are the ones containing the most important type-related
information, such as
There are three groups of traits involved in each traversal.
TypeVisitable. This is implemented once for many types, including:
- Types of interest, for which the the methods delegate to the visitor.
- All other types, including generic containers like
Option. It defines a “skeleton” of how they should be visited.
TypeSuperVisitable. This is implemented only for each type of interest, and defines the visiting “skeleton” for these types.
TypeVisitor. This is implemented for each visitor. This defines how types of interest are visited.
This means each visit is a mixture of (a) generic visiting operations, and (b) custom visit operations that are specific to the visitor.
TypeVisitableimpls handle most of the traversal, and call into
TypeVisitorwhen they encounter a type of interest.
TypeVisitormay call into another
TypeVisitableimpl, because some of the types of interest are recursive and can contain other types of interest.
TypeVisitormay also call into a
TypeSuperVisitableimpl, because each visitor might provide custom handling only for some types of interest, or only for some variants of each type of interest, and then use default traversal for the remaining cases.
For example, if you have
struct S(Ty, U) where
S: TypeVisitable and
U: TypeVisitable, and an instance
s = S(ty, u), it would be visited like so:
s.visit_with(visitor) calls - ty.visit_with(visitor) calls - visitor.visit_ty(ty) may call - ty.super_visit_with(visitor) - u.visit_with(visitor)
t. A bound var can be a bound region or a bound type.