@@ -12,6 +12,10 @@ compilation. Specifically:
12
12
- [ ` CrateInfo ` ] is serialized to ` JSON ` when the ` -Z no-link ` flag is used, and
13
13
deserialized from ` JSON ` when the ` -Z link-only ` flag is used.
14
14
15
+ [ `CrateInfo` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/struct.CrateInfo.html
16
+ [ persist incremental compilation results ] : queries/incremental-compilation-in-detail.md#the-real-world-how-persistence-makes-everything-complicated
17
+ [ serialize ] : https://en.wikipedia.org/wiki/Serialization
18
+
15
19
## The ` Encodable ` and ` Decodable ` traits
16
20
17
21
The [ ` rustc_serialize ` ] crate defines two traits for types which can be serialized:
@@ -26,13 +30,14 @@ pub trait Decodable<D: Decoder>: Sized {
26
30
}
27
31
```
28
32
29
- It also defines implementations of these for integer types, floating point
30
- types, ` bool ` , ` char ` , ` str ` and various common standard library types.
33
+ It also defines implementations of these for various common standard library
34
+ [ primitive types] ( https://doc.rust-lang.org/std/#primitives ) such as integer
35
+ types, floating point types, ` bool ` , ` char ` , ` str ` , etc.
31
36
32
- For types that are constructed from those types, ` Encodable ` and ` Decodable ` are
33
- usually implemented by [ derives] . These generate implementations that forward
34
- deserialization to the fields of the ` struct ` or ` enum ` . For a ` struct ` those ` impl ` s
35
- look something like this:
37
+ For types that are constructed from those types, ` Encodable ` and ` Decodable `
38
+ are usually implemented by [ derives] . These generate implementations that
39
+ forward deserialization to the ` field ` s of the ` struct ` or ` enum ` . For a
40
+ ` struct ` those ` impl ` s look something like this:
36
41
37
42
``` rust,ignore
38
43
#![feature(rustc_private)]
@@ -64,16 +69,17 @@ impl<D: Decoder> Decodable<D> for MyStruct {
64
69
}
65
70
}
66
71
```
72
+ [ `rustc_serialize` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/index.html
67
73
68
74
## Encoding and Decoding arena allocated types
69
75
70
76
Rust's compiler has a lot of [ arena allocated types] . Deserializing these types
71
77
isn't possible without access to the ` arena ` that they need to be allocated on.
72
- The [ ` TyDecoder ` ] and [ ` TyEncoder ` ] ` trait ` s are supertraits of ` Decoder ` and
73
- ` Encoder ` that allow access to a ` TyCtxt ` .
78
+ The [ ` TyDecoder ` ] and [ ` TyEncoder ` ] ` trait ` s are supertraits of [ ` Decoder ` ] and
79
+ [ ` Encoder ` ] that allow access to a [ ` TyCtxt ` ] .
74
80
75
81
Types which contain ` arena ` allocated types can then bound the type parameter
76
- of their ` Encodable ` and ` Decodable ` implementations with these ` trait ` s. For
82
+ of their [ ` Encodable ` ] and [ ` Decodable ` ] implementations with these ` trait ` s. For
77
83
example
78
84
79
85
``` rust,ignore
@@ -82,38 +88,62 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for MyStruct<'tcx> {
82
88
}
83
89
```
84
90
85
- The ` TyEncodable ` and ` TyDecodable ` [ derive macros] [ derives ] will expand to such
91
+ The [ ` TyEncodable ` ] and [ ` TyDecodable ` ] [ derive macros] [ derives ] will expand to such
86
92
an implementation.
87
93
88
94
Decoding the actual ` arena ` allocated type is harder, because some of the
89
- implementations can't be written due to the orphan rules. To work around this,
90
- the [ ` RefDecodable ` ] trait is defined in ` rustc_middle ` . This can then be
95
+ implementations can't be written due to the [ orphan rules] . To work around this,
96
+ the [ ` RefDecodable ` ] trait is defined in [ ` rustc_middle ` ] . This can then be
91
97
implemented for any type. The ` TyDecodable ` macro will call ` RefDecodable ` to
92
98
decode references, but various generic code needs types to actually be
93
99
` Decodable ` with a specific decoder.
94
100
95
101
For interned types instead of manually implementing ` RefDecodable ` , using a new
96
- type wrapper, like ` ty::Predicate ` and manually implementing ` Encodable ` and
102
+ type wrapper, like [ ` ty::Predicate ` ] and manually implementing ` Encodable ` and
97
103
` Decodable ` may be simpler.
98
104
105
+ [ `Decodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/trait.Decodable.html
106
+ [ `Decoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/trait.Decoder.html
107
+ [ `Encodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/trait.Encodable.html
108
+ [ `Encoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/trait.Encoder.html
109
+ [ `RefDecodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.RefDecodable.html
110
+ [ `rustc_middle` ] : https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_type_ir/codec.rs.html#21
111
+ [ `ty::Predicate` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/predicate/struct.Predicate.html
112
+ [ `TyCtxt` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html
113
+ [ `TyDecodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_macros/derive.TyDecodable.html
114
+ [ `TyDecoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.TyDecoder.html
115
+ [ `TyEncodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_macros/derive.TyEncodable.html
116
+ [ `TyEncoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.TyEncoder.html
117
+ [ arena allocated types ] : memory.md
118
+ [ derives ] : #derive-macros
119
+ [ orphan rules ] :https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
120
+
99
121
## Derive macros
100
122
101
- The ` rustc_macros ` crate defines various derives to help implement ` Decodable `
123
+ The [ ` rustc_macros ` ] crate defines various derives to help implement ` Decodable `
102
124
and ` Encodable ` .
103
125
104
126
- The ` Encodable ` and ` Decodable ` macros generate implementations that apply to
105
127
all ` Encoders ` and ` Decoders ` . These should be used in crates that don't
106
- depend on ` rustc_middle ` , or that have to be serialized by a type that does
128
+ depend on [ ` rustc_middle ` ] , or that have to be serialized by a type that does
107
129
not implement ` TyEncoder ` .
108
- - ` MetadataEncodable ` and ` MetadataDecodable ` generate implementations that
130
+ - [ ` MetadataEncodable ` ] and [ ` MetadataDecodable ` ] generate implementations that
109
131
only allow decoding by [ ` rustc_metadata::rmeta::encoder::EncodeContext ` ] and
110
132
[ ` rustc_metadata::rmeta::decoder::DecodeContext ` ] . These are used for types
111
- that contain ` rustc_metadata::rmeta::Lazy* ` .
133
+ that contain [ ` rustc_metadata::rmeta:: ` ] ` Lazy* ` .
112
134
- ` TyEncodable ` and ` TyDecodable ` generate implementation that apply to any
113
135
` TyEncoder ` or ` TyDecoder ` . These should be used for types that are only
114
136
serialized in crate metadata and/or the incremental cache, which is most
115
137
serializable types in ` rustc_middle ` .
116
138
139
+ [ `MetadataDecodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_macros/derive.MetadataDecodable.html
140
+ [ `MetadataEncodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_macros/derive.MetadataEncodable.html
141
+ [ `rustc_macros` ] : https://github.com/rust-lang/rust/tree/master/compiler/rustc_macros
142
+ [ `rustc_metadata::rmeta::` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/index.html
143
+ [ `rustc_metadata::rmeta::decoder::DecodeContext` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/decoder/struct.DecodeContext.html
144
+ [ `rustc_metadata::rmeta::encoder::EncodeContext` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/encoder/struct.EncodeContext.html
145
+ [ `rustc_middle` ] : https://github.com/rust-lang/rust/tree/master/compiler/rustc_middle
146
+
117
147
## Shorthands
118
148
119
149
` Ty ` can be deeply recursive, if each ` Ty ` was encoded naively then crate
@@ -127,8 +157,9 @@ within the file being written is encoded instead. A similar scheme is used for
127
157
128
158
Crate metadata is initially loaded before the ` TyCtxt<'tcx> ` is created, so
129
159
some deserialization needs to be deferred from the initial loading of metadata.
130
- The [ ` LazyValue<T> ` ] type wraps the (relative) offset in the crate metadata where a
131
- ` T ` has been serialized. There are also some variants, [ ` LazyArray<T> ` ] and [ ` LazyTable<I, T> ` ] .
160
+ The [ ` LazyValue<T> ` ] type wraps the (relative) offset in the crate metadata
161
+ where a ` T ` has been serialized. There are also some variants, [ ` LazyArray<T> ` ]
162
+ and [ ` LazyTable<I, T> ` ] .
132
163
133
164
The ` LazyArray<[T]> ` and ` LazyTable<I, T> ` types provide some functionality over
134
165
` Lazy<Vec<T>> ` and ` Lazy<HashMap<I, T>> ` :
@@ -138,28 +169,17 @@ The `LazyArray<[T]>` and `LazyTable<I, T>` types provide some functionality over
138
169
- Indexing into a ` LazyTable<I, T> ` does not require decoding entries other
139
170
than the one being read.
140
171
141
- ** note** : ` LazyValue<T> ` does not cache its value after being deserialized the first
142
- time. Instead the query system is the main way of caching these results.
172
+ ** note** : ` LazyValue<T> ` does not cache its value after being deserialized the
173
+ first time. Instead the query system its self is the main way of caching these
174
+ results.
175
+
176
+ [ `LazyArray<T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
177
+ [ `LazyTable<I, T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
178
+ [ `LazyValue<T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
143
179
144
180
## Specialization
145
181
146
182
A few types, most notably ` DefId ` , need to have different implementations for
147
183
different ` Encoder ` s. This is currently handled by ad-hoc specializations, for
148
184
example: ` DefId ` has a ` default ` implementation of ` Encodable<E> ` and a
149
185
specialized one for ` Encodable<CacheEncoder> ` .
150
-
151
- [ `CrateInfo` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/struct.CrateInfo.html
152
- [ `LazyArray<T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
153
- [ `LazyTable<I, T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
154
- [ `LazyValue<T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
155
- [ `RefDecodable` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.RefDecodable.html
156
- [ `rustc_metadata::rmeta::decoder::DecodeContext` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/decoder/struct.DecodeContext.html
157
- [ `rustc_metadata::rmeta::encoder::EncodeContext` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/encoder/struct.EncodeContext.html
158
- [ `rustc_serialize` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/index.html
159
- [ `TyDecoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.TyDecoder.html
160
- [ `TyEncoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.TyEncoder.html
161
- [ arena allocated types ] : memory.md
162
- [ AST ] : the-parser.md
163
- [ derives ] : #derive-macros
164
- [ persist incremental compilation results ] : queries/incremental-compilation-in-detail.md#the-real-world-how-persistence-makes-everything-complicated
165
- [ serialize ] : https://en.wikipedia.org/wiki/Serialization
0 commit comments