Skip to content

Commit c71abe5

Browse files
weihangloseanmonstar
authored andcommitted
docs(client): doc tests to async/await
1 parent e90f003 commit c71abe5

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

src/client/mod.rs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,34 @@
2727
//! [full client example](https://github.com/hyperium/hyper/blob/master/examples/client.rs).
2828
//!
2929
//! ```
30-
//! extern crate hyper;
31-
//!
30+
//! # #![feature(async_await)]
3231
//! use hyper::{Client, Uri};
33-
//! # #[cfg(feature = "runtime")]
34-
//! use hyper::rt::{self, Future, Stream};
3532
//!
3633
//! # #[cfg(feature = "runtime")]
37-
//! # fn fetch_httpbin() {
34+
//! # async fn fetch_httpbin() -> hyper::Result<()> {
3835
//! let client = Client::new();
3936
//!
40-
//! let fut = client
41-
//!
42-
//! // Make a GET /ip to 'http://httpbin.org'
43-
//! .get(Uri::from_static("http://httpbin.org/ip"))
44-
//!
45-
//! // And then, if the request gets a response...
46-
//! .and_then(|res| {
47-
//! println!("status: {}", res.status());
48-
//!
49-
//! // Concatenate the body stream into a single buffer...
50-
//! // This returns a new future, since we must stream body.
51-
//! res.into_body().concat2()
52-
//! })
53-
//!
54-
//! // And then, if reading the full body succeeds...
55-
//! .and_then(|body| {
56-
//! // The body is just bytes, but let's print a string...
57-
//! let s = ::std::str::from_utf8(&body)
58-
//! .expect("httpbin sends utf-8 JSON");
37+
//! // Make a GET /ip to 'http://httpbin.org'
38+
//! let res = client.get(Uri::from_static("http://httpbin.org/ip")).await?;
5939
//!
60-
//! println!("body: {}", s);
40+
//! // And then, if the request gets a response...
41+
//! println!("status: {}", res.status());
6142
//!
62-
//! // and_then requires we return a new Future, and it turns
63-
//! // out that Result is a Future that is ready immediately.
64-
//! Ok(())
65-
//! })
43+
//! // Concatenate the body stream into a single buffer...
44+
//! let mut body = res.into_body();
45+
//! let mut bytes = Vec::new();
46+
//! while let Some(next) = body.next().await {
47+
//! let chunk = next?;
48+
//! bytes.extend(chunk);
49+
//! }
6650
//!
67-
//! // Map any errors that might have happened...
68-
//! .map_err(|err| {
69-
//! println!("error: {}", err);
70-
//! });
51+
//! // And then, if reading the full body succeeds...
52+
//! // The body is just bytes, but let's print a string...
53+
//! let s = std::str::from_utf8(&bytes)
54+
//! .expect("httpbin sends utf-8 JSON");
7155
//!
72-
//! // A runtime is needed to execute our asynchronous code. In order to
73-
//! // spawn the future into the runtime, it should already have been
74-
//! // started and running before calling this code.
75-
//! rt::spawn(fut);
56+
//! println!("body: {}", s);
57+
//! # Ok(())
7658
//! # }
7759
//! # fn main () {}
7860
//! ```

0 commit comments

Comments
 (0)