@@ -72,25 +72,77 @@ decl_derive!(
7272 hash_stable:: hash_stable_no_context_derive
7373) ;
7474
75- decl_derive ! ( [ Decodable_NoContext ] => serialize:: decodable_nocontext_derive) ;
75+ // Encoding and Decoding derives
76+ decl_derive ! ( [ Decodable_NoContext ] =>
77+ /// See docs on derive [`Decodable`].
78+ ///
79+ /// Derives `Decodable<D> for T where D: Decoder`.
80+ serialize:: decodable_nocontext_derive
81+ ) ;
7682decl_derive ! ( [ Encodable_NoContext ] => serialize:: encodable_nocontext_derive) ;
77- decl_derive ! ( [ Decodable ] => serialize:: decodable_derive) ;
83+ decl_derive ! ( [ Decodable ] =>
84+ /// Derives `Decodable<D> for T where D: SpanDecoder`
85+ ///
86+ /// # Deriving decoding traits
87+ ///
88+ /// > Some shared docs about decoding traits, since this is likely the first trait you find
89+ ///
90+ /// The difference between theses derives can be subtle!
91+ /// At a high level, there's the `T: Decodable<D>` trait that says some type `T`
92+ /// can be decoded using a decoder `D`. There are various decoders!
93+ /// The different derives place different *trait* bounds on this type `D`.
94+ ///
95+ /// Even though this derive, based on its name, seems like the most vanilla one,
96+ /// it actually places a pretty strict bound on `D`: `SpanDecoder`.
97+ /// It means that types that derive this can contain spans, among other things,
98+ /// and still be decoded. The reason this is hard is that at least in metadata,
99+ /// spoans can only be decoded later, once some information from the header
100+ /// is already decoded to properly deal with spans.
101+ ///
102+ /// The hierarchy is roughly:
103+ ///
104+ /// - derive [`Decodable_NoContext`] is the most relaxed bounds that could be placed on `D`,
105+ /// and is only really suited for structs and enums containing primitive types.
106+ /// - derive [`BlobDecodable`] may be a better default, than deriving `Decodable`:
107+ /// it places fewer requirements on `D`, while still allowing some complex types to be decoded.
108+ /// - derive [`LazyDecodable`]: Only for types containing `Lazy{Array,Table,Value}`.
109+ /// - derive [`Decodable`] for structures containing spans. Requires `D: SpanDecoder`
110+ /// - derive [`TyDecodable`] for types that require access to the `TyCtxt` while decoding.
111+ /// For example: arena allocated types.
112+ serialize:: decodable_derive
113+ ) ;
78114decl_derive ! ( [ Encodable ] => serialize:: encodable_derive) ;
79- decl_derive ! ( [ TyDecodable ] => serialize:: type_decodable_derive) ;
115+ decl_derive ! ( [ TyDecodable ] =>
116+ /// See docs on derive [`Decodable`].
117+ ///
118+ /// Derives `Decodable<D> for T where D: TyDecoder`.
119+ serialize:: type_decodable_derive
120+ ) ;
80121decl_derive ! ( [ TyEncodable ] => serialize:: type_encodable_derive) ;
81- decl_derive ! ( [ MetadataDecodable ] =>
82- /// This constrains the decoder to be specifically the decoder that can decode LazyArrays in metadata.
83- /// Therefore, we only use this on things containing LazyArray really.
84- /// Anything else should either be `NoContext`, if possible `BlobDecodable`, or otherwise just `Decodable`.
85- serialize:: meta_decodable_derive
122+ decl_derive ! ( [ LazyDecodable ] =>
123+ /// See docs on derive [`Decodable`].
124+ ///
125+ /// Derives `Decodable<D> for T where D: LazyDecoder`.
126+ /// This constrains the decoder to be specifically the decoder that can decode
127+ /// `LazyArray`s, `LazyValue`s amd `LazyTable`s in metadata.
128+ /// Therefore, we only need this on things containing LazyArray really.
129+ ///
130+ /// Most decodable derives mirror an encodable derive.
131+ /// [`LazyDecodable`] and [`BlobDecodable`] together roughly mirror [`MetadataEncodable`]
132+ serialize:: lazy_decodable_derive
86133) ;
87134decl_derive ! ( [ BlobDecodable ] =>
88- /// For anything that is "simple" to decode, without needing anything but the original data,
89- /// but for which the Decoder can customize some things
90- /// (unlike Decodable_NoContext which individual decoders can't customize) .
135+ /// See docs on derive [`Decodable`].
136+ ///
137+ /// Derives `Decodable<D> for T where D: BlobDecoder` .
91138 serialize:: blob_decodable_derive
92139) ;
93- decl_derive ! ( [ MetadataEncodable ] => serialize:: meta_encodable_derive) ;
140+ decl_derive ! ( [ MetadataEncodable ] =>
141+ /// Most encodable derives mirror a decodable derive.
142+ /// [`MetadataEncodable`] is roughly mirrored by the combination of [`LazyDecodable`] and [`BlobDecodable`]
143+ serialize:: meta_encodable_derive
144+ ) ;
145+
94146decl_derive ! (
95147 [ TypeFoldable , attributes( type_foldable) ] =>
96148 /// Derives `TypeFoldable` for the annotated `struct` or `enum` (`union` is not supported).
0 commit comments