@@ -4,24 +4,30 @@ use std::fmt::{self, Debug, Formatter};
4
4
5
5
use futures_util:: StreamExt ;
6
6
use quinn:: { RecvStream , SendStream } ;
7
- use serde :: { de :: DeserializeOwned , Serialize } ;
7
+ use transmog :: { Format , OwnedDeserializer } ;
8
8
9
9
use super :: ReceiverStream ;
10
- use crate :: { error, Receiver , Sender } ;
10
+ use crate :: {
11
+ error:: { self , SerializationError } ,
12
+ Receiver , Sender ,
13
+ } ;
11
14
12
15
/// An intermediate state to define which type to accept in this stream. See
13
16
/// [`accept_stream`](Self::accept).
14
17
#[ must_use = "`Incoming` does nothing unless accepted with `Incoming::accept`" ]
15
- pub struct Incoming < T : DeserializeOwned > {
18
+ pub struct Incoming < T , F : OwnedDeserializer < T > > {
16
19
/// [`SendStream`] to build [`Sender`].
17
20
sender : SendStream ,
18
21
/// [`RecvStream`] to build [`Receiver`].
19
- receiver : ReceiverStream < T > ,
22
+ receiver : ReceiverStream < T , F > ,
20
23
/// Requested type.
21
24
r#type : Option < Result < T , error:: Incoming > > ,
22
25
}
23
26
24
- impl < T : DeserializeOwned > Debug for Incoming < T > {
27
+ impl < T , F > Debug for Incoming < T , F >
28
+ where
29
+ F : OwnedDeserializer < T > ,
30
+ {
25
31
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
26
32
f. debug_struct ( "Incoming" )
27
33
. field ( "sender" , & self . sender )
@@ -31,12 +37,16 @@ impl<T: DeserializeOwned> Debug for Incoming<T> {
31
37
}
32
38
}
33
39
34
- impl < T : DeserializeOwned > Incoming < T > {
40
+ impl < T , F > Incoming < T , F >
41
+ where
42
+ F : OwnedDeserializer < T > + Clone ,
43
+ F :: Error : SerializationError ,
44
+ {
35
45
/// Builds a new [`Incoming`] from raw [`quinn`] types.
36
- pub ( super ) fn new ( sender : SendStream , receiver : RecvStream ) -> Self {
46
+ pub ( super ) fn new ( sender : SendStream , receiver : RecvStream , format : F ) -> Self {
37
47
Self {
38
48
sender,
39
- receiver : ReceiverStream :: new ( receiver) ,
49
+ receiver : ReceiverStream :: new ( receiver, format ) ,
40
50
r#type : None ,
41
51
}
42
52
}
@@ -80,12 +90,57 @@ impl<T: DeserializeOwned> Incoming<T> {
80
90
/// - [`error::Incoming::Receiver`] if receiving the type information to the
81
91
/// peer failed, see [`error::Receiver`] for more details
82
92
/// - [`error::Incoming::Closed`] if the stream was closed
83
- pub async fn accept <
84
- S : DeserializeOwned + Serialize + Send + ' static ,
85
- R : DeserializeOwned + Serialize + Send + ' static ,
86
- > (
93
+ pub async fn accept < S : Send + ' static , R : Send + ' static > (
94
+ self ,
95
+ ) -> Result < ( Sender < S , F > , Receiver < R > ) , error:: Incoming >
96
+ where
97
+ F : OwnedDeserializer < R > + Format < ' static , S > + ' static ,
98
+ <F as Format < ' static , S > >:: Error : SerializationError ,
99
+ <F as Format < ' static , R > >:: Error : SerializationError ,
100
+ {
101
+ let format = self . receiver . format . clone ( ) ;
102
+ self . accept_with_format ( format) . await
103
+ }
104
+
105
+ /// Accept the incoming stream with the given types.
106
+ ///
107
+ /// Use `S` and `R` to define which type this stream is sending and
108
+ /// receiving.
109
+ ///
110
+ /// # Errors
111
+ /// - [`error::Incoming::Receiver`] if receiving the type information to the
112
+ /// peer failed, see [`error::Receiver`] for more details
113
+ /// - [`error::Incoming::Closed`] if the stream was closed
114
+ pub async fn accept_raw < S : Send + ' static , R : Send + ' static > (
115
+ self ,
116
+ ) -> Result < ( Sender < S , F > , Receiver < R > ) , error:: Incoming >
117
+ where
118
+ F : OwnedDeserializer < R > + Format < ' static , S > + ' static ,
119
+ <F as Format < ' static , S > >:: Error : SerializationError ,
120
+ <F as Format < ' static , R > >:: Error : SerializationError ,
121
+ {
122
+ let format = self . receiver . format . clone ( ) ;
123
+ self . accept_with_format ( format) . await
124
+ }
125
+
126
+ /// Accept the incoming stream with the given types.
127
+ ///
128
+ /// Use `S` and `R` to define which type this stream is sending and
129
+ /// receiving.
130
+ ///
131
+ /// # Errors
132
+ /// - [`error::Incoming::Receiver`] if receiving the type information to the
133
+ /// peer failed, see [`error::Receiver`] for more details
134
+ /// - [`error::Incoming::Closed`] if the stream was closed
135
+ pub async fn accept_with_format < S : Send + ' static , R : Send + ' static , NewFormat > (
87
136
mut self ,
88
- ) -> Result < ( Sender < S > , Receiver < R > ) , error:: Incoming > {
137
+ format : NewFormat ,
138
+ ) -> Result < ( Sender < S , NewFormat > , Receiver < R > ) , error:: Incoming >
139
+ where
140
+ NewFormat : OwnedDeserializer < R > + Format < ' static , S > + Clone + ' static ,
141
+ <NewFormat as Format < ' static , S > >:: Error : SerializationError ,
142
+ <NewFormat as Format < ' static , R > >:: Error : SerializationError ,
143
+ {
89
144
match self . r#type {
90
145
Some ( Ok ( _) ) => ( ) ,
91
146
Some ( Err ( error) ) => return Err ( error) ,
@@ -100,8 +155,8 @@ impl<T: DeserializeOwned> Incoming<T> {
100
155
}
101
156
}
102
157
103
- let sender = Sender :: new ( self . sender ) ;
104
- let receiver = Receiver :: new ( self . receiver . transmute ( ) ) ;
158
+ let sender = Sender :: new ( self . sender , format . clone ( ) ) ;
159
+ let receiver = Receiver :: new ( self . receiver . transmute ( format ) ) ;
105
160
106
161
Ok ( ( sender, receiver) )
107
162
}
0 commit comments