如何在Tokio中使用异步/等待语法?

问题描述 投票:2回答:1

我正在尝试对Rust中的进程使用异步/等待。我正在使用tokiotokio-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)

我该如何正确处理?

async-await rust rust-tokio
1个回答
5
投票

Tokio 0.1和相关的板条箱]使用stable期货0.1条板箱实现。此板条箱的Future特征在概念上与标准库中Future特征的版本相似,但细节上有很大不同。async/ await语法围绕标准库中的特征版本构建。

尚未发布的Tokio 0.2和相关的板条箱是使用标准库Future实现的,正在重新制作以更好地支持async / await语法。

Tokio 0.2

[dependencies]
tokio = "0.2.0-alpha.2"
tokio-process = "0.3.0-alpha.2"
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/tt`
hello world

经过测试:

  • Rustc 1.39.0-nightly(6ef275e6c 2019-09-24)

Tokio 0.1

对“ tokio async await”执行互联网搜索会导致使用恰当命名的板条箱tokio-async-await。本文记录了如何使Tokio能够参与不稳定的期货交易:

[dependencies]
tokio = { version = "0.1.15", features = ["async-await-preview"] }
tokio-process = "0.2.3"

在您的代码中,您必须在针对期货0.1板条箱和标准库中的特征而实现的Future之间进行转换。一种简单的方法是使用await宏的Tokio版本:

#![feature(await_macro, async_await, futures_api)]

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 = tokio::await!(out);
    println!("{:?}", s);
}
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/oo`
Ok(Output { status: ExitStatus(ExitStatus(0)), stdout: "hello world\n", stderr: "" })

经过测试:

  • Rust 1.34.0-nightly(e1c6d0057 2019-02-22)
  • Rust 1.34.0-nightly(aadbc459b 2019-02-23)

另请参见:

© www.soinside.com 2019 - 2024. All rights reserved.