tokio::run_async + futures 0.3 + tokio::net::UnixStream panics.

Setup

[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"]}

Code

#![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);
    });
}

Run

     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

The compiler forces me to use futures 0.3.

  1. await!() requires futures 0.3 (does not compile with futures 0.1)
  2. async {} block requires futures 0.3 (because await!(async {}) works)
  3. tokio::run_async requires futures 0.3 (does not compile with futures 0.1)

It seems that futures 0.3 has something incompatible with futures 0.1. Is there a workaround?

2 Answers

0
mq7 On

Working Solution

The code snippet in reddit worked.

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(()) })
    )));
}

Use futures 0.3 in await! (not tokio::await), and use these functions instead of tokio::run_async, tokio::spawn_async.

Non-working Solution

The error message suggests to use futures 0.1, but converting futures 0.3 into 0.1 and using tokio::await! didn't work:

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

Run

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
0
kralyk On

I ran into a similar problem. I am using the following utility to make 0.3 futures runnable on Tokio runtime:

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

I suppose it could be turned into a trait too...