问题描述
我正在尝试对Rust中的进程使用异步/等待.我正在使用tokio
和tokio-process
:
I'm trying to use async/await with processes in Rust. I'm using tokio
and 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或更高版本,它应该可以正常工作.
TL;DR: Use Tokio 0.2 or newer and it should just work.
Tokio 0.1和相关的板条箱是使用期货0.1板条箱实现的.此板条箱中的Future
特征在概念上与标准库中Future
特征的版本相似,但细节上却大不相同. async
/await
语法是围绕标准库中特征的版本构建的.
Tokio 0.1 and related crates are implemented using the futures 0.1 crate. The Future
trait from this crate is conceptually similar to the version of the Future
trait from the standard library but substantially different in details. async
/ await
syntax is built around the version of the trait in the standard library.
Tokio 0.2和相关的板条箱是使用标准库Future
实现的,并且已经过重新设计以更好地支持async
/await
语法.
Tokio 0.2 and related crates are implemented using the standard library Future
and have been reworked to better support async
/ await
syntax.
[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板条箱和标准库的特征实施的Future
之间进行转换 .另外,您需要启动Tokio运行时服务,最好同时运行Tokio 0.1和Tokio 0.3.这就是 tokio-compat 出现的地方:
You have to translate between a Future
implemented against the trait from the futures 0.1 crate and from the standard library. 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 comes in:
[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的测试
Tested with Rust 1.43.0
另请参阅:
- How do I convert an async / standard library future to futures 0.1?
- Is there a way that we can convert from futures 0.1 to the standard library futures?
- Why is a trait not implemented for a type that clearly has it implemented?
这篇关于如何在Tokio中使用async/await语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!