@@ -14,12 +14,16 @@ use core::{
14
14
} ,
15
15
time:: Duration ,
16
16
} ;
17
+ use core_futures_io:: { AsyncRead , AsyncWrite } ;
17
18
use futures:: {
18
19
future:: { ready, Map , Ready } ,
19
20
stream:: { once, Forward , Once , StreamFuture } ,
20
21
FutureExt , Sink , StreamExt ,
21
22
} ;
22
- use protocol:: { Bottom , Channels , Format , Protocol } ;
23
+ use protocol:: {
24
+ format:: { ByteFormat , ItemFormat } ,
25
+ Bottom , Channels , Format , Protocol ,
26
+ } ;
23
27
use serde:: { de:: DeserializeOwned , Serialize } ;
24
28
25
29
#[ macro_export]
@@ -49,9 +53,27 @@ pub trait Serializer {
49
53
) -> Result < T , Self :: DeserializeError > ;
50
54
}
51
55
56
+ pub trait ByteSerializer : Serializer {
57
+ type SerializeError ;
58
+ type DeserializeError ;
59
+
60
+ fn serialize < T : Serialize + DeserializeOwned , W : AsyncWrite > (
61
+ & mut self ,
62
+ writer : & mut W ,
63
+ item : T ,
64
+ ) -> Result < ( ) , <Self as ByteSerializer >:: SerializeError > ;
65
+
66
+ fn deserialize < T : Serialize + DeserializeOwned , R : AsyncRead > (
67
+ & mut self ,
68
+ reader : & mut R ,
69
+ ) -> Result < T , <Self as ByteSerializer >:: DeserializeError > ;
70
+ }
71
+
52
72
pub struct Serde < T : Serializer > ( T ) ;
53
73
54
- impl < T : Serializer , U : Serialize + DeserializeOwned > Format < U > for Serde < T > {
74
+ impl < T : Serializer , U : Serialize + DeserializeOwned > Format < U > for Serde < T > { }
75
+
76
+ impl < T : Serializer , U : Serialize + DeserializeOwned > ItemFormat < U > for Serde < T > {
55
77
type Representation = T :: Representation ;
56
78
type SerializeError = T :: SerializeError ;
57
79
type Serialize = Ready < Result < T :: Representation , T :: SerializeError > > ;
@@ -67,6 +89,21 @@ impl<T: Serializer, U: Serialize + DeserializeOwned> Format<U> for Serde<T> {
67
89
}
68
90
}
69
91
92
+ impl < T : ByteSerializer , U : Serialize + DeserializeOwned > ByteFormat < U > for Serde < T > {
93
+ type SerializeError = <T as ByteSerializer >:: SerializeError ;
94
+ type Serialize = Ready < Result < ( ) , <T as ByteSerializer >:: SerializeError > > ;
95
+ type DeserializeError = <T as ByteSerializer >:: DeserializeError ;
96
+ type Deserialize = Ready < Result < U , <T as ByteSerializer >:: DeserializeError > > ;
97
+
98
+ fn serialize < W : AsyncWrite > ( & mut self , writer : & mut W , item : U ) -> Self :: Serialize {
99
+ ready ( ByteSerializer :: serialize ( & mut self . 0 , writer, item) )
100
+ }
101
+
102
+ fn deserialize < R : AsyncRead > ( & mut self , reader : & mut R ) -> Self :: Deserialize {
103
+ ready ( ByteSerializer :: deserialize ( & mut self . 0 , reader) )
104
+ }
105
+ }
106
+
70
107
// impl<T: Serializer> Format<Bottom> for Serde<T> {
71
108
// type Representation = T::Representation;
72
109
// type SerializeError = T::SerializeError;
0 commit comments