Module rustc_middle::ty::fold
source · Expand description
A folding traversal mechanism for complex data structures that contain type information.
This is a modifying traversal. It consumes the data structure, producing a
(possibly) modified version of it. Both fallible and infallible versions are
available. The name is potentially confusing, because this traversal is more
like Iterator::map
than Iterator::fold
.
This traversal has limited flexibility. Only a small number of “types of
interest” within the complex data structures can receive custom
modification. These are the ones containing the most important type-related
information, such as Ty
, Predicate
, Region
, and Const
.
There are three groups of traits involved in each traversal.
TypeFoldable
. This is implemented once for many types, including:- Types of interest, for which the methods delegate to the folder.
- All other types, including generic containers like
Vec
andOption
. It defines a “skeleton” of how they should be folded.
TypeSuperFoldable
. This is implemented only for each type of interest, and defines the folding “skeleton” for these types.TypeFolder
/`FallibleTypeFolder. One of these is implemented for each folder. This defines how types of interest are folded.
This means each fold is a mixture of (a) generic folding operations, and (b) custom fold operations that are specific to the folder.
- The
TypeFoldable
impls handle most of the traversal, and call intoTypeFolder
/FallibleTypeFolder
when they encounter a type of interest. - A
TypeFolder
/FallibleTypeFolder
may call into anotherTypeFoldable
impl, because some of the types of interest are recursive and can contain other types of interest. - A
TypeFolder
/FallibleTypeFolder
may also call into aTypeSuperFoldable
impl, because each folder 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: TypeFoldable
and U: TypeFoldable
, and an instance s = S(ty, u)
, it would be folded like so:
s.fold_with(folder) calls
- ty.fold_with(folder) calls
- folder.fold_ty(ty) may call
- ty.super_fold_with(folder)
- u.fold_with(folder)
Structs
Traits
super_fold_with
method if the primary argument
doesn’t satisfy a particular condition.