From c0896f19861dca3147fb337adccb65bb43272bb5 Mon Sep 17 00:00:00 2001 From: knight42 Date: Wed, 18 Oct 2017 09:21:36 +0800 Subject: [PATCH] Add Response::copy_to --- src/response.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/response.rs b/src/response.rs index 61394d4..a8f81e3 100644 --- a/src/response.rs +++ b/src/response.rs @@ -165,6 +165,32 @@ impl Response { serde_json::from_reader(self).map_err(::error::from) } + /// Copy the response body into a writer. + /// + /// This function internally uses [`std::io::copy`] and hence will continuously read data from + /// the body and then write it into writer in a streaming fashion until EOF is met. + /// + /// On success, the total number of bytes that were copied to `writer` is returned. + /// [`std::io::copy`]: https://doc.rust-lang.org/std/io/fn.copy.html + /// + /// # Example + /// + /// ```rust + /// # fn run() -> Result<(), Box<::std::error::Error>> { + /// let mut resp = reqwest::get("http://httpbin.org/range/5")?; + /// let mut buf: Vec = vec![]; + /// resp.copy_to(&mut buf)?; + /// assert_eq!(b"abcde", buf.as_slice()); + /// # Ok(()) + /// # } + /// ``` + #[inline] + pub fn copy_to(&mut self, w: &mut W) -> io::Result + where W: io::Write + { + io::copy(self, w) + } + /// Turn a response into an error if the server returned an error. /// /// # Example