@@ -138,6 +138,7 @@ pub trait TyDecoder<'a, 'tcx: 'a>: Decoder {
138138 }
139139}
140140
141+ #[ inline]
141142pub fn decode_cnum < ' a , ' tcx , D > ( decoder : & mut D ) -> Result < CrateNum , D :: Error >
142143 where D : TyDecoder < ' a , ' tcx > ,
143144 ' tcx : ' a ,
@@ -146,6 +147,7 @@ pub fn decode_cnum<'a, 'tcx, D>(decoder: &mut D) -> Result<CrateNum, D::Error>
146147 Ok ( decoder. map_encoded_cnum_to_current ( cnum) )
147148}
148149
150+ #[ inline]
149151pub fn decode_ty < ' a , ' tcx , D > ( decoder : & mut D ) -> Result < Ty < ' tcx > , D :: Error >
150152 where D : TyDecoder < ' a , ' tcx > ,
151153 ' tcx : ' a ,
@@ -165,6 +167,7 @@ pub fn decode_ty<'a, 'tcx, D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
165167 }
166168}
167169
170+ #[ inline]
168171pub fn decode_predicates < ' a , ' tcx , D > ( decoder : & mut D )
169172 -> Result < ty:: GenericPredicates < ' tcx > , D :: Error >
170173 where D : TyDecoder < ' a , ' tcx > ,
@@ -188,6 +191,7 @@ pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
188191 } )
189192}
190193
194+ #[ inline]
191195pub fn decode_substs < ' a , ' tcx , D > ( decoder : & mut D ) -> Result < & ' tcx Substs < ' tcx > , D :: Error >
192196 where D : TyDecoder < ' a , ' tcx > ,
193197 ' tcx : ' a ,
@@ -197,13 +201,15 @@ pub fn decode_substs<'a, 'tcx, D>(decoder: &mut D) -> Result<&'tcx Substs<'tcx>,
197201 Ok ( tcx. mk_substs ( ( 0 ..len) . map ( |_| Decodable :: decode ( decoder) ) ) ?)
198202}
199203
204+ #[ inline]
200205pub fn decode_region < ' a , ' tcx , D > ( decoder : & mut D ) -> Result < ty:: Region < ' tcx > , D :: Error >
201206 where D : TyDecoder < ' a , ' tcx > ,
202207 ' tcx : ' a ,
203208{
204209 Ok ( decoder. tcx ( ) . mk_region ( Decodable :: decode ( decoder) ?) )
205210}
206211
212+ #[ inline]
207213pub fn decode_ty_slice < ' a , ' tcx , D > ( decoder : & mut D )
208214 -> Result < & ' tcx ty:: Slice < Ty < ' tcx > > , D :: Error >
209215 where D : TyDecoder < ' a , ' tcx > ,
@@ -213,6 +219,7 @@ pub fn decode_ty_slice<'a, 'tcx, D>(decoder: &mut D)
213219 Ok ( decoder. tcx ( ) . mk_type_list ( ( 0 ..len) . map ( |_| Decodable :: decode ( decoder) ) ) ?)
214220}
215221
222+ #[ inline]
216223pub fn decode_adt_def < ' a , ' tcx , D > ( decoder : & mut D )
217224 -> Result < & ' tcx ty:: AdtDef , D :: Error >
218225 where D : TyDecoder < ' a , ' tcx > ,
@@ -222,6 +229,7 @@ pub fn decode_adt_def<'a, 'tcx, D>(decoder: &mut D)
222229 Ok ( decoder. tcx ( ) . adt_def ( def_id) )
223230}
224231
232+ #[ inline]
225233pub fn decode_existential_predicate_slice < ' a , ' tcx , D > ( decoder : & mut D )
226234 -> Result < & ' tcx ty:: Slice < ty:: ExistentialPredicate < ' tcx > > , D :: Error >
227235 where D : TyDecoder < ' a , ' tcx > ,
@@ -232,6 +240,7 @@ pub fn decode_existential_predicate_slice<'a, 'tcx, D>(decoder: &mut D)
232240 . mk_existential_predicates ( ( 0 ..len) . map ( |_| Decodable :: decode ( decoder) ) ) ?)
233241}
234242
243+ #[ inline]
235244pub fn decode_byte_array < ' a , ' tcx , D > ( decoder : & mut D )
236245 -> Result < ByteArray < ' tcx > , D :: Error >
237246 where D : TyDecoder < ' a , ' tcx > ,
@@ -242,10 +251,138 @@ pub fn decode_byte_array<'a, 'tcx, D>(decoder: &mut D)
242251 } )
243252}
244253
254+ #[ inline]
245255pub fn decode_const < ' a , ' tcx , D > ( decoder : & mut D )
246256 -> Result < & ' tcx ty:: Const < ' tcx > , D :: Error >
247257 where D : TyDecoder < ' a , ' tcx > ,
248258 ' tcx : ' a ,
249259{
250260 Ok ( decoder. tcx ( ) . mk_const ( Decodable :: decode ( decoder) ?) )
251261}
262+
263+ #[ macro_export]
264+ macro_rules! __impl_decoder_methods {
265+ ( $( $name: ident -> $ty: ty; ) * ) => {
266+ $( fn $name( & mut self ) -> Result <$ty, Self :: Error > {
267+ self . opaque. $name( )
268+ } ) *
269+ }
270+ }
271+
272+ #[ macro_export]
273+ macro_rules! implement_ty_decoder {
274+ ( $DecoderName: ident <$( $typaram: tt) ,* >) => {
275+ mod __ty_decoder_impl {
276+ use super :: $DecoderName;
277+ use $crate:: ty;
278+ use $crate:: ty:: codec:: * ;
279+ use $crate:: ty:: subst:: Substs ;
280+ use $crate:: hir:: def_id:: { CrateNum } ;
281+ use $crate:: middle:: const_val:: ByteArray ;
282+ use rustc_serialize:: { Decoder , SpecializedDecoder } ;
283+ use std:: borrow:: Cow ;
284+
285+ impl <$( $typaram ) ,* > Decoder for $DecoderName<$( $typaram) ,* > {
286+ type Error = String ;
287+
288+ __impl_decoder_methods! {
289+ read_nil -> ( ) ;
290+
291+ read_u128 -> u128 ;
292+ read_u64 -> u64 ;
293+ read_u32 -> u32 ;
294+ read_u16 -> u16 ;
295+ read_u8 -> u8 ;
296+ read_usize -> usize ;
297+
298+ read_i128 -> i128 ;
299+ read_i64 -> i64 ;
300+ read_i32 -> i32 ;
301+ read_i16 -> i16 ;
302+ read_i8 -> i8 ;
303+ read_isize -> isize ;
304+
305+ read_bool -> bool ;
306+ read_f64 -> f64 ;
307+ read_f32 -> f32 ;
308+ read_char -> char ;
309+ read_str -> Cow <str >;
310+ }
311+
312+ fn error( & mut self , err: & str ) -> Self :: Error {
313+ self . opaque. error( err)
314+ }
315+ }
316+
317+ // FIXME(#36588) These impls are horribly unsound as they allow
318+ // the caller to pick any lifetime for 'tcx, including 'static,
319+ // by using the unspecialized proxies to them.
320+
321+ impl <$( $typaram) ,* > SpecializedDecoder <CrateNum > for $DecoderName<$( $typaram) ,* > {
322+ fn specialized_decode( & mut self ) -> Result <CrateNum , Self :: Error > {
323+ decode_cnum( self )
324+ }
325+ }
326+
327+ impl <$( $typaram) ,* > SpecializedDecoder <ty:: Ty <' tcx>> for $DecoderName<$( $typaram) ,* > {
328+ fn specialized_decode( & mut self ) -> Result <ty:: Ty <' tcx>, Self :: Error > {
329+ decode_ty( self )
330+ }
331+ }
332+
333+ impl <$( $typaram) ,* > SpecializedDecoder <ty:: GenericPredicates <' tcx>>
334+ for $DecoderName<$( $typaram) ,* > {
335+ fn specialized_decode( & mut self ) -> Result <ty:: GenericPredicates <' tcx>, Self :: Error > {
336+ decode_predicates( self )
337+ }
338+ }
339+
340+ impl <$( $typaram) ,* > SpecializedDecoder <& ' tcx Substs <' tcx>> for $DecoderName<$( $typaram) ,* > {
341+ fn specialized_decode( & mut self ) -> Result <& ' tcx Substs <' tcx>, Self :: Error > {
342+ decode_substs( self )
343+ }
344+ }
345+
346+ impl <$( $typaram) ,* > SpecializedDecoder <ty:: Region <' tcx>> for $DecoderName<$( $typaram) ,* > {
347+ fn specialized_decode( & mut self ) -> Result <ty:: Region <' tcx>, Self :: Error > {
348+ decode_region( self )
349+ }
350+ }
351+
352+ impl <$( $typaram) ,* > SpecializedDecoder <& ' tcx ty:: Slice <ty:: Ty <' tcx>>>
353+ for $DecoderName<$( $typaram) ,* > {
354+ fn specialized_decode( & mut self ) -> Result <& ' tcx ty:: Slice <ty:: Ty <' tcx>>, Self :: Error > {
355+ decode_ty_slice( self )
356+ }
357+ }
358+
359+ impl <$( $typaram) ,* > SpecializedDecoder <& ' tcx ty:: AdtDef > for $DecoderName<$( $typaram) ,* > {
360+ fn specialized_decode( & mut self ) -> Result <& ' tcx ty:: AdtDef , Self :: Error > {
361+ decode_adt_def( self )
362+ }
363+ }
364+
365+ impl <$( $typaram) ,* > SpecializedDecoder <& ' tcx ty:: Slice <ty:: ExistentialPredicate <' tcx>>>
366+ for $DecoderName<$( $typaram) ,* > {
367+ fn specialized_decode( & mut self )
368+ -> Result <& ' tcx ty:: Slice <ty:: ExistentialPredicate <' tcx>>, Self :: Error > {
369+ decode_existential_predicate_slice( self )
370+ }
371+ }
372+
373+ impl <$( $typaram) ,* > SpecializedDecoder <ByteArray <' tcx>> for $DecoderName<$( $typaram) ,* > {
374+ fn specialized_decode( & mut self ) -> Result <ByteArray <' tcx>, Self :: Error > {
375+ decode_byte_array( self )
376+ }
377+ }
378+
379+ impl <$( $typaram) ,* > SpecializedDecoder <& ' tcx $crate:: ty:: Const <' tcx>>
380+ for $DecoderName<$( $typaram) ,* > {
381+ fn specialized_decode( & mut self ) -> Result <& ' tcx ty:: Const <' tcx>, Self :: Error > {
382+ decode_const( self )
383+ }
384+ }
385+ }
386+ }
387+ }
388+
0 commit comments