From ccd2853451f9596f78770ba149d59fe3ec56e3f6 Mon Sep 17 00:00:00 2001 From: James Kominick Date: Tue, 6 Jun 2017 10:27:29 -0400 Subject: [PATCH] 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(()) /// # } /// ```