Skip to content

Commit 0e400af

Browse files
carllercheDavid Barsky
authored and
David Barsky
committed
Async/await polish (tokio-rs#1058)
A general refresh of Tokio's experimental async / await support.
1 parent df70213 commit 0e400af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+403
-214
lines changed

.cirrus.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ task:
3131
folder: $HOME/.cargo/registry
3232
test_script:
3333
- . $HOME/.cargo/env
34-
- cargo test --all --no-fail-fast
34+
- cargo test --all
3535
- (cd tokio-trace/test-log-support && cargo test)
3636
- (cd tokio-trace/test_static_max_level_features && cargo test)
3737
- cargo doc --all
3838
i686_test_script:
3939
- . $HOME/.cargo/env
4040
- |
41-
cargo test --all --exclude tokio-tls --no-fail-fast --target i686-unknown-freebsd
41+
cargo test --all --exclude tokio-tls --exclude tokio-macros --target i686-unknown-freebsd
4242
before_cache_script:
4343
- rm -rf $HOME/.cargo/registry/index

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
members = [
44
"tokio",
5-
"tokio-async-await",
65
"tokio-buf",
76
"tokio-codec",
87
"tokio-current-thread",
98
"tokio-executor",
109
"tokio-fs",
10+
"tokio-futures",
1111
"tokio-io",
12+
"tokio-macros",
1213
"tokio-reactor",
1314
"tokio-signal",
1415
"tokio-sync",

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,21 @@ have greater guarantees of stability.
126126

127127
The crates included as part of Tokio are:
128128

129-
* [`tokio-async-await`]: Experimental `async` / `await` support.
130-
131-
* [`tokio-codec`]: Utilities for encoding and decoding protocol frames.
132-
133129
* [`tokio-current-thread`]: Schedule the execution of futures on the current
134130
thread.
135131

136132
* [`tokio-executor`]: Task execution related traits and utilities.
137133

138134
* [`tokio-fs`]: Filesystem (and standard in / out) APIs.
139135

136+
* [`tokio-futures`]: Experimental `std::future::Future` and `async` / `await` support.
137+
138+
* [`tokio-codec`]: Utilities for encoding and decoding protocol frames.
139+
140140
* [`tokio-io`]: Asynchronous I/O related traits and utilities.
141141

142+
* [`tokio-macros`]: Macros for usage with Tokio.
143+
142144
* [`tokio-reactor`]: Event loop that drives I/O resources (like TCP and UDP
143145
sockets).
144146

@@ -154,12 +156,13 @@ The crates included as part of Tokio are:
154156
* [`tokio-uds`]: Unix Domain Socket bindings for use with `tokio-io` and
155157
`tokio-reactor`.
156158

157-
[`tokio-async-await`]: tokio-async-await
158159
[`tokio-codec`]: tokio-codec
159160
[`tokio-current-thread`]: tokio-current-thread
160161
[`tokio-executor`]: tokio-executor
161162
[`tokio-fs`]: tokio-fs
163+
[`tokio-futures`]: tokio-futures
162164
[`tokio-io`]: tokio-io
165+
[`tokio-macros`]: tokio-macros
163166
[`tokio-reactor`]: tokio-reactor
164167
[`tokio-tcp`]: tokio-tcp
165168
[`tokio-threadpool`]: tokio-threadpool

async-await/.cargo/config

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
target-dir = "../target"

async-await/Cargo.toml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
[package]
2+
name = "examples"
3+
edition = "2018"
4+
version = "0.1.0"
5+
authors = ["Carl Lerche <[email protected]>"]
6+
license = "MIT"
7+
8+
# Break out of the parent workspace
9+
[workspace]
10+
11+
[[bin]]
12+
name = "chat"
13+
path = "src/chat.rs"
14+
15+
[[bin]]
16+
name = "echo_client"
17+
path = "src/echo_client.rs"
18+
19+
[[bin]]
20+
name = "echo_server"
21+
path = "src/echo_server.rs"
22+
23+
[[bin]]
24+
name = "hyper"
25+
path = "src/hyper.rs"
26+
27+
[dependencies]
28+
tokio = { version = "0.1.18", features = ["async-await-preview"] }
29+
futures = "0.1.23"
30+
bytes = "0.4.9"
31+
hyper = "0.12.8"
32+
33+
# Avoid using crates.io for Tokio dependencies
34+
[patch.crates-io]
35+
tokio = { path = "../tokio" }
36+
tokio-codec = { path = "../tokio-codec" }
37+
tokio-current-thread = { path = "../tokio-current-thread" }
38+
tokio-executor = { path = "../tokio-executor" }
39+
tokio-fs = { path = "../tokio-fs" }
40+
# tokio-futures = { path = "../" }
41+
tokio-io = { path = "../tokio-io" }
42+
tokio-reactor = { path = "../tokio-reactor" }
43+
tokio-signal = { path = "../tokio-signal" }
44+
tokio-tcp = { path = "../tokio-tcp" }
45+
tokio-threadpool = { path = "../tokio-threadpool" }
46+
tokio-timer = { path = "../tokio-timer" }
47+
tokio-tls = { path = "../tokio-tls" }
48+
tokio-udp = { path = "../tokio-udp" }
49+
tokio-uds = { path = "../tokio-uds" }
File renamed without changes.

tokio-async-await/examples/src/chat.rs async-await/src/chat.rs

+20-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#![feature(await_macro, async_await, futures_api)]
2-
3-
#[macro_use]
4-
extern crate tokio;
5-
extern crate futures; // v0.1
1+
#![feature(await_macro, async_await)]
62

3+
use tokio::await;
74
use tokio::codec::{LinesCodec, Decoder};
85
use tokio::net::{TcpListener, TcpStream};
96
use tokio::prelude::*;
@@ -95,7 +92,8 @@ async fn process(stream: TcpStream, state: Arc<Mutex<Shared>>) -> io::Result<()>
9592
Ok(())
9693
}
9794

98-
fn main() {
95+
#[tokio::main]
96+
async fn main() {
9997
// Create the shared state. This is how all the peers communicate.
10098
//
10199
// The server task will hold a handle to this. For every new client, the
@@ -113,23 +111,21 @@ fn main() {
113111
println!("server running on localhost:6142");
114112

115113
// Start the Tokio runtime.
116-
tokio::run_async(async move {
117-
let mut incoming = listener.incoming();
118-
119-
while let Some(stream) = await!(incoming.next()) {
120-
let stream = match stream {
121-
Ok(stream) => stream,
122-
Err(_) => continue,
123-
};
124-
125-
let state = state.clone();
126-
127-
tokio::spawn_async(async move {
128-
if let Err(_) = await!(process(stream, state)) {
129-
eprintln!("failed to process connection");
130-
}
131-
});
132-
}
133-
});
114+
let mut incoming = listener.incoming();
115+
116+
while let Some(stream) = await!(incoming.next()) {
117+
let stream = match stream {
118+
Ok(stream) => stream,
119+
Err(_) => continue,
120+
};
121+
122+
let state = state.clone();
123+
124+
tokio::spawn_async(async move {
125+
if let Err(_) = await!(process(stream, state)) {
126+
eprintln!("failed to process connection");
127+
}
128+
});
129+
}
134130
}
135131

tokio-async-await/examples/src/echo_client.rs async-await/src/echo_client.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#![feature(await_macro, async_await, futures_api)]
2-
3-
#[macro_use]
4-
extern crate tokio;
1+
#![feature(await_macro, async_await)]
52

3+
use tokio::await;
64
use tokio::net::TcpStream;
75
use tokio::prelude::*;
86

@@ -36,18 +34,17 @@ async fn run_client(addr: &SocketAddr) -> io::Result<()> {
3634
Ok(())
3735
}
3836

39-
fn main() {
37+
#[tokio::main]
38+
async fn main() {
4039
use std::env;
4140

4241
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
4342
let addr = addr.parse::<SocketAddr>().unwrap();
4443

4544
// Connect to the echo serveer
4645

47-
tokio::run_async(async move {
48-
match await!(run_client(&addr)) {
49-
Ok(_) => println!("done."),
50-
Err(e) => eprintln!("echo client failed; error = {:?}", e),
51-
}
52-
});
46+
match await!(run_client(&addr)) {
47+
Ok(_) => println!("done."),
48+
Err(e) => eprintln!("echo client failed; error = {:?}", e),
49+
}
5350
}

tokio-async-await/examples/src/echo_server.rs async-await/src/echo_server.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#![feature(await_macro, async_await, futures_api)]
2-
3-
#[macro_use]
4-
extern crate tokio;
1+
#![feature(await_macro, async_await)]
52

3+
use tokio::await;
64
use tokio::net::{TcpListener, TcpStream};
75
use tokio::prelude::*;
86

@@ -24,7 +22,8 @@ fn handle(mut stream: TcpStream) {
2422
});
2523
}
2624

27-
fn main() {
25+
#[tokio::main]
26+
async fn main() {
2827
use std::env;
2928

3029
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string());
@@ -34,12 +33,10 @@ fn main() {
3433
let listener = TcpListener::bind(&addr).unwrap();
3534
println!("Listening on: {}", addr);
3635

37-
tokio::run_async(async {
38-
let mut incoming = listener.incoming();
36+
let mut incoming = listener.incoming();
3937

40-
while let Some(stream) = await!(incoming.next()) {
41-
let stream = stream.unwrap();
42-
handle(stream);
43-
}
44-
});
38+
while let Some(stream) = await!(incoming.next()) {
39+
let stream = stream.unwrap();
40+
handle(stream);
41+
}
4542
}

async-await/src/hyper.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![feature(await_macro, async_await)]
2+
3+
use tokio::await;
4+
use tokio::prelude::*;
5+
use hyper::Client;
6+
7+
use std::time::Duration;
8+
use std::str;
9+
10+
#[tokio::main]
11+
async fn main() {
12+
let client = Client::new();
13+
14+
let uri = "http://httpbin.org/ip".parse().unwrap();
15+
16+
let response = await!({
17+
client.get(uri)
18+
.timeout(Duration::from_secs(10))
19+
}).unwrap();
20+
21+
println!("Response: {}", response.status());
22+
23+
let mut body = response.into_body();
24+
25+
while let Some(chunk) = await!(body.next()) {
26+
let chunk = chunk.unwrap();
27+
println!("chunk = {}", str::from_utf8(&chunk[..]).unwrap());
28+
}
29+
}

azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
parameters:
7777
name: async_await
7878
displayName: Async / Await
79-
rust: nightly-2019-04-22
79+
rust: nightly-2019-04-25
8080
noDefaultFeatures: ''
8181
benches: true
8282
crates:

ci/patch.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# repository.
33
[patch.crates-io]
44
tokio = { path = "tokio" }
5-
tokio-async-await = { path = "tokio-async-await" }
65
tokio-buf = { path = "tokio-buf" }
76
tokio-codec = { path = "tokio-codec" }
87
tokio-current-thread = { path = "tokio-current-thread" }
98
tokio-executor = { path = "tokio-executor" }
109
tokio-fs = { path = "tokio-fs" }
10+
tokio-futures = { path = "tokio-futures" }
1111
tokio-io = { path = "tokio-io" }
1212
tokio-reactor = { path = "tokio-reactor" }
1313
tokio-signal = { path = "tokio-signal" }

tokio-async-await/examples/.cargo/config

-2
This file was deleted.

tokio-async-await/examples/Cargo.toml

-49
This file was deleted.

0 commit comments

Comments
 (0)