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
This commit is contained in:
		
							
								
								
									
										31
									
								
								src/body.rs
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								src/body.rs
									
									
									
									
									
								
							| @@ -19,16 +19,25 @@ impl Body { | |||||||
|     /// request at the new location, the `Response` will be returned with |     /// request at the new location, the `Response` will be returned with | ||||||
|     /// the redirect status code set. |     /// the redirect status code set. | ||||||
|     /// |     /// | ||||||
|     /// A `Body` constructed from a set of bytes, like `String` or `Vec<u8>`, |     /// ```rust | ||||||
|     /// are stored differently and can be reused. |     /// # use std::fs::File; | ||||||
|  |     /// # use reqwest::Body; | ||||||
|  |     /// # fn run() -> Result<(), Box<std::error::Error>> { | ||||||
|  |     /// let file = File::open("national_secrets.txt")?; | ||||||
|  |     /// let body = Body::new(file); | ||||||
|  |     /// # Ok(()) | ||||||
|  |     /// # } | ||||||
|  |     /// ``` | ||||||
|  |     /// | ||||||
|  |     /// If you have a set of bytes, like `String` or `Vec<u8>`, using the | ||||||
|  |     /// `From` implementations for `Body` will store the data in a manner | ||||||
|  |     /// it can be reused. | ||||||
|     /// |     /// | ||||||
|     /// ```rust |     /// ```rust | ||||||
|     /// # use reqwest::Body; |     /// # use reqwest::Body; | ||||||
|     /// # use std::fs::File; |  | ||||||
|     /// # fn run() -> Result<(), Box<std::error::Error>> { |     /// # fn run() -> Result<(), Box<std::error::Error>> { | ||||||
|     /// // std::fs::File implements std::io::Read |     /// let s = "A stringy body"; | ||||||
|     /// let file = File::open("national_secrets.txt")?; |     /// let body = Body::from(s); | ||||||
|     /// let body = Body::new(file); |  | ||||||
|     /// # Ok(()) |     /// # Ok(()) | ||||||
|     /// # } |     /// # } | ||||||
|     /// ``` |     /// ``` | ||||||
| @@ -44,14 +53,12 @@ impl Body { | |||||||
|     /// request. |     /// request. | ||||||
|     /// |     /// | ||||||
|     /// ```rust |     /// ```rust | ||||||
|  |     /// # use std::fs::File; | ||||||
|     /// # use reqwest::Body; |     /// # use reqwest::Body; | ||||||
|     /// # fn run() -> Result<(), Box<std::error::Error>> { |     /// # fn run() -> Result<(), Box<std::error::Error>> { | ||||||
|     /// // &[u8] implements std::io::Read, and the source `s` has a |     /// let file = File::open("a_large_file.txt")?; | ||||||
|     /// // 'static lifetime and a known number of bytes. |     /// let file_size = file.metadata()?.len(); | ||||||
|     /// let s = "A predictable body"; |     /// let body = Body::sized(file, file_size); | ||||||
|     /// let bytes = s.as_bytes(); |  | ||||||
|     /// let size  = bytes.len() as u64; |  | ||||||
|     /// let body = Body::sized(bytes, size); |  | ||||||
|     /// # Ok(()) |     /// # Ok(()) | ||||||
|     /// # } |     /// # } | ||||||
|     /// ``` |     /// ``` | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user