@@ -98,8 +98,7 @@ pub type ChunkedTextStream = Streaming<String, CborEncoding>;
9898///
9999/// Also note that not all browsers support streaming bodies to servers.
100100pub struct Streaming < T = String , E = ( ) > {
101- output_stream : Pin < Box < dyn Stream < Item = Result < T , StreamingError > > + Send > > ,
102- input_stream : Pin < Box < dyn Stream < Item = Result < T , StreamingError > > + Send > > ,
101+ stream : Pin < Box < dyn Stream < Item = Result < T , StreamingError > > + Send > > ,
103102 encoding : PhantomData < E > ,
104103}
105104
@@ -123,9 +122,8 @@ impl<T: 'static + Send, E> Streaming<T, E> {
123122 pub fn new ( value : impl Stream < Item = T > + Send + ' static ) -> Self {
124123 // Box and pin the incoming stream and store as a trait object
125124 Self {
126- input_stream : Box :: pin ( value. map ( |item| Ok ( item) ) )
125+ stream : Box :: pin ( value. map ( |item| Ok ( item) ) )
127126 as Pin < Box < dyn Stream < Item = Result < T , StreamingError > > + Send > > ,
128- output_stream : Box :: pin ( futures:: stream:: empty ( ) ) as _ ,
129127 encoding : PhantomData ,
130128 }
131129 }
@@ -148,21 +146,20 @@ impl<T: 'static + Send, E> Streaming<T, E> {
148146
149147 /// Returns the next item in the stream, or `None` if the stream has ended.
150148 pub async fn next ( & mut self ) -> Option < Result < T , StreamingError > > {
151- self . output_stream . as_mut ( ) . next ( ) . await
149+ self . stream . as_mut ( ) . next ( ) . await
152150 }
153151
154152 /// Consumes the wrapper, returning the inner stream.
155153 pub fn into_inner ( self ) -> impl Stream < Item = Result < T , StreamingError > > + Send {
156- self . input_stream
154+ self . stream
157155 }
158156
159157 /// Creates a streaming payload from an existing stream of bytes.
160158 ///
161159 /// This uses the internal framing mechanism to decode the stream into items of type `T`.
162160 fn from_bytes ( stream : impl Stream < Item = Result < T , StreamingError > > + Send + ' static ) -> Self {
163161 Self {
164- input_stream : Box :: pin ( stream) ,
165- output_stream : Box :: pin ( futures:: stream:: empty ( ) ) as _ ,
162+ stream : Box :: pin ( stream) ,
166163 encoding : PhantomData ,
167164 }
168165 }
@@ -184,8 +181,7 @@ where
184181{
185182 fn from ( value : S ) -> Self {
186183 Self {
187- input_stream : Box :: pin ( value. map ( |data| data. map_err ( |_| StreamingError :: Failed ) ) ) ,
188- output_stream : Box :: pin ( futures:: stream:: empty ( ) ) as _ ,
184+ stream : Box :: pin ( value. map ( |data| data. map_err ( |_| StreamingError :: Failed ) ) ) ,
189185 encoding : PhantomData ,
190186 }
191187 }
@@ -207,7 +203,7 @@ impl IntoResponse for Streaming<String> {
207203 fn into_response ( self ) -> axum_core:: response:: Response {
208204 axum:: response:: Response :: builder ( )
209205 . header ( "Content-Type" , "text/plain; charset=utf-8" )
210- . body ( axum:: body:: Body :: from_stream ( self . input_stream ) )
206+ . body ( axum:: body:: Body :: from_stream ( self . stream ) )
211207 . unwrap ( )
212208 }
213209}
@@ -216,14 +212,14 @@ impl IntoResponse for Streaming<Bytes> {
216212 fn into_response ( self ) -> axum_core:: response:: Response {
217213 axum:: response:: Response :: builder ( )
218214 . header ( "Content-Type" , "application/octet-stream" )
219- . body ( axum:: body:: Body :: from_stream ( self . input_stream ) )
215+ . body ( axum:: body:: Body :: from_stream ( self . stream ) )
220216 . unwrap ( )
221217 }
222218}
223219
224220impl < T : DeserializeOwned + Serialize + ' static , E : Encoding > IntoResponse for Streaming < T , E > {
225221 fn into_response ( self ) -> axum_core:: response:: Response {
226- let res = self . input_stream . map ( |r| match r {
222+ let res = self . stream . map ( |r| match r {
227223 Ok ( res) => match encode_stream_frame :: < T , E > ( res) {
228224 Some ( bytes) => Ok ( bytes) ,
229225 None => Err ( StreamingError :: Failed ) ,
@@ -250,8 +246,7 @@ impl FromResponse for Streaming<String> {
250246 } ) ) ;
251247
252248 Ok ( Self {
253- output_stream : client_stream,
254- input_stream : Box :: pin ( futures:: stream:: empty ( ) ) ,
249+ stream : client_stream,
255250 encoding : PhantomData ,
256251 } )
257252 } )
@@ -269,8 +264,7 @@ impl FromResponse for Streaming<Bytes> {
269264 ) ) ) ;
270265
271266 Ok ( Self {
272- output_stream : client_stream,
273- input_stream : Box :: pin ( futures:: stream:: empty ( ) ) ,
267+ stream : client_stream,
274268 encoding : PhantomData ,
275269 } )
276270 }
@@ -293,8 +287,7 @@ impl<T: DeserializeOwned + Serialize + 'static + Send, E: Encoding> FromResponse
293287 ) ) ) ;
294288
295289 Ok ( Self {
296- output_stream : client_stream,
297- input_stream : Box :: pin ( futures:: stream:: empty ( ) ) ,
290+ stream : client_stream,
298291 encoding : PhantomData ,
299292 } )
300293 } )
@@ -323,8 +316,7 @@ impl<S> FromRequest<S> for Streaming<String> {
323316 let stream = body. into_data_stream ( ) ;
324317
325318 Ok ( Self {
326- input_stream : Box :: pin ( futures:: stream:: empty ( ) ) ,
327- output_stream : Box :: pin ( stream. map ( |byte| match byte {
319+ stream : Box :: pin ( stream. map ( |byte| match byte {
328320 Ok ( bytes) => match String :: from_utf8 ( bytes. to_vec ( ) ) {
329321 Ok ( string) => Ok ( string) ,
330322 Err ( _) => Err ( StreamingError :: Decoding ) ,
@@ -359,8 +351,7 @@ impl<S> FromRequest<S> for ByteStream {
359351 let stream = body. into_data_stream ( ) ;
360352
361353 Ok ( Self {
362- input_stream : Box :: pin ( futures:: stream:: empty ( ) ) ,
363- output_stream : Box :: pin ( stream. map ( |byte| match byte {
354+ stream : Box :: pin ( stream. map ( |byte| match byte {
364355 Ok ( bytes) => Ok ( bytes) ,
365356 Err ( _) => Err ( StreamingError :: Failed ) ,
366357 } ) ) ,
@@ -394,8 +385,7 @@ impl<T: DeserializeOwned + Serialize + 'static + Send, E: Encoding, S> FromReque
394385 let stream = body. into_data_stream ( ) ;
395386
396387 Ok ( Self {
397- input_stream : Box :: pin ( futures:: stream:: empty ( ) ) ,
398- output_stream : Box :: pin ( stream. map ( |byte| match byte {
388+ stream : Box :: pin ( stream. map ( |byte| match byte {
399389 Ok ( bytes) => match decode_stream_frame :: < T , E > ( bytes) {
400390 Some ( res) => Ok ( res) ,
401391 None => Err ( StreamingError :: Decoding ) ,
@@ -416,7 +406,7 @@ impl IntoRequest for Streaming<String> {
416406 async move {
417407 builder
418408 . header ( "Content-Type" , "text/plain; charset=utf-8" ) ?
419- . send_body_stream ( self . input_stream . map ( |e| e. map ( Bytes :: from) ) )
409+ . send_body_stream ( self . stream . map ( |e| e. map ( Bytes :: from) ) )
420410 . await
421411 }
422412 }
@@ -430,7 +420,7 @@ impl IntoRequest for ByteStream {
430420 async move {
431421 builder
432422 . header ( ContentType :: name ( ) , "application/octet-stream" ) ?
433- . send_body_stream ( self . input_stream )
423+ . send_body_stream ( self . stream )
434424 . await
435425 }
436426 }
@@ -446,7 +436,7 @@ impl<T: DeserializeOwned + Serialize + 'static + Send, E: Encoding> IntoRequest
446436 async move {
447437 builder
448438 . header ( "Content-Type" , E :: stream_content_type ( ) ) ?
449- . send_body_stream ( self . input_stream . map ( |r| {
439+ . send_body_stream ( self . stream . map ( |r| {
450440 r. and_then ( |item| {
451441 encode_stream_frame :: < T , E > ( item) . ok_or ( StreamingError :: Failed )
452442 } )
0 commit comments