Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a225ddd

Browse files
committed
Add some documentation for (De|En)codable
1 parent b90018e commit a225ddd

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/librustc_middle/ty/codec.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ pub trait TyEncoder<'tcx>: Encoder {
7474
fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>;
7575
}
7676

77+
/// Trait for decoding to a reference.
78+
///
79+
/// This is a separate trait from `Decodable` so that we can implement it for
80+
/// upstream types, such as `FxHashSet`.
81+
///
82+
/// The `TyDecodable` derive macro will use this trait for fields that are
83+
/// references (and don't use a type alias to hide that).
84+
///
85+
/// `Decodable` can still be implemented in cases where `Decodable` is required
86+
/// by a trait bound.
7787
pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> {
7888
fn decode(d: &mut D) -> Result<&'tcx Self, D::Error>;
7989
}
@@ -301,6 +311,7 @@ macro_rules! impl_decodable_via_ref {
301311
})*
302312
}
303313
}
314+
304315
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::AdtDef {
305316
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
306317
let def_id = <DefId as Decodable<D>>::decode(decoder)?;

src/librustc_serialize/serialize.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,32 @@ pub trait Decoder {
379379
fn error(&mut self, err: &str) -> Self::Error;
380380
}
381381

382+
/// Trait for types that can be serialized
383+
///
384+
/// This can be implemented using the `Encodable`, `TyEncodable` and
385+
/// `MetadataEncodable` macros.
386+
///
387+
/// * `Encodable` should be used in crates that don't depend on
388+
/// `librustc_middle`.
389+
/// * `TyEncodable` should be used for types that are only serialized in crate
390+
/// metadata or the incremental cache, except for simple enums.where
391+
/// * `MetadataEncodable` is used in `rustc_metadata` for types that are only
392+
/// serialized in crate metadata.
382393
pub trait Encodable<S: Encoder> {
383394
fn encode(&self, s: &mut S) -> Result<(), S::Error>;
384395
}
385396

397+
/// Trait for types that can be deserialized
398+
///
399+
/// This can be implemented using the `Decodable`, `TyDecodable` and
400+
/// `MetadataDecodable` macros.
401+
///
402+
/// * `Decodable` should be used in crates that don't depend on
403+
/// `librustc_middle`.
404+
/// * `TyDecodable` should be used for types that are only serialized in crate
405+
/// metadata or the incremental cache, except for simple enums.where
406+
/// * `MetadataDecodable` is used in `rustc_metadata` for types that are only
407+
/// serialized in crate metadata.
386408
pub trait Decodable<D: Decoder>: Sized {
387409
fn decode(d: &mut D) -> Result<Self, D::Error>;
388410
}

0 commit comments

Comments
 (0)