Module rustc::middle::ty_foldUnstable
[-] [+]
[src]
Generalized type folding mechanism. The setup is a bit convoluted
but allows for convenient usage. Let T be an instance of some
"foldable type" (one which implements TypeFoldable
) and F be an
instance of a "folder" (a type which implements TypeFolder
). Then
the setup is intended to be:
T.fold_with(F) --calls--> F.fold_T(T) --calls--> super_fold_T(F, T)
This way, when you define a new folder F, you can override
fold_T()
to customize the behavior, and invoke super_fold_T()
to get the original behavior. Meanwhile, to actually fold
something, you can just write T.fold_with(F)
, which is
convenient. (Note that fold_with
will also transparently handle
things like a Vec<T>
where T is foldable and so on.)
In this ideal setup, the only function that actually does
anything is super_fold_T
, which traverses the type T
. Moreover,
super_fold_T
should only ever call T.fold_with()
.
In some cases, we follow a degenerate pattern where we do not have
a fold_T
nor super_fold_T
method. Instead, T.fold_with
traverses the structure directly. This is suboptimal because the
behavior cannot be overridden, but it's much less work to implement.
If you ever do need an override that doesn't exist, it's not hard
to convert the degenerate pattern into the proper thing.
Structs
BottomUpFolder | |
RegionEraser | |
RegionFolder | Folds over the substructure of a type, visiting its component types and all regions that occur free within it. |
Traits
TypeFoldable | The TypeFoldable trait is implemented for every type that can be folded. Basically, every type that has a corresponding method in TypeFolder. |
TypeFolder | The TypeFolder trait defines the actual folding. There is a
method defined for every foldable type. Each of these has a
default implementation that does an "identity" fold. Within each
identity fold, it should invoke |