@@ -691,42 +691,39 @@ impl<E> SpecializationError for E {
691
691
/// Implement this trait on encoders, with `T` being the type
692
692
/// you want to encode (employing `UseSpecializedEncodable`),
693
693
/// using a strategy specific to the encoder.
694
- /// Can also be implemented alongside `UseSpecializedEncodable`
695
- /// to provide a default `specialized_encode` for encoders
696
- /// which do not implement `SpecializedEncoder` themselves.
697
- pub trait SpecializedEncoder < T : ?Sized > : Encoder {
694
+ pub trait SpecializedEncoder < T : ?Sized + UseSpecializedEncodable > : Encoder {
698
695
/// Encode the value in a manner specific to this encoder state.
699
- /// Defaults to returning an error (see `SpecializationError`).
700
696
fn specialized_encode ( & mut self , value : & T ) -> Result < ( ) , Self :: Error > ;
701
697
}
702
698
703
- impl < E : Encoder , T : ?Sized > SpecializedEncoder < T > for E {
704
- default fn specialized_encode ( & mut self , _ : & T ) -> Result < ( ) , E :: Error > {
705
- Err ( E :: Error :: not_found :: < E , T > ( "SpecializedEncoder" , "specialized_encode" ) )
699
+ impl < E : Encoder , T : ?Sized + UseSpecializedEncodable > SpecializedEncoder < T > for E {
700
+ default fn specialized_encode ( & mut self , value : & T ) -> Result < ( ) , E :: Error > {
701
+ value . default_encode ( self )
706
702
}
707
703
}
708
704
709
705
/// Implement this trait on decoders, with `T` being the type
710
706
/// you want to decode (employing `UseSpecializedDecodable`),
711
707
/// using a strategy specific to the decoder.
712
- /// Can also be implemented alongside `UseSpecializedDecodable`
713
- /// to provide a default `specialized_decode` for decoders
714
- /// which do not implement `SpecializedDecoder` themselves.
715
- pub trait SpecializedDecoder < T > : Decoder {
708
+ pub trait SpecializedDecoder < T : UseSpecializedDecodable > : Decoder {
716
709
/// Decode a value in a manner specific to this decoder state.
717
- /// Defaults to returning an error (see `SpecializationError`).
718
710
fn specialized_decode ( & mut self ) -> Result < T , Self :: Error > ;
719
711
}
720
712
721
- impl < D : Decoder , T > SpecializedDecoder < T > for D {
713
+ impl < D : Decoder , T : UseSpecializedDecodable > SpecializedDecoder < T > for D {
722
714
default fn specialized_decode ( & mut self ) -> Result < T , D :: Error > {
723
- Err ( D :: Error :: not_found :: < D , T > ( "SpecializedDecoder" , "specialized_decode" ) )
715
+ T :: default_decode ( self )
724
716
}
725
717
}
726
718
727
719
/// Implement this trait on your type to get an `Encodable`
728
720
/// implementation which goes through `SpecializedEncoder`.
729
- pub trait UseSpecializedEncodable { }
721
+ pub trait UseSpecializedEncodable {
722
+ /// Defaults to returning an error (see `SpecializationError`).
723
+ fn default_encode < E : Encoder > ( & self , _: & mut E ) -> Result < ( ) , E :: Error > {
724
+ Err ( E :: Error :: not_found :: < E , Self > ( "SpecializedEncoder" , "specialized_encode" ) )
725
+ }
726
+ }
730
727
731
728
impl < T : ?Sized + UseSpecializedEncodable > Encodable for T {
732
729
default fn encode < E : Encoder > ( & self , e : & mut E ) -> Result < ( ) , E :: Error > {
@@ -736,7 +733,12 @@ impl<T: ?Sized + UseSpecializedEncodable> Encodable for T {
736
733
737
734
/// Implement this trait on your type to get an `Decodable`
738
735
/// implementation which goes through `SpecializedDecoder`.
739
- pub trait UseSpecializedDecodable : Sized { }
736
+ pub trait UseSpecializedDecodable : Sized {
737
+ /// Defaults to returning an error (see `SpecializationError`).
738
+ fn default_decode < D : Decoder > ( _: & mut D ) -> Result < Self , D :: Error > {
739
+ Err ( D :: Error :: not_found :: < D , Self > ( "SpecializedDecoder" , "specialized_decode" ) )
740
+ }
741
+ }
740
742
741
743
impl < T : UseSpecializedDecodable > Decodable for T {
742
744
default fn decode < D : Decoder > ( d : & mut D ) -> Result < T , D :: Error > {
0 commit comments