@@ -385,11 +385,11 @@ pub trait Decoder {
385385/// `MetadataEncodable` macros.
386386///
387387/// * `Encodable` should be used in crates that don't depend on
388- /// `librustc_middle`.
388+ /// `rustc_middle`.
389+ /// * `MetadataEncodable` is used in `rustc_metadata` for types that contain
390+ /// `rustc_metadata::rmeta::Lazy`.
389391/// * `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.
392+ /// metadata or the incremental cache. This is most types in `rustc_middle`.
393393pub trait Encodable < S : Encoder > {
394394 fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > ;
395395}
@@ -400,61 +400,50 @@ pub trait Encodable<S: Encoder> {
400400/// `MetadataDecodable` macros.
401401///
402402/// * `Decodable` should be used in crates that don't depend on
403- /// `librustc_middle`.
403+ /// `rustc_middle`.
404+ /// * `MetadataDecodable` is used in `rustc_metadata` for types that contain
405+ /// `rustc_metadata::rmeta::Lazy`.
404406/// * `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.
407+ /// metadata or the incremental cache. This is most types in `rustc_middle`.
408408pub trait Decodable < D : Decoder > : Sized {
409409 fn decode ( d : & mut D ) -> Result < Self , D :: Error > ;
410410}
411411
412- impl < S : Encoder > Encodable < S > for usize {
413- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
414- s. emit_usize ( * self )
415- }
416- }
417-
418- impl < D : Decoder > Decodable < D > for usize {
419- fn decode ( d : & mut D ) -> Result < usize , D :: Error > {
420- d. read_usize ( )
421- }
422- }
423-
424- impl < S : Encoder > Encodable < S > for u8 {
425- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
426- s. emit_u8 ( * self )
427- }
428- }
429-
430- impl < D : Decoder > Decodable < D > for u8 {
431- fn decode ( d : & mut D ) -> Result < u8 , D :: Error > {
432- d. read_u8 ( )
433- }
434- }
435-
436- impl < S : Encoder > Encodable < S > for u16 {
437- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
438- s. emit_u16 ( * self )
439- }
440- }
441-
442- impl < D : Decoder > Decodable < D > for u16 {
443- fn decode ( d : & mut D ) -> Result < u16 , D :: Error > {
444- d. read_u16 ( )
445- }
446- }
412+ macro_rules! direct_serialize_impls {
413+ ( $( $ty: ident $emit_method: ident $read_method: ident) ,* ) => {
414+ $(
415+ impl <S : Encoder > Encodable <S > for $ty {
416+ fn encode( & self , s: & mut S ) -> Result <( ) , S :: Error > {
417+ s. $emit_method( * self )
418+ }
419+ }
447420
448- impl < S : Encoder > Encodable < S > for u32 {
449- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
450- s. emit_u32 ( * self )
421+ impl <D : Decoder > Decodable <D > for $ty {
422+ fn decode( d: & mut D ) -> Result <$ty, D :: Error > {
423+ d. $read_method( )
424+ }
425+ }
426+ ) *
451427 }
452428}
453429
454- impl < D : Decoder > Decodable < D > for u32 {
455- fn decode ( d : & mut D ) -> Result < u32 , D :: Error > {
456- d. read_u32 ( )
457- }
430+ direct_serialize_impls ! {
431+ usize emit_usize read_usize,
432+ u8 emit_u8 read_u8,
433+ u16 emit_u16 read_u16,
434+ u32 emit_u32 read_u32,
435+ u64 emit_u64 read_u64,
436+ u128 emit_u128 read_u128,
437+ isize emit_isize read_isize,
438+ i8 emit_i8 read_i8,
439+ i16 emit_i16 read_i16,
440+ i32 emit_i32 read_i32,
441+ i64 emit_i64 read_i64,
442+ i128 emit_i128 read_i128,
443+ f32 emit_f32 read_f32,
444+ f64 emit_f64 read_f64,
445+ bool emit_bool read_bool,
446+ char emit_char read_char
458447}
459448
460449impl < S : Encoder > Encodable < S > for :: std:: num:: NonZeroU32 {
@@ -469,102 +458,6 @@ impl<D: Decoder> Decodable<D> for ::std::num::NonZeroU32 {
469458 }
470459}
471460
472- impl < S : Encoder > Encodable < S > for u64 {
473- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
474- s. emit_u64 ( * self )
475- }
476- }
477-
478- impl < D : Decoder > Decodable < D > for u64 {
479- fn decode ( d : & mut D ) -> Result < u64 , D :: Error > {
480- d. read_u64 ( )
481- }
482- }
483-
484- impl < S : Encoder > Encodable < S > for u128 {
485- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
486- s. emit_u128 ( * self )
487- }
488- }
489-
490- impl < D : Decoder > Decodable < D > for u128 {
491- fn decode ( d : & mut D ) -> Result < u128 , D :: Error > {
492- d. read_u128 ( )
493- }
494- }
495-
496- impl < S : Encoder > Encodable < S > for isize {
497- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
498- s. emit_isize ( * self )
499- }
500- }
501-
502- impl < D : Decoder > Decodable < D > for isize {
503- fn decode ( d : & mut D ) -> Result < isize , D :: Error > {
504- d. read_isize ( )
505- }
506- }
507-
508- impl < S : Encoder > Encodable < S > for i8 {
509- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
510- s. emit_i8 ( * self )
511- }
512- }
513-
514- impl < D : Decoder > Decodable < D > for i8 {
515- fn decode ( d : & mut D ) -> Result < i8 , D :: Error > {
516- d. read_i8 ( )
517- }
518- }
519-
520- impl < S : Encoder > Encodable < S > for i16 {
521- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
522- s. emit_i16 ( * self )
523- }
524- }
525-
526- impl < D : Decoder > Decodable < D > for i16 {
527- fn decode ( d : & mut D ) -> Result < i16 , D :: Error > {
528- d. read_i16 ( )
529- }
530- }
531-
532- impl < S : Encoder > Encodable < S > for i32 {
533- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
534- s. emit_i32 ( * self )
535- }
536- }
537-
538- impl < D : Decoder > Decodable < D > for i32 {
539- fn decode ( d : & mut D ) -> Result < i32 , D :: Error > {
540- d. read_i32 ( )
541- }
542- }
543-
544- impl < S : Encoder > Encodable < S > for i64 {
545- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
546- s. emit_i64 ( * self )
547- }
548- }
549-
550- impl < D : Decoder > Decodable < D > for i64 {
551- fn decode ( d : & mut D ) -> Result < i64 , D :: Error > {
552- d. read_i64 ( )
553- }
554- }
555-
556- impl < S : Encoder > Encodable < S > for i128 {
557- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
558- s. emit_i128 ( * self )
559- }
560- }
561-
562- impl < D : Decoder > Decodable < D > for i128 {
563- fn decode ( d : & mut D ) -> Result < i128 , D :: Error > {
564- d. read_i128 ( )
565- }
566- }
567-
568461impl < S : Encoder > Encodable < S > for str {
569462 fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
570463 s. emit_str ( self )
@@ -589,54 +482,6 @@ impl<D: Decoder> Decodable<D> for String {
589482 }
590483}
591484
592- impl < S : Encoder > Encodable < S > for f32 {
593- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
594- s. emit_f32 ( * self )
595- }
596- }
597-
598- impl < D : Decoder > Decodable < D > for f32 {
599- fn decode ( d : & mut D ) -> Result < f32 , D :: Error > {
600- d. read_f32 ( )
601- }
602- }
603-
604- impl < S : Encoder > Encodable < S > for f64 {
605- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
606- s. emit_f64 ( * self )
607- }
608- }
609-
610- impl < D : Decoder > Decodable < D > for f64 {
611- fn decode ( d : & mut D ) -> Result < f64 , D :: Error > {
612- d. read_f64 ( )
613- }
614- }
615-
616- impl < S : Encoder > Encodable < S > for bool {
617- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
618- s. emit_bool ( * self )
619- }
620- }
621-
622- impl < D : Decoder > Decodable < D > for bool {
623- fn decode ( d : & mut D ) -> Result < bool , D :: Error > {
624- d. read_bool ( )
625- }
626- }
627-
628- impl < S : Encoder > Encodable < S > for char {
629- fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
630- s. emit_char ( * self )
631- }
632- }
633-
634- impl < D : Decoder > Decodable < D > for char {
635- fn decode ( d : & mut D ) -> Result < char , D :: Error > {
636- d. read_char ( )
637- }
638- }
639-
640485impl < S : Encoder > Encodable < S > for ( ) {
641486 fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
642487 s. emit_unit ( )
0 commit comments