Merge branch 'master' into cc

This commit is contained in:
Jia Chao 2022-09-21 16:35:35 +08:00
commit 0cb5e57296
29 changed files with 86 additions and 74 deletions

View File

@ -9,22 +9,17 @@ 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
#- run: bash ci/spellcheck.sh list
- run: cp src/SUMMARY_zh.md src/SUMMARY.md
- 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
- name: Deploy

2
.rustfmt.toml Normal file
View File

@ -0,0 +1,2 @@
# https://github.com/rust-lang/async-book/pull/59#issuecomment-556240879
disable_all_formatting = true

View File

@ -2,7 +2,7 @@
name = "example_01_02_why_async"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -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) {
// ...

View File

@ -2,7 +2,7 @@
name = "example_01_04_async_await_primer"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -2,6 +2,6 @@
name = "example_02_02_future_trait"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -2,7 +2,7 @@
name = "example_02_03_timer"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -2,7 +2,7 @@
name = "example_02_04_executor"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -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,
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

View File

@ -2,7 +2,7 @@
name = "example_03_01_async_await"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -2,7 +2,7 @@
name = "example_05_01_streams"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -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
@ -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

View File

@ -2,7 +2,7 @@
name = "example_05_02_iteration_and_concurrency"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -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

View File

@ -2,7 +2,7 @@
name = "example_06_02_join"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -2,7 +2,7 @@
name = "example_06_03_select"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -2,7 +2,7 @@
name = "example_07_05_recursion"
version = "0.1.0"
authors = ["Taylor Cramer <cramertj@google.com>"]
edition = "2018"
edition = "2021"
[lib]

View File

@ -2,7 +2,7 @@
name = "sync_tcp_server"
version = "0.1.0"
authors = ["Your Name <you@example.com"]
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -2,7 +2,7 @@
name = "async_tcp_server"
version = "0.1.0"
authors = ["Your Name <you@example.com"]
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -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>

View File

@ -2,7 +2,7 @@
name = "slow_request"
version = "0.1.0"
authors = ["Your Name <you@example.com"]
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -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>

View File

@ -2,7 +2,6 @@ use std::fs;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::net::TcpStream;
use std::time::Duration;
#[async_std::main]
async fn main() {
@ -14,6 +13,7 @@ async fn main() {
}
// ANCHOR: handle_connection
use std::time::Duration;
use async_std::task;
async fn handle_connection(mut stream: TcpStream) {

View File

@ -2,7 +2,7 @@
name = "concurrent_tcp_server"
version = "0.1.0"
authors = ["Your Name <you@example.com"]
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -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>

View File

@ -2,7 +2,7 @@
name = "final_tcp_server"
version = "0.1.0"
authors = ["Your Name <you@example.com"]
edition = "2018"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -21,7 +21,6 @@ async fn main() {
// ANCHOR_END: main_func
use async_std::io::{Read, Write};
use std::marker::Unpin;
async fn handle_connection(mut stream: impl Read + Write + Unpin) {
let mut buffer = [0; 1024];
@ -90,7 +89,6 @@ mod tests {
// ANCHOR_END: mock_write
// ANCHOR: unpin
use std::marker::Unpin;
impl Unpin for MockTcpStream {}
// ANCHOR_END: unpin
@ -108,8 +106,6 @@ mod tests {
};
handle_connection(&mut stream).await;
let mut buf = [0u8; 1024];
stream.read(&mut buf).await.unwrap();
let expected_contents = fs::read_to_string("hello.html").unwrap();
let expected_response = format!("HTTP/1.1 200 OK\r\n\r\n{}", expected_contents);

View File

@ -25,7 +25,7 @@ a new dependency:
name = "timer_future"
version = "0.1.0"
authors = ["XYZ Author"]
edition = "2018"
edition = "2021"
[dependencies]
futures = "0.3"

View File

@ -17,7 +17,6 @@ First, we'll change the signature of `handle_connection` to make it easier to te
it requires any struct that implements `async_std::io::Read`, `async_std::io::Write`, and `marker::Unpin`.
Changing the type signature to reflect this allows us to pass a mock for testing.
```rust,ignore
use std::marker::Unpin;
use async_std::io::{Read, Write};
async fn handle_connection(mut stream: impl Read + Write + Unpin) {