From 9370cd329885fc0f6c559629f16f964d892d3c9b Mon Sep 17 00:00:00 2001 From: Florian Gilcher Date: Mon, 26 Aug 2019 11:08:51 -0700 Subject: [PATCH] Fix README example --- README.md | 4 ++-- examples/socket-timeouts.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 examples/socket-timeouts.rs diff --git a/README.md b/README.md index b3f4189..3ba7c5f 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,9 @@ async fn get() -> io::Result> { let mut buf = vec![]; io::timeout(Duration::from_secs(5), async { - stream.read_to_end(&mut buf).await? + stream.read_to_end(&mut buf).await?; Ok(buf) - }) + }).await } fn main() { diff --git a/examples/socket-timeouts.rs b/examples/socket-timeouts.rs new file mode 100644 index 0000000..b5cb9be --- /dev/null +++ b/examples/socket-timeouts.rs @@ -0,0 +1,29 @@ +use std::time::Duration; + +use async_std::{ + prelude::*, + task, + io, + net::TcpStream, +}; + +async fn get() -> io::Result> { + let mut stream = TcpStream::connect("example.com:80").await?; + stream.write_all(b"GET /index.html HTTP/1.0\r\n\r\n").await?; + + let mut buf = vec![]; + + io::timeout(Duration::from_secs(5), async { + stream.read_to_end(&mut buf).await?; + Ok(buf) + }).await +} + +fn main() { + task::block_on(async { + let raw_response = get().await.expect("request"); + let response = String::from_utf8(raw_response) + .expect("utf8 conversion"); + println!("received: {}", response); + }); +}