forked from rust-lang/async-book
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from huangjj27/update-from-upstream
Update from upstream
- Loading branch information
Showing
39 changed files
with
204 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# https://github.com/rust-lang/async-book/pull/59#issuecomment-556240879 | ||
disable_all_formatting = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_01_02_why_async" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_01_04_async_await_primer" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
name = "example_02_02_future_trait" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_02_03_timer" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_02_04_executor" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_03_01_async_await" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_05_01_streams" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_05_02_iteration_and_concurrency" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_06_02_join" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_06_03_select" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "example_06_04_spawning" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
futures = "0.3" | ||
|
||
[dependencies.async-std] | ||
version = "1.12.0" | ||
features = ["attributes"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![cfg(test)] | ||
#![allow(dead_code)] | ||
|
||
// ANCHOR: example | ||
use async_std::{task, net::TcpListener, net::TcpStream}; | ||
use futures::AsyncWriteExt; | ||
|
||
async fn process_request(stream: &mut TcpStream) -> Result<(), std::io::Error>{ | ||
stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await?; | ||
stream.write_all(b"Hello World").await?; | ||
Ok(()) | ||
} | ||
|
||
async fn main() { | ||
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); | ||
loop { | ||
// Accept a new connection | ||
let (mut stream, _) = listener.accept().await.unwrap(); | ||
// Now process this request without blocking the main loop | ||
task::spawn(async move {process_request(&mut stream).await}); | ||
} | ||
} | ||
// ANCHOR_END: example | ||
use std::time::Duration; | ||
async fn my_task(time: Duration) { | ||
println!("Hello from my_task with time {:?}", time); | ||
task::sleep(time).await; | ||
println!("Goodbye from my_task with time {:?}", time); | ||
} | ||
// ANCHOR: join_all | ||
use futures::future::join_all; | ||
async fn task_spawner(){ | ||
let tasks = vec![ | ||
task::spawn(my_task(Duration::from_secs(1))), | ||
task::spawn(my_task(Duration::from_secs(2))), | ||
task::spawn(my_task(Duration::from_secs(3))), | ||
]; | ||
// If we do not await these tasks and the function finishes, they will be dropped | ||
join_all(tasks).await; | ||
} | ||
// ANCHOR_END: join_all | ||
|
||
#[test] | ||
fn run_task_spawner() { | ||
futures::executor::block_on(task_spawner()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "example_07_05_recursion" | ||
version = "0.1.0" | ||
authors = ["Taylor Cramer <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "sync_tcp_server" | ||
version = "0.1.0" | ||
authors = ["Your Name <[email protected]"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ | |
name = "async_tcp_server" | ||
version = "0.1.0" | ||
authors = ["Your Name <[email protected]"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies.async-std] | ||
version = "1.6" | ||
version = "1.12" | ||
features = ["attributes"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Hello!</title> | ||
</head> | ||
<body> | ||
<h1>Oops!</h1> | ||
<p>Sorry, I don't know what you're asking for.</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,10 @@ | |
name = "slow_request" | ||
version = "0.1.0" | ||
authors = ["Your Name <[email protected]"] | ||
edition = "2018" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies.async-std] | ||
version = "1.6" | ||
version = "1.12" | ||
features = ["attributes"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Hello!</title> | ||
</head> | ||
<body> | ||
<h1>Hello!</h1> | ||
<p>Hi from Rust</p> | ||
</body> | ||
</html> |
Oops, something went wrong.