16
16
- ` TypeFolder ` defines what you want to do with the types you encounter while processing the
17
17
` TypeFoldable ` .
18
18
19
- For example, the ` TypeFolder ` trait has a method
20
- [ ` fold_ty ` ]
21
- that takes a type as input and returns a new type as a result. ` TypeFoldable ` invokes the
22
- ` TypeFolder ` ` fold_foo ` methods on itself, giving the ` TypeFolder ` access to its contents (the
23
- types, regions, etc that are contained within).
19
+ For example, the ` TypeFolder ` trait has a method [ ` fold_ty ` ]
20
+ that takes a type as input and returns a new type as a result.
21
+ ` TypeFoldable ` invokes the ` TypeFolder ` ` fold_foo ` methods on itself,
22
+ giving the ` TypeFolder ` access to its contents (the types, regions, etc that are contained within).
24
23
25
24
You can think of it with this analogy to the iterator combinators we have come to love in Rust:
26
25
@@ -35,8 +34,7 @@ So to reiterate:
35
34
- ` TypeFolder ` is a trait that defines a “map” operation.
36
35
- ` TypeFoldable ` is a trait that is implemented by things that embed types.
37
36
38
- In the case of ` subst ` , we can see that it is implemented as a ` TypeFolder ` :
39
- [ ` ArgFolder ` ] .
37
+ In the case of ` subst ` , we can see that it is implemented as a ` TypeFolder ` : [ ` ArgFolder ` ] .
40
38
Looking at its implementation, we see where the actual substitutions are happening.
41
39
42
40
However, you might also notice that the implementation calls this ` super_fold_with ` method. What is
@@ -90,18 +88,14 @@ things. We only want to do something when we reach a type. That means there may
90
88
` TypeFoldable ` types whose implementations basically just forward to their fields’ ` TypeFoldable `
91
89
implementations. Such implementations of ` TypeFoldable ` tend to be pretty tedious to write by hand.
92
90
For this reason, there is a ` derive ` macro that allows you to ` #![derive(TypeFoldable)] ` . It is
93
- defined
94
- [ here] .
95
-
96
- ** ` subst ` ** In the case of substitutions the [ actual
97
- folder]
98
- is going to be doing the indexing we’ve already mentioned. There we define a ` Folder ` and call
99
- ` fold_with ` on the ` TypeFoldable ` to process yourself. Then
100
- [ fold_ty]
101
- the method that process each type it looks for a ` ty::Param ` and for those it replaces it for
102
- something from the list of substitutions, otherwise recursively process the type. To replace it,
103
- calls
104
- [ ty_for_param]
91
+ defined [ here] .
92
+
93
+ ** ` subst ` ** In the case of substitutions the [ actual folder]
94
+ is going to be doing the indexing we’ve already mentioned.
95
+ There we define a ` Folder ` and call ` fold_with ` on the ` TypeFoldable ` to process yourself.
96
+ Then [ fold_ty] the method that process each type it looks for a ` ty::Param ` and for those
97
+ it replaces it for something from the list of substitutions, otherwise recursively process the type.
98
+ To replace it, calls [ ty_for_param]
105
99
and all that does is index into the list of substitutions with the index of the ` Param ` .
106
100
107
101
[ a previous chapter ] : ty_module/instantiating_binders.md
0 commit comments