11use crate :: { Decoder , Encoder } ;
22use thiserror:: Error ;
33
4- pub trait IsBinary < T , E : ?Sized > {
5- fn is_binary ( ) -> bool ;
6- }
7-
8- impl < D , T > IsBinary < T , [ u8 ] > for D
9- where
10- D : Decoder < T , Encoded = [ u8 ] > ,
11- {
12- fn is_binary ( ) -> bool {
13- true
14- }
15- }
16-
17- impl < D , T > IsBinary < T , str > for D
18- where
19- D : Decoder < T , Encoded = str > ,
20- {
21- fn is_binary ( ) -> bool {
22- false
23- }
24- }
25-
264#[ derive( Debug , Error ) ]
275pub enum HybridCoderError < E > {
286 #[ error( "Not implemented: {0}" ) ]
@@ -34,6 +12,8 @@ pub enum HybridCoderError<E> {
3412pub trait HybridDecoder < T , E : ?Sized > {
3513 type Error ;
3614
15+ fn is_binary_decoder ( ) -> bool ;
16+
3717 fn decode_str ( _val : & str ) -> Result < T , HybridCoderError < Self :: Error > > {
3818 Err ( HybridCoderError :: NotImplemented (
3919 "You're trying to decode from a string. This codec is binary." ,
5333{
5434 type Error = D :: Error ;
5535
36+ #[ inline( always) ]
37+ fn is_binary_decoder ( ) -> bool {
38+ true
39+ }
40+
5641 fn decode_bin ( val : & [ u8 ] ) -> Result < T , HybridCoderError < Self :: Error > > {
5742 Ok ( D :: decode ( val) ?)
5843 }
6449{
6550 type Error = D :: Error ;
6651
52+ #[ inline( always) ]
53+ fn is_binary_decoder ( ) -> bool {
54+ false
55+ }
56+
6757 fn decode_str ( val : & str ) -> Result < T , HybridCoderError < Self :: Error > > {
6858 Ok ( D :: decode ( val) ?)
6959 }
7262pub trait HybridEncoder < T , E > {
7363 type Error ;
7464
65+ fn is_binary_encoder ( ) -> bool ;
66+
7567 fn encode_str ( _val : & T ) -> Result < String , HybridCoderError < Self :: Error > > {
7668 Err ( HybridCoderError :: NotImplemented (
7769 "You're trying to encode into a string. This codec is binary." ,
9183{
9284 type Error = E :: Error ;
9385
86+ #[ inline( always) ]
87+ fn is_binary_encoder ( ) -> bool {
88+ true
89+ }
90+
9491 fn encode_bin ( val : & T ) -> Result < Vec < u8 > , HybridCoderError < Self :: Error > > {
9592 Ok ( E :: encode ( val) ?)
9693 }
@@ -102,6 +99,11 @@ where
10299{
103100 type Error = E :: Error ;
104101
102+ #[ inline( always) ]
103+ fn is_binary_encoder ( ) -> bool {
104+ false
105+ }
106+
105107 fn encode_str ( val : & T ) -> Result < String , HybridCoderError < Self :: Error > > {
106108 Ok ( E :: encode ( val) ?)
107109 }
0 commit comments