Skip to content

Commit 4034a8e

Browse files
TbkhiNoratrieb
authored andcommitted
updates documentation
1 parent 131e86b commit 4034a8e

File tree

1 file changed

+57
-37
lines changed

1 file changed

+57
-37
lines changed

src/serialization.md

+57-37
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ compilation. Specifically:
1212
- [`CrateInfo`] is serialized to `JSON` when the `-Z no-link` flag is used, and
1313
deserialized from `JSON` when the `-Z link-only` flag is used.
1414

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+
1519
## The `Encodable` and `Decodable` traits
1620

1721
The [`rustc_serialize`] crate defines two traits for types which can be serialized:
@@ -26,13 +30,14 @@ pub trait Decodable<D: Decoder>: Sized {
2630
}
2731
```
2832

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.
3136

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:
3641

3742
```rust,ignore
3843
#![feature(rustc_private)]
@@ -64,16 +69,17 @@ impl<D: Decoder> Decodable<D> for MyStruct {
6469
}
6570
}
6671
```
72+
[`rustc_serialize`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_serialize/index.html
6773

6874
## Encoding and Decoding arena allocated types
6975

7076
Rust's compiler has a lot of [arena allocated types]. Deserializing these types
7177
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`].
7480

7581
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
7783
example
7884

7985
```rust,ignore
@@ -82,38 +88,62 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for MyStruct<'tcx> {
8288
}
8389
```
8490

85-
The `TyEncodable` and `TyDecodable` [derive macros][derives] will expand to such
91+
The [`TyEncodable`] and [`TyDecodable`] [derive macros][derives] will expand to such
8692
an implementation.
8793

8894
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
9197
implemented for any type. The `TyDecodable` macro will call `RefDecodable` to
9298
decode references, but various generic code needs types to actually be
9399
`Decodable` with a specific decoder.
94100

95101
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
97103
`Decodable` may be simpler.
98104

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+
99121
## Derive macros
100122

101-
The `rustc_macros` crate defines various derives to help implement `Decodable`
123+
The [`rustc_macros`] crate defines various derives to help implement `Decodable`
102124
and `Encodable`.
103125

104126
- The `Encodable` and `Decodable` macros generate implementations that apply to
105127
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
107129
not implement `TyEncoder`.
108-
- `MetadataEncodable` and `MetadataDecodable` generate implementations that
130+
- [`MetadataEncodable`] and [`MetadataDecodable`] generate implementations that
109131
only allow decoding by [`rustc_metadata::rmeta::encoder::EncodeContext`] and
110132
[`rustc_metadata::rmeta::decoder::DecodeContext`]. These are used for types
111-
that contain `rustc_metadata::rmeta::Lazy*`.
133+
that contain [`rustc_metadata::rmeta::`]`Lazy*`.
112134
- `TyEncodable` and `TyDecodable` generate implementation that apply to any
113135
`TyEncoder` or `TyDecoder`. These should be used for types that are only
114136
serialized in crate metadata and/or the incremental cache, which is most
115137
serializable types in `rustc_middle`.
116138

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+
117147
## Shorthands
118148

119149
`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
127157

128158
Crate metadata is initially loaded before the `TyCtxt<'tcx>` is created, so
129159
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>`].
132163

133164
The `LazyArray<[T]>` and `LazyTable<I, T>` types provide some functionality over
134165
`Lazy<Vec<T>>` and `Lazy<HashMap<I, T>>`:
@@ -138,28 +169,17 @@ The `LazyArray<[T]>` and `LazyTable<I, T>` types provide some functionality over
138169
- Indexing into a `LazyTable<I, T>` does not require decoding entries other
139170
than the one being read.
140171

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
143179

144180
## Specialization
145181

146182
A few types, most notably `DefId`, need to have different implementations for
147183
different `Encoder`s. This is currently handled by ad-hoc specializations, for
148184
example: `DefId` has a `default` implementation of `Encodable<E>` and a
149185
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

Comments
 (0)