tokio :: run_async与tokio :: net :: UnixStream恐慌

时间:2019-04-01 03:11:36

标签: rust rust-tokio

tokio::run_async + futures 0.3 + tokio::net::UnixStream恐慌。

设置

[package]
name = "prac"
version = "0.1.0"
edition = "2018"

[dependencies]
futures-preview = {version="0.3.0-alpha.13", features=["compat", "io-compat"]}
tokio = {version="0.1.18", features=["async-await-preview"]}

代码

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

use tokio::net::UnixStream;
use tokio::net::UnixListener;
use futures::compat::Future01CompatExt;
use futures::compat::Stream01CompatExt;
use futures::StreamExt;

fn main() {
    let _ = std::fs::remove_file("/tmp/test.sock");

    tokio::run_async(async {
        let fut = UnixListener::bind(&"/tmp/test.sock").unwrap().incoming().compat();
        let fut = fut.for_each(|stream| {
            let _ = stream;
            println!("new conn");
            futures::future::ready(())
        });
        tokio::spawn_async(fut);

        let fut = UnixStream::connect(&"/tmp/test.sock").compat();
        let _conn = await!(fut);
    });
}

运行

     Running `target/debug/main bin main`
thread 'tokio-runtime-worker-0' panicked at 'not yet implemented: async-await-preview currently only supports futures 0.1. Use the compatibility layer of futures 0.3 instead, if you want to use futures 0.3.', /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-async-await-0.1.6/src/compat/backward.rs:76:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
thread 'tokio-runtime-worker-1' panicked at 'not yet implemented: async-await-preview currently only supports futures 0.1. Use the compatibility layer of futures 0.3 instead, if you want to use futures 0.3.', /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-async-await-0.1.6/src/compat/backward.rs:76:5

编译器强迫我使用期货0.3。

  1. await!()要求期货0.3(不与期货0.1一起编译)
  2. async {}块要求期货0.3(因为await!(async {})有效)
  3. tokio::run_async要求期货0.3(不与期货0.1一起编译)

看来,期货0.3与期货0.1不兼容。 有解决方法吗?

2 个答案:

答案 0 :(得分:0)

工作解决方案

reddit中的代码段有效。

use futures::{Future,FutureExt};
use futures::compat::Compat;

pub fn tokio_run<F: Future<Output=()> + Send + 'static>(future: F) {
    tokio::run(Compat::new(Box::pin(
        future.map(|()| -> Result<(), ()> { Ok(()) })
    )));
}

pub fn tokio_spawn<F: Future<Output=()> + Send + 'static>(future: F) {
    tokio::spawn(Compat::new(Box::pin(
        future.map(|()| -> Result<(), ()> { Ok(()) })
    )));
}

await!(而不是tokio::await)中使用Future 0.3,并使用这些功能代替tokio::run_async, tokio::spawn_async

非工作解决方案

错误消息建议使用Futures 0.1,但是将Futures 0.3转换为0.1并使用tokio::await!无效:

use tokio::await;
...
await!(future03.into_awaitable());

运行

thread 'tokio-runtime-worker-0' panicked at 'not yet implemented: async-await-
preview currently only supports futures 0.1. Use the compatibility layer of futures 0.3 instead, if you want to use futures 0.3.', /Users/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-async-await-0.1.6/src/compat/backward.rs:76:5

答案 1 :(得分:0)

我遇到了类似的问题。我正在使用以下实用程序来使0.3期货在Tokio运行时上可运行:

fn compat<F: StdFuture + Send + 'static>(f: F) -> impl Future<Item=F::Output, Error=()> {
    f.unit_error().boxed().compat()
}

我想它也可以变成特质...