11# Serialization in Rustc
22
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:
55
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.
910- Certain query outputs are serialized in a binary format to
1011 [ 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.
1314
1415## The ` Encodable ` and ` Decodable ` traits
1516
@@ -30,7 +31,7 @@ types, `bool`, `char`, `str` and various common standard library types.
3031
3132For types that are constructed from those types, ` Encodable ` and ` Decodable ` are
3233usually 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
3435look something like this:
3536
3637``` rust,ignore
@@ -51,6 +52,7 @@ impl<E: Encoder> Encodable<E> for MyStruct {
5152 })
5253 }
5354}
55+
5456impl<D: Decoder> Decodable<D> for MyStruct {
5557 fn decode(s: &mut D) -> Result<MyStruct, D::Error> {
5658 s.read_struct("MyStruct", 2, |d| {
@@ -65,13 +67,13 @@ impl<D: Decoder> Decodable<D> for MyStruct {
6567
6668## Encoding and Decoding arena allocated types
6769
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
7173` Encoder ` that allow access to a ` TyCtxt ` .
7274
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
7577example
7678
7779``` rust,ignore
@@ -83,7 +85,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for MyStruct<'tcx> {
8385The ` TyEncodable ` and ` TyDecodable ` [ derive macros] [ derives ] will expand to such
8486an implementation.
8587
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
8789implementations can't be written due to the orphan rules. To work around this,
8890the [ ` RefDecodable ` ] trait is defined in ` rustc_middle ` . This can then be
8991implemented for any type. The ` TyDecodable ` macro will call ` RefDecodable ` to
@@ -117,7 +119,7 @@ and `Encodable`.
117119` Ty ` can be deeply recursive, if each ` Ty ` was encoded naively then crate
118120metadata would be very large. To handle this, each ` TyEncoder ` has a cache of
119121locations 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
121123within the file being written is encoded instead. A similar scheme is used for
122124` ty::Predicate ` .
123125
@@ -131,7 +133,7 @@ The [`LazyValue<T>`] type wraps the (relative) offset in the crate metadata wher
131133The ` LazyArray<[T]> ` and ` LazyTable<I, T> ` types provide some functionality over
132134` Lazy<Vec<T>> ` and ` Lazy<HashMap<I, T>> ` :
133135
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
135137 first collecting into a ` Vec<T> ` .
136138- Indexing into a ` LazyTable<I, T> ` does not require decoding entries other
137139 than the one being read.
@@ -142,15 +144,9 @@ time. Instead the query system is the main way of caching these results.
142144## Specialization
143145
144146A 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> ` .
154150
155151[ `CrateInfo` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/struct.CrateInfo.html
156152[ `LazyArray<T>` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_metadata/rmeta/struct.LazyValue.html
@@ -162,3 +158,8 @@ for `Encodable<CacheEncoder>`.
162158[ `rustc_serialize` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/index.html
163159[ `TyDecoder` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/codec/trait.TyDecoder.html
164160[ `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