gpt4 book ai didi

rust - tokio::run_async 与 tokio::net::UnixStream panic

转载 作者:行者123 更新时间:2023-11-29 08:09:14 25 4
gpt4 key购买 nike

tokio::run_async + futures 0.3 + tokio::net::UnixStream panic 。

设置

[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

编译器强制我使用 futures 0.3。

  1. await!() 需要 futures 0.3(不使用 futures 0.1 编译)
  2. async {} block 需要 futures 0.3(因为 await!(async {}) 有效)
  3. tokio::run_async 需要 futures 0.3(不使用 futures 0.1 编译)

futures 0.3 似乎有一些与 futures 0.1 不兼容的地方。有解决方法吗?

最佳答案

工作解决方案

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)中使用 futures 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

关于rust - tokio::run_async 与 tokio::net::UnixStream panic ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55447650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com