@@ -4,24 +4,30 @@ use std::fmt::{self, Debug, Formatter};
44
55use futures_util:: StreamExt ;
66use quinn:: { RecvStream , SendStream } ;
7- use serde :: { de :: DeserializeOwned , Serialize } ;
7+ use transmog :: { Format , OwnedDeserializer } ;
88
99use super :: ReceiverStream ;
10- use crate :: { error, Receiver , Sender } ;
10+ use crate :: {
11+ error:: { self , SerializationError } ,
12+ Receiver , Sender ,
13+ } ;
1114
1215/// An intermediate state to define which type to accept in this stream. See
1316/// [`accept_stream`](Self::accept).
1417#[ must_use = "`Incoming` does nothing unless accepted with `Incoming::accept`" ]
15- pub struct Incoming < T : DeserializeOwned > {
18+ pub struct Incoming < T , F : OwnedDeserializer < T > > {
1619 /// [`SendStream`] to build [`Sender`].
1720 sender : SendStream ,
1821 /// [`RecvStream`] to build [`Receiver`].
19- receiver : ReceiverStream < T > ,
22+ receiver : ReceiverStream < T , F > ,
2023 /// Requested type.
2124 r#type : Option < Result < T , error:: Incoming > > ,
2225}
2326
24- impl < T : DeserializeOwned > Debug for Incoming < T > {
27+ impl < T , F > Debug for Incoming < T , F >
28+ where
29+ F : OwnedDeserializer < T > ,
30+ {
2531 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
2632 f. debug_struct ( "Incoming" )
2733 . field ( "sender" , & self . sender )
@@ -31,12 +37,16 @@ impl<T: DeserializeOwned> Debug for Incoming<T> {
3137 }
3238}
3339
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+ {
3545 /// 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 {
3747 Self {
3848 sender,
39- receiver : ReceiverStream :: new ( receiver) ,
49+ receiver : ReceiverStream :: new ( receiver, format ) ,
4050 r#type : None ,
4151 }
4252 }
@@ -80,12 +90,57 @@ impl<T: DeserializeOwned> Incoming<T> {
8090 /// - [`error::Incoming::Receiver`] if receiving the type information to the
8191 /// peer failed, see [`error::Receiver`] for more details
8292 /// - [`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 > (
87136 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+ {
89144 match self . r#type {
90145 Some ( Ok ( _) ) => ( ) ,
91146 Some ( Err ( error) ) => return Err ( error) ,
@@ -100,8 +155,8 @@ impl<T: DeserializeOwned> Incoming<T> {
100155 }
101156 }
102157
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 ) ) ;
105160
106161 Ok ( ( sender, receiver) )
107162 }
0 commit comments