1
1
# Serialization in Rustc
2
2
3
- Rustc has to [ serialize] and deserialize various data during compilation.
4
- Specifically:
3
+ Rust's compiler has to [ serialize] and deserialize various data during
4
+ compilation. Specifically:
5
5
6
- - "Crate metadata", mainly query outputs, are serialized in a binary
7
- format into ` rlib ` and ` rmeta ` files that are output when compiling a library
8
- crate, these are then deserialized by crates that depend on that library.
6
+ - Certain crate metadata, consisting mainly of query outputs, are serialized
7
+ from a binary format into ` rlib ` and ` rmeta ` files that are output when
8
+ compiling a library crate. These ` rlib ` and ` rmeta ` files are then
9
+ deserialized by the crates which depend on that library.
9
10
- Certain query outputs are serialized in a binary format to
10
11
[ persist incremental compilation results] .
11
- - [ ` CrateInfo ` ] is serialized to json when the ` -Z no-link ` flag is used, and
12
- deserialized from json when the ` -Z link-only ` flag is used.
12
+ - [ ` CrateInfo ` ] is serialized to ` JSON ` when the ` -Z no-link ` flag is used, and
13
+ deserialized from ` JSON ` when the ` -Z link-only ` flag is used.
13
14
14
15
## The ` Encodable ` and ` Decodable ` traits
15
16
@@ -30,7 +31,7 @@ types, `bool`, `char`, `str` and various common standard library types.
30
31
31
32
For types that are constructed from those types, ` Encodable ` and ` Decodable ` are
32
33
usually implemented by [ derives] . These generate implementations that forward
33
- deserialization to the fields of the struct or enum. For a struct those impls
34
+ deserialization to the fields of the ` struct ` or ` enum ` . For a ` struct ` those ` impl ` s
34
35
look something like this:
35
36
36
37
``` rust,ignore
@@ -51,6 +52,7 @@ impl<E: Encoder> Encodable<E> for MyStruct {
51
52
})
52
53
}
53
54
}
55
+
54
56
impl<D: Decoder> Decodable<D> for MyStruct {
55
57
fn decode(s: &mut D) -> Result<MyStruct, D::Error> {
56
58
s.read_struct("MyStruct", 2, |d| {
@@ -65,13 +67,13 @@ impl<D: Decoder> Decodable<D> for MyStruct {
65
67
66
68
## Encoding and Decoding arena allocated types
67
69
68
- Rustc has a lot of [ arena allocated types] . Deserializing these types isn't
69
- possible without access to the arena that they need to be allocated on. The
70
- [ ` TyDecoder ` ] and [ ` TyEncoder ` ] traits are supertraits of ` Decoder ` and
70
+ Rust's compiler has a lot of [ arena allocated types] . Deserializing these types
71
+ 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
71
73
` Encoder ` that allow access to a ` TyCtxt ` .
72
74
73
- Types which contain arena allocated types can then bound the type parameter of
74
- their ` Encodable ` and ` Decodable ` implementations with these traits . For
75
+ Types which contain ` arena ` allocated types can then bound the type parameter
76
+ of their ` Encodable ` and ` Decodable ` implementations with these ` trait ` s . For
75
77
example
76
78
77
79
``` rust,ignore
@@ -83,7 +85,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for MyStruct<'tcx> {
83
85
The ` TyEncodable ` and ` TyDecodable ` [ derive macros] [ derives ] will expand to such
84
86
an implementation.
85
87
86
- Decoding the actual arena allocated type is harder, because some of the
88
+ Decoding the actual ` arena ` allocated type is harder, because some of the
87
89
implementations can't be written due to the orphan rules. To work around this,
88
90
the [ ` RefDecodable ` ] trait is defined in ` rustc_middle ` . This can then be
89
91
implemented for any type. The ` TyDecodable ` macro will call ` RefDecodable ` to
@@ -117,7 +119,7 @@ and `Encodable`.
117
119
` Ty ` can be deeply recursive, if each ` Ty ` was encoded naively then crate
118
120
metadata would be very large. To handle this, each ` TyEncoder ` has a cache of
119
121
locations in its output where it has serialized types. If a type being encoded
120
- is in the cache, then instead of serializing the type as usual, the byte offset
122
+ is in cache, then instead of serializing the type as usual, the byte offset
121
123
within the file being written is encoded instead. A similar scheme is used for
122
124
` ty::Predicate ` .
123
125
@@ -131,7 +133,7 @@ The [`LazyValue<T>`] type wraps the (relative) offset in the crate metadata wher
131
133
The ` LazyArray<[T]> ` and ` LazyTable<I, T> ` types provide some functionality over
132
134
` Lazy<Vec<T>> ` and ` Lazy<HashMap<I, T>> ` :
133
135
134
- - It's possible to encode a ` LazyArray<T> ` directly from an iterator , without
136
+ - It's possible to encode a ` LazyArray<T> ` directly from an ` Iterator ` , without
135
137
first collecting into a ` Vec<T> ` .
136
138
- Indexing into a ` LazyTable<I, T> ` does not require decoding entries other
137
139
than the one being read.
@@ -142,15 +144,9 @@ time. Instead the query system is the main way of caching these results.
142
144
## Specialization
143
145
144
146
A few types, most notably ` DefId ` , need to have different implementations for
145
- different ` Encoder ` s. This is currently handled by ad-hoc specializations:
146
- ` DefId ` has a ` default ` implementation of ` Encodable<E> ` and a specialized one
147
- for ` Encodable<CacheEncoder> ` .
148
-
149
- [ arena allocated types ] : memory.md
150
- [ AST ] : the-parser.md
151
- [ derives ] : #derive-macros
152
- [ persist incremental compilation results ] : queries/incremental-compilation-in-detail.md#the-real-world-how-persistence-makes-everything-complicated
153
- [ serialize ] : https://en.wikipedia.org/wiki/Serialization
147
+ different ` Encoder ` s. This is currently handled by ad-hoc specializations, for
148
+ example: ` DefId ` has a ` default ` implementation of ` Encodable<E> ` and a
149
+ specialized one for ` Encodable<CacheEncoder> ` .
154
150
155
151
[ `CrateInfo` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/struct.CrateInfo.html
156
152
[ `LazyArray<T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
@@ -162,3 +158,8 @@ for `Encodable<CacheEncoder>`.
162
158
[ `rustc_serialize` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/index.html
163
159
[ `TyDecoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.TyDecoder.html
164
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