Skip to content

Commit

Permalink
Merge pull request #94 from huangjj27/update-from-upstream
Browse files Browse the repository at this point in the history
Update from upstream
  • Loading branch information
huangjj27 authored Dec 6, 2023
2 parents bf42e8f + c13bebe commit 915baaa
Show file tree
Hide file tree
Showing 39 changed files with 204 additions and 91 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/ci.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ on:
jobs:
test:
name: build and test
env:
MDBOOK_LINKCHECK_VERSION: 0.7.0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust
run: rustup update stable && rustup default stable
- run: sudo apt-get update && sudo apt-get install aspell aspell-en
- name: Install mdbook and mdbook-linkcheck
run: |
tag=$(curl -LsSf https://api.github.com/repos/rust-lang/mdBook/releases/latest | jq -r '.tag_name')
curl -LsSf https://github.com/rust-lang/mdBook/releases/download/$tag/mdbook-$tag-x86_64-unknown-linux-gnu.tar.gz | tar xzf -
curl -LsSf https://github.com/Michael-F-Bryan/mdbook-linkcheck/releases/download/v${MDBOOK_LINKCHECK_VERSION}/mdbook-linkcheck-v${MDBOOK_LINKCHECK_VERSION}-x86_64-unknown-linux-gnu.tar.gz | tar xzf -
echo $(pwd) >> $GITHUB_PATH
- name: Install mdbook
uses: taiki-e/install-action@mdbook
- name: Install mdbook-linkcheck
uses: taiki-e/install-action@mdbook-linkcheck
- run: bash ci/spellcheck.sh list
- run: mdbook build
- run: cargo test --all --manifest-path=./examples/Cargo.toml --target-dir ./target
Expand Down
2 changes: 2 additions & 0 deletions .rustfmt.toml
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
2 changes: 2 additions & 0 deletions ci/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ AsyncRead
AsyncWrite
AwaitingFutOne
AwaitingFutTwo
cancelling
combinator
combinators
compat
Expand Down Expand Up @@ -37,6 +38,7 @@ interprocess
IoBlocker
IOCP
IoObject
JoinHandle
kqueue
localhost
LocalExecutor
Expand Down
2 changes: 1 addition & 1 deletion examples/01_02_why_async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_01_02_why_async"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
9 changes: 2 additions & 7 deletions examples/01_02_why_async/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#![cfg(test)]

use {
futures::{
executor::block_on,
join,
},
std::thread,
};
use futures::{executor::block_on, join};
use std::thread;

fn download(_url: &str) {
// ...
Expand Down
2 changes: 1 addition & 1 deletion examples/01_04_async_await_primer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion examples/02_02_future_trait/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
name = "example_02_02_future_trait"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]
2 changes: 1 addition & 1 deletion examples/02_03_timer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_02_03_timer"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
2 changes: 1 addition & 1 deletion examples/02_04_executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_02_04_executor"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
28 changes: 13 additions & 15 deletions examples/02_04_executor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#![cfg(test)]

// ANCHOR: imports
use {
futures::{
future::{BoxFuture, FutureExt},
task::{waker_ref, ArcWake},
},
std::{
future::Future,
sync::mpsc::{sync_channel, Receiver, SyncSender},
sync::{Arc, Mutex},
task::{Context, Poll},
time::Duration,
},
// The timer we wrote in the previous section:
timer_future::TimerFuture,
use futures::{
future::{BoxFuture, FutureExt},
task::{waker_ref, ArcWake},
};
use std::{
future::Future,
sync::mpsc::{sync_channel, Receiver, SyncSender},
sync::{Arc, Mutex},
task::Context,
time::Duration,
};
// The timer we wrote in the previous section:
use timer_future::TimerFuture;
// ANCHOR_END: imports

// ANCHOR: executor_decl
Expand Down Expand Up @@ -92,7 +90,7 @@ impl Executor {
if let Some(mut future) = future_slot.take() {
// Create a `LocalWaker` from the task itself
let waker = waker_ref(&task);
let context = &mut Context::from_waker(&*waker);
let context = &mut Context::from_waker(&waker);
// `BoxFuture<T>` is a type alias for
// `Pin<Box<dyn Future<Output = T> + Send + 'static>>`.
// We can get a `Pin<&mut dyn Future + Send + 'static>`
Expand Down
2 changes: 1 addition & 1 deletion examples/03_01_async_await/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_03_01_async_await"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
2 changes: 1 addition & 1 deletion examples/05_01_streams/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_05_01_streams"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
18 changes: 7 additions & 11 deletions examples/05_01_streams/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#![cfg(test)]

mod stream_trait {
use {
futures::stream::{Stream as RealStream},
std::{
pin::Pin,
task::{Context, Poll},
},
use futures::stream::Stream as RealStream;
use std::{
pin::Pin,
task::{Context, Poll},
};

// ANCHOR: stream_trait
Expand Down Expand Up @@ -34,11 +32,9 @@ impl<I> Stream for dyn RealStream<Item = I> {
}

mod channels {
use {
futures::{
channel::mpsc,
prelude::*,
},
use futures::{
channel::mpsc,
prelude::*,
};

// ANCHOR: channels
Expand Down
2 changes: 1 addition & 1 deletion examples/05_02_iteration_and_concurrency/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
16 changes: 7 additions & 9 deletions examples/05_02_iteration_and_concurrency/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#![cfg(test)]

use {
futures::{
executor::block_on,
stream::{self, Stream},
},
std::{
io,
pin::Pin,
},
use futures::{
executor::block_on,
stream::{self, Stream},
};
use std::{
io,
pin::Pin,
};

// ANCHOR: nexts
Expand Down
2 changes: 1 addition & 1 deletion examples/06_02_join/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_06_02_join"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
2 changes: 1 addition & 1 deletion examples/06_03_select/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_06_03_select"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
7 changes: 0 additions & 7 deletions examples/06_03_select/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ async fn get_new_num() -> u8 { /* ... */ 5 }

async fn run_on_new_num(_: u8) -> u8 { /* ... */ 5 }

// Runs `run_on_new_num` with the latest number
// retrieved from `get_new_num`.
//
// `get_new_num` is re-run every time a timer elapses,
// immediately cancelling the currently running
// `run_on_new_num` and replacing it with the newly
// returned value.
async fn run_loop(
mut interval_timer: impl Stream<Item = ()> + FusedStream + Unpin,
starting_num: u8,
Expand Down
13 changes: 13 additions & 0 deletions examples/06_04_spawning/Cargo.toml
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"]
46 changes: 46 additions & 0 deletions examples/06_04_spawning/src/lib.rs
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());
}
2 changes: 1 addition & 1 deletion examples/07_05_recursion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "example_07_05_recursion"
version = "0.1.0"
authors = ["Taylor Cramer <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]

Expand Down
2 changes: 1 addition & 1 deletion examples/09_01_sync_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions examples/09_02_async_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
11 changes: 11 additions & 0 deletions examples/09_03_slow_request/404.html
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>
4 changes: 2 additions & 2 deletions examples/09_03_slow_request/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
11 changes: 11 additions & 0 deletions examples/09_03_slow_request/hello.html
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>
Loading

0 comments on commit 915baaa

Please sign in to comment.