@@ -7,17 +7,17 @@ Binders can wrap an arbitrary Rust type `T`, not just a `Ty`.
7
7
So, how do we implement the ` instantiate ` methods on the ` Early/Binder ` types?
8
8
9
9
The answer is a couple of traits:
10
- [ ` TypeFoldable ` ] ( https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFoldable.html )
10
+ [ ` TypeFoldable ` ]
11
11
and
12
- [ ` TypeFolder ` ] ( https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html ) .
12
+ [ ` TypeFolder ` ] .
13
13
14
14
- ` TypeFoldable ` is implemented by types that embed type information. It allows you to recursively
15
15
process the contents of the ` TypeFoldable ` and do stuff to them.
16
16
- ` TypeFolder ` defines what you want to do with the types you encounter while processing the
17
17
` TypeFoldable ` .
18
18
19
19
For example, the ` TypeFolder ` trait has a method
20
- [ ` fold_ty ` ] ( https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html#method.fold_ty )
20
+ [ ` fold_ty ` ]
21
21
that takes a type as input and returns a new type as a result. ` TypeFoldable ` invokes the
22
22
` TypeFolder ` ` fold_foo ` methods on itself, giving the ` TypeFolder ` access to its contents (the
23
23
types, regions, etc that are contained within).
@@ -36,7 +36,7 @@ So to reiterate:
36
36
- ` TypeFoldable ` is a trait that is implemented by things that embed types.
37
37
38
38
In the case of ` subst ` , we can see that it is implemented as a ` TypeFolder ` :
39
- [ ` ArgFolder ` ] ( https://doc.rust-lang.org/nightly/nightly-rustc/rustc_type_ir/binder/struct.ArgFolder.html ) .
39
+ [ ` ArgFolder ` ] .
40
40
Looking at its implementation, we see where the actual substitutions are happening.
41
41
42
42
However, you might also notice that the implementation calls this ` super_fold_with ` method. What is
@@ -91,17 +91,25 @@ things. We only want to do something when we reach a type. That means there may
91
91
implementations. Such implementations of ` TypeFoldable ` tend to be pretty tedious to write by hand.
92
92
For this reason, there is a ` derive ` macro that allows you to ` #![derive(TypeFoldable)] ` . It is
93
93
defined
94
- [ here] ( https://github.com/rust-lang/rust/blob/master/compiler/rustc_macros/src/type_foldable.rs ) .
94
+ [ here] .
95
95
96
96
** ` subst ` ** In the case of substitutions the [ actual
97
- folder] ( https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L440-L451 )
97
+ folder]
98
98
is going to be doing the indexing we’ve already mentioned. There we define a ` Folder ` and call
99
99
` fold_with ` on the ` TypeFoldable ` to process yourself. Then
100
- [ fold_ty] ( https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L512-L536 )
100
+ [ fold_ty]
101
101
the method that process each type it looks for a ` ty::Param ` and for those it replaces it for
102
102
something from the list of substitutions, otherwise recursively process the type. To replace it,
103
103
calls
104
- [ ty_for_param] ( https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L552-L587 )
104
+ [ ty_for_param]
105
105
and all that does is index into the list of substitutions with the index of the ` Param ` .
106
106
107
107
[ a previous chapter ] : ty_module/instantiating_binders.md
108
+ [ `TypeFoldable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFoldable.html
109
+ [ `TypeFolder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html
110
+ [ `fold_ty` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait.TypeFolder.html#method.fold_ty
111
+ [ `ArgFolder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_type_ir/binder/struct.ArgFolder.html
112
+ [ here ] : https://github.com/rust-lang/rust/blob/master/compiler/rustc_macros/src/type_foldable.rs
113
+ [ actual folder ] : https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L440-L451
114
+ [ fold_ty ] : https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L512-L536
115
+ [ ty_for_param ] : https://github.com/rust-lang/rust/blob/75ff3110ac6d8a0259023b83fd20d7ab295f8dd6/src/librustc_middle/ty/subst.rs#L552-L587
0 commit comments