11use std:: cell:: { Ref , RefMut } ;
22use std:: task:: { Context , Poll } ;
3- use std:: { fmt, future:: Future , marker:: PhantomData , mem , pin:: Pin , rc:: Rc } ;
3+ use std:: { cell :: Cell , fmt, future:: Future , marker:: PhantomData , pin:: Pin , rc:: Rc } ;
44
55use serde:: de:: DeserializeOwned ;
66
@@ -18,7 +18,7 @@ use super::{ClientConfig, error::JsonPayloadError};
1818/// Client Response
1919pub struct ClientResponse {
2020 pub ( crate ) head : ResponseHead ,
21- pub ( crate ) payload : Payload ,
21+ pub ( crate ) payload : Cell < Option < Payload > > ,
2222 config : Rc < ClientConfig > ,
2323}
2424
@@ -63,8 +63,8 @@ impl ClientResponse {
6363 pub fn new ( head : ResponseHead , payload : Payload , config : Rc < ClientConfig > ) -> Self {
6464 ClientResponse {
6565 head,
66- payload,
6766 config,
67+ payload : Cell :: new ( Some ( payload) ) ,
6868 }
6969 }
7070
@@ -114,13 +114,17 @@ impl ClientResponse {
114114 }
115115
116116 /// Set a body and return previous body value
117- pub fn set_payload ( & mut self , payload : Payload ) {
118- self . payload = payload;
117+ pub fn set_payload ( & self , payload : Payload ) {
118+ self . payload . set ( Some ( payload) ) ;
119119 }
120120
121121 /// Get response's payload
122- pub fn take_payload ( & mut self ) -> Payload {
123- mem:: take ( & mut self . payload )
122+ pub fn take_payload ( & self ) -> Payload {
123+ if let Some ( pl) = self . payload . take ( ) {
124+ pl
125+ } else {
126+ Payload :: None
127+ }
124128 }
125129
126130 /// Request extensions
@@ -138,7 +142,7 @@ impl ClientResponse {
138142
139143impl ClientResponse {
140144 /// Loads http response's body.
141- pub fn body ( & mut self ) -> MessageBody {
145+ pub fn body ( & self ) -> MessageBody {
142146 MessageBody :: new ( self )
143147 }
144148
@@ -149,7 +153,7 @@ impl ClientResponse {
149153 ///
150154 /// * content type is not `application/json`
151155 /// * content length is greater than 256k
152- pub fn json < T : DeserializeOwned > ( & mut self ) -> JsonBody < T > {
156+ pub fn json < T : DeserializeOwned > ( & self ) -> JsonBody < T > {
153157 JsonBody :: new ( self )
154158 }
155159}
@@ -158,7 +162,13 @@ impl Stream for ClientResponse {
158162 type Item = Result < Bytes , PayloadError > ;
159163
160164 fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
161- Pin :: new ( & mut self . get_mut ( ) . payload ) . poll_next ( cx)
165+ if let Some ( mut pl) = self . payload . take ( ) {
166+ let result = Pin :: new ( & mut pl) . poll_next ( cx) ;
167+ self . payload . set ( Some ( pl) ) ;
168+ result
169+ } else {
170+ Poll :: Ready ( None )
171+ }
162172 }
163173}
164174
@@ -183,7 +193,7 @@ pub struct MessageBody {
183193
184194impl MessageBody {
185195 /// Create `MessageBody` for request.
186- pub fn new ( res : & mut ClientResponse ) -> MessageBody {
196+ pub fn new ( res : & ClientResponse ) -> MessageBody {
187197 let mut len = None ;
188198 if let Some ( l) = res. headers ( ) . get ( & CONTENT_LENGTH ) {
189199 if let Ok ( s) = l. to_str ( ) {
@@ -276,7 +286,7 @@ where
276286 U : DeserializeOwned ,
277287{
278288 /// Create `JsonBody` for request.
279- pub fn new ( res : & mut ClientResponse ) -> Self {
289+ pub fn new ( res : & ClientResponse ) -> Self {
280290 // check content-type
281291 let json = if let Ok ( Some ( mime) ) = res. mime_type ( ) {
282292 mime. subtype ( ) == mime:: JSON || mime. suffix ( ) == Some ( mime:: JSON )
@@ -423,24 +433,24 @@ mod tests {
423433
424434 #[ crate :: rt_test]
425435 async fn test_body ( ) {
426- let mut req = TestResponse :: with_header ( header:: CONTENT_LENGTH , "xxxx" ) . finish ( ) ;
436+ let req = TestResponse :: with_header ( header:: CONTENT_LENGTH , "xxxx" ) . finish ( ) ;
427437 match req. body ( ) . await . err ( ) . unwrap ( ) {
428438 PayloadError :: UnknownLength => ( ) ,
429439 _ => unreachable ! ( "error" ) ,
430440 }
431441
432- let mut req = TestResponse :: with_header ( header:: CONTENT_LENGTH , "1000000" ) . finish ( ) ;
442+ let req = TestResponse :: with_header ( header:: CONTENT_LENGTH , "1000000" ) . finish ( ) ;
433443 match req. body ( ) . await . err ( ) . unwrap ( ) {
434444 PayloadError :: Overflow => ( ) ,
435445 _ => unreachable ! ( "error" ) ,
436446 }
437447
438- let mut req = TestResponse :: default ( )
448+ let req = TestResponse :: default ( )
439449 . set_payload ( Bytes :: from_static ( b"test" ) )
440450 . finish ( ) ;
441451 assert_eq ! ( req. body( ) . await . ok( ) . unwrap( ) , Bytes :: from_static( b"test" ) ) ;
442452
443- let mut req = TestResponse :: default ( )
453+ let req = TestResponse :: default ( )
444454 . set_payload ( Bytes :: from_static ( b"11111111111111" ) )
445455 . finish ( ) ;
446456 match req. body ( ) . limit ( 5 ) . await . err ( ) . unwrap ( ) {
@@ -466,20 +476,20 @@ mod tests {
466476
467477 #[ crate :: rt_test]
468478 async fn test_json_body ( ) {
469- let mut req = TestResponse :: default ( ) . finish ( ) ;
470- let json = JsonBody :: < MyObject > :: new ( & mut req) . await ;
479+ let req = TestResponse :: default ( ) . finish ( ) ;
480+ let json = JsonBody :: < MyObject > :: new ( & req) . await ;
471481 assert ! ( json_eq( json. err( ) . unwrap( ) , JsonPayloadError :: ContentType ) ) ;
472482
473- let mut req = TestResponse :: default ( )
483+ let req = TestResponse :: default ( )
474484 . header (
475485 header:: CONTENT_TYPE ,
476486 header:: HeaderValue :: from_static ( "application/text" ) ,
477487 )
478488 . finish ( ) ;
479- let json = JsonBody :: < MyObject > :: new ( & mut req) . await ;
489+ let json = JsonBody :: < MyObject > :: new ( & req) . await ;
480490 assert ! ( json_eq( json. err( ) . unwrap( ) , JsonPayloadError :: ContentType ) ) ;
481491
482- let mut req = TestResponse :: default ( )
492+ let req = TestResponse :: default ( )
483493 . header (
484494 header:: CONTENT_TYPE ,
485495 header:: HeaderValue :: from_static ( "application/json" ) ,
@@ -490,13 +500,13 @@ mod tests {
490500 )
491501 . finish ( ) ;
492502
493- let json = JsonBody :: < MyObject > :: new ( & mut req) . limit ( 100 ) . await ;
503+ let json = JsonBody :: < MyObject > :: new ( & req) . limit ( 100 ) . await ;
494504 assert ! ( json_eq(
495505 json. err( ) . unwrap( ) ,
496506 JsonPayloadError :: Payload ( PayloadError :: Overflow )
497507 ) ) ;
498508
499- let mut req = TestResponse :: default ( )
509+ let req = TestResponse :: default ( )
500510 . header (
501511 header:: CONTENT_TYPE ,
502512 header:: HeaderValue :: from_static ( "application/json" ) ,
@@ -508,7 +518,7 @@ mod tests {
508518 . set_payload ( Bytes :: from_static ( b"{\" name\" : \" test\" }" ) )
509519 . finish ( ) ;
510520
511- let json = JsonBody :: < MyObject > :: new ( & mut req) . await ;
521+ let json = JsonBody :: < MyObject > :: new ( & req) . await ;
512522 assert_eq ! (
513523 json. ok( ) . unwrap( ) ,
514524 MyObject {
0 commit comments