make body return borrowed decoder
This commit is contained in:
		| @@ -66,6 +66,13 @@ pub struct Chunk { | ||||
|     inner: ::hyper::Chunk, | ||||
| } | ||||
|  | ||||
| impl AsRef<[u8]> for Chunk { | ||||
|     #[inline] | ||||
|     fn as_ref(&self) -> &[u8] { | ||||
|         &*self | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl ::std::ops::Deref for Chunk { | ||||
|     type Target = [u8]; | ||||
|     #[inline] | ||||
|   | ||||
| @@ -82,6 +82,19 @@ impl fmt::Debug for Decoder { | ||||
| } | ||||
|  | ||||
| impl Decoder { | ||||
|     /// An empty decoder. | ||||
|     ///  | ||||
|     /// This decoder will produce a single 0 byte chunk. | ||||
|     #[inline] | ||||
|     pub fn empty() -> Decoder { | ||||
|         Decoder { | ||||
|             inner: Inner::PlainText(body::empty()) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /// A plain text decoder. | ||||
|     ///  | ||||
|     /// This decoder will emit the underlying chunks as-is. | ||||
|     #[inline] | ||||
|     fn plain_text(body: Body) -> Decoder { | ||||
|         Decoder { | ||||
| @@ -89,6 +102,9 @@ impl Decoder { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /// A gzip decoder. | ||||
|     ///  | ||||
|     /// This decoder will buffer and decompress chunks that are gzipped. | ||||
|     #[inline] | ||||
|     fn gzip(mut body: Body) -> Decoder { | ||||
|         Decoder { | ||||
| @@ -385,14 +401,6 @@ impl<S> ReadableChunks<S> | ||||
|  | ||||
| // pub(crate) | ||||
|  | ||||
| #[inline] | ||||
| pub fn take(decoder: &mut Decoder) -> Decoder { | ||||
|     let inner = mem::replace(&mut decoder.inner, Inner::PlainText(body::empty())); | ||||
|     Decoder { | ||||
|         inner: inner, | ||||
|     } | ||||
| } | ||||
|  | ||||
| /// Constructs a Decoder from a hyper request. | ||||
| /// | ||||
| /// A decoder is just a wrapper around the hyper request that knows | ||||
|   | ||||
| @@ -63,17 +63,17 @@ impl Response { | ||||
|     ///  | ||||
|     /// This function will replace the body on the response with an empty one. | ||||
|     #[inline] | ||||
|     pub fn body(&mut self) -> Decoder { | ||||
|         decoder::take(&mut self.body) | ||||
|     pub fn body(&self) -> &Decoder { | ||||
|         &self.body | ||||
|     } | ||||
|  | ||||
|     /// Try to deserialize the response body as JSON using `serde`. | ||||
|     #[inline] | ||||
|     pub fn json<T: DeserializeOwned>(&mut self) -> Json<T> { | ||||
|         let body = self.body().concat2(); | ||||
|  | ||||
|         let body = mem::replace(&mut self.body, Decoder::empty()); | ||||
|          | ||||
|         Json { | ||||
|             concat: body, | ||||
|             concat: body.concat2(), | ||||
|             _marker: PhantomData, | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -172,6 +172,7 @@ pub mod unstable { | ||||
|         pub use ::async_impl::{ | ||||
|             Body, | ||||
|             Chunk, | ||||
|             Decoder, | ||||
|             Client, | ||||
|             ClientBuilder, | ||||
|             Request, | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| use std::mem; | ||||
| use std::fmt; | ||||
| use std::io::{self, Read}; | ||||
| use std::time::Duration; | ||||
| @@ -182,7 +183,7 @@ impl Response { | ||||
|     /// ``` | ||||
|     #[inline] | ||||
|     pub fn error_for_status(self) -> ::Result<Self> { | ||||
|         let Response { inner, body, _thread_handle } = self; | ||||
|         let Response { body, inner, _thread_handle } = self; | ||||
|         inner.error_for_status().map(move |inner| { | ||||
|             Response { | ||||
|                 inner: inner, | ||||
| @@ -227,8 +228,9 @@ impl Stream for WaitBody { | ||||
| // pub(crate) | ||||
|  | ||||
| pub fn new(mut res: async_impl::Response, timeout: Option<Duration>, thread: KeepCoreThreadAlive) -> Response { | ||||
|     let body = mem::replace(res.body_mut(), async_impl::Decoder::empty()); | ||||
|     let body = async_impl::ReadableChunks::new(WaitBody { | ||||
|         inner: wait::stream(res.body(), timeout) | ||||
|         inner: wait::stream(body, timeout) | ||||
|     }); | ||||
|  | ||||
|     Response { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user