When the body type of a `Request` or `Response` implements `HttpBody`, the `Request` or `Response` itself now implements `HttpBody`. This allows writing things like `hyper::body::aggregate(req)` instead of `hyper::body::aggregate(req.into_body())`. Closes #2067
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| #![deny(warnings)]
 | |
| #![warn(rust_2018_idioms)]
 | |
| 
 | |
| #[macro_use]
 | |
| extern crate serde_derive;
 | |
| 
 | |
| use bytes::buf::BufExt as _;
 | |
| use hyper::Client;
 | |
| 
 | |
| // A simple type alias so as to DRY.
 | |
| type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
 | |
| 
 | |
| #[tokio::main]
 | |
| async fn main() -> Result<()> {
 | |
|     let url = "http://jsonplaceholder.typicode.com/users".parse().unwrap();
 | |
|     let users = fetch_json(url).await?;
 | |
|     // print users
 | |
|     println!("users: {:#?}", users);
 | |
| 
 | |
|     // print the sum of ids
 | |
|     let sum = users.iter().fold(0, |acc, user| acc + user.id);
 | |
|     println!("sum of ids: {}", sum);
 | |
|     Ok(())
 | |
| }
 | |
| 
 | |
| async fn fetch_json(url: hyper::Uri) -> Result<Vec<User>> {
 | |
|     let client = Client::new();
 | |
| 
 | |
|     // Fetch the url...
 | |
|     let res = client.get(url).await?;
 | |
| 
 | |
|     // asynchronously aggregate the chunks of the body
 | |
|     let body = hyper::body::aggregate(res).await?;
 | |
| 
 | |
|     // try to parse as json with serde_json
 | |
|     let users = serde_json::from_reader(body.reader())?;
 | |
| 
 | |
|     Ok(users)
 | |
| }
 | |
| 
 | |
| #[derive(Deserialize, Debug)]
 | |
| struct User {
 | |
|     id: i32,
 | |
|     name: String,
 | |
| }
 |