From c1e422ee03491be41a2ea27696feab023afa6742 Mon Sep 17 00:00:00 2001 From: James Kominick Date: Mon, 5 Jun 2017 23:25:21 -0400 Subject: [PATCH 1/2] Body constructor doc examples - Add doc examples for `Body` constructors --- src/body.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/body.rs b/src/body.rs index e05cf63..759979d 100644 --- a/src/body.rs +++ b/src/body.rs @@ -21,6 +21,17 @@ impl Body { /// /// A `Body` constructed from a set of bytes, like `String` or `Vec`, /// are stored differently and can be reused. + /// + /// ```rust + /// # use reqwest::Body; + /// # use std::fs::File; + /// # fn run() -> Result<(), Box> { + /// // std::fs::File implements std::io::Read + /// let file = File::open("national_secrets.txt")?; + /// let body = Body::new(file); + /// # Ok(()) + /// # } + /// ``` pub fn new(reader: R) -> Body { Body { reader: Kind::Reader(Box::new(reader), None), @@ -31,6 +42,19 @@ impl Body { /// advance, but where we don't want to load the data in memory. This /// is useful if we need to ensure `Content-Length` is passed with the /// request. + /// + /// ```rust + /// # use reqwest::Body; + /// # fn run() -> Result<(), Box> { + /// // &[u8] implements std::io::Read, and the source `s` has a + /// // 'static lifetime and a known number of bytes. + /// let s = "A predictable body"; + /// let bytes = s.as_bytes(); + /// let size = bytes.len() as u64; + /// let body = Body::sized(bytes, size); + /// # Ok(()) + /// # } + /// ``` pub fn sized(reader: R, len: u64) -> Body { Body { reader: Kind::Reader(Box::new(reader), Some(len)), From ccd2853451f9596f78770ba149d59fe3ec56e3f6 Mon Sep 17 00:00:00 2001 From: James Kominick Date: Tue, 6 Jun 2017 10:27:29 -0400 Subject: [PATCH 2/2] Body constructor doc examples - Use a file & filesize in `Body::sized` example - Point out the available `From` impls on `Body` for constructing reusable `Body`s --- src/body.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/body.rs b/src/body.rs index 759979d..b1c59b5 100644 --- a/src/body.rs +++ b/src/body.rs @@ -19,16 +19,25 @@ impl Body { /// request at the new location, the `Response` will be returned with /// the redirect status code set. /// - /// A `Body` constructed from a set of bytes, like `String` or `Vec`, - /// are stored differently and can be reused. + /// ```rust + /// # use std::fs::File; + /// # use reqwest::Body; + /// # fn run() -> Result<(), Box> { + /// let file = File::open("national_secrets.txt")?; + /// let body = Body::new(file); + /// # Ok(()) + /// # } + /// ``` + /// + /// If you have a set of bytes, like `String` or `Vec`, using the + /// `From` implementations for `Body` will store the data in a manner + /// it can be reused. /// /// ```rust /// # use reqwest::Body; - /// # use std::fs::File; /// # fn run() -> Result<(), Box> { - /// // std::fs::File implements std::io::Read - /// let file = File::open("national_secrets.txt")?; - /// let body = Body::new(file); + /// let s = "A stringy body"; + /// let body = Body::from(s); /// # Ok(()) /// # } /// ``` @@ -44,14 +53,12 @@ impl Body { /// request. /// /// ```rust + /// # use std::fs::File; /// # use reqwest::Body; /// # fn run() -> Result<(), Box> { - /// // &[u8] implements std::io::Read, and the source `s` has a - /// // 'static lifetime and a known number of bytes. - /// let s = "A predictable body"; - /// let bytes = s.as_bytes(); - /// let size = bytes.len() as u64; - /// let body = Body::sized(bytes, size); + /// let file = File::open("a_large_file.txt")?; + /// let file_size = file.metadata()?.len(); + /// let body = Body::sized(file, file_size); /// # Ok(()) /// # } /// ```