如何在 Tokio 中使用 async/await 语法?
How do I use async/await syntax with Tokio?
我正在尝试将 async/await 与 Rust 中的进程一起使用。我正在使用 tokio
和 tokio-process
:
#![feature(await_macro, async_await, futures_api)]
extern crate tokio;
extern crate tokio_process;
use std::process::Command;
use tokio_process::CommandExt;
fn main() {
tokio::run_async(main_async());
}
async fn main_async() {
let out = Command::new("echo")
.arg("hello")
.arg("world")
.output_async();
let s = await!(out);
}
这是我得到的错误:
error[E0277]: the trait bound `tokio_process::OutputAsync: std::future::Future` is not satisfied
--> src/main.rs:21:13
|
21 | let s = await!(out);
| ^^^^^^^^^^^ the trait `std::future::Future` is not implemented for `tokio_process::OutputAsync`
|
= note: required by `std::future::poll_with_tls_waker`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0277]: the trait bound `tokio_process::OutputAsync: std::future::Future` is not satisfied
--> src/main.rs:21:13
|
21 | let s = await!(out);
| ^^^^^^^^^^^ the trait `std::future::Future` is not implemented for `tokio_process::OutputAsync`
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
我该如何做对?
TL;DR:使用 Tokio 0.2 或更新版本,它应该可以正常工作。
Tokio 0.1 和相关的 crate 是使用 futures 0.1 crate 实现的。这个 crate 中的 Future
trait 在概念上类似于标准库中的 Future
trait 版本,但在细节上有很大不同。 async
/ await
语法是围绕标准库中特征的版本构建的。
Tokio 0.2 和相关的板条箱是使用标准库 Future
实现的,并且已经过重新设计以更好地支持 async
/ await
语法。
东京 0.2
[dependencies]
tokio = { version = "0.2", features = ["full"] }
use tokio::process::Command;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let out = Command::new("echo")
.arg("hello")
.arg("world")
.output()
.await?;
let s = String::from_utf8_lossy(&out.stdout);
println!("{}", s);
Ok(())
}
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/tokio2`
hello world
测试:
- Rustc 1.39.0
东京 0.1
你必须. In addition, you need to start up the Tokio runtime services, ideally for both Tokio 0.1 and Tokio 0.3. That's where tokio-compat进来:
[dependencies]
futures = { version = "0.3", features = ["compat"] }
tokio-compat = "0.1"
tokio-process = "0.2"
use futures::compat::Future01CompatExt;
use std::process::Command;
use tokio_process::CommandExt;
fn main() {
tokio_compat::run_std(main_async());
}
async fn main_async() {
let out = Command::new("echo")
.arg("hello")
.arg("world")
.output_async();
// Convert future from 0.1 to 0.3
let s = out.compat().await;
println!("{:?}", s);
}
% cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/tt`
Ok(Output { status: ExitStatus(ExitStatus(0)), stdout: "hello world\n", stderr: "" })
使用 Rust 1.43.0 测试
另请参阅:
我正在尝试将 async/await 与 Rust 中的进程一起使用。我正在使用 tokio
和 tokio-process
:
#![feature(await_macro, async_await, futures_api)]
extern crate tokio;
extern crate tokio_process;
use std::process::Command;
use tokio_process::CommandExt;
fn main() {
tokio::run_async(main_async());
}
async fn main_async() {
let out = Command::new("echo")
.arg("hello")
.arg("world")
.output_async();
let s = await!(out);
}
这是我得到的错误:
error[E0277]: the trait bound `tokio_process::OutputAsync: std::future::Future` is not satisfied
--> src/main.rs:21:13
|
21 | let s = await!(out);
| ^^^^^^^^^^^ the trait `std::future::Future` is not implemented for `tokio_process::OutputAsync`
|
= note: required by `std::future::poll_with_tls_waker`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error[E0277]: the trait bound `tokio_process::OutputAsync: std::future::Future` is not satisfied
--> src/main.rs:21:13
|
21 | let s = await!(out);
| ^^^^^^^^^^^ the trait `std::future::Future` is not implemented for `tokio_process::OutputAsync`
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
我该如何做对?
TL;DR:使用 Tokio 0.2 或更新版本,它应该可以正常工作。
Tokio 0.1 和相关的 crate 是使用 futures 0.1 crate 实现的。这个 crate 中的 Future
trait 在概念上类似于标准库中的 Future
trait 版本,但在细节上有很大不同。 async
/ await
语法是围绕标准库中特征的版本构建的。
Tokio 0.2 和相关的板条箱是使用标准库 Future
实现的,并且已经过重新设计以更好地支持 async
/ await
语法。
东京 0.2
[dependencies]
tokio = { version = "0.2", features = ["full"] }
use tokio::process::Command;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let out = Command::new("echo")
.arg("hello")
.arg("world")
.output()
.await?;
let s = String::from_utf8_lossy(&out.stdout);
println!("{}", s);
Ok(())
}
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `target/debug/tokio2`
hello world
测试:
- Rustc 1.39.0
东京 0.1
你必须
[dependencies]
futures = { version = "0.3", features = ["compat"] }
tokio-compat = "0.1"
tokio-process = "0.2"
use futures::compat::Future01CompatExt;
use std::process::Command;
use tokio_process::CommandExt;
fn main() {
tokio_compat::run_std(main_async());
}
async fn main_async() {
let out = Command::new("echo")
.arg("hello")
.arg("world")
.output_async();
// Convert future from 0.1 to 0.3
let s = out.compat().await;
println!("{:?}", s);
}
% cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/tt`
Ok(Output { status: ExitStatus(ExitStatus(0)), stdout: "hello world\n", stderr: "" })
使用 Rust 1.43.0 测试
另请参阅: