有什么方法可以将Futures 0.1转换为标准库Futures?

时间:2019-10-29 16:13:06

标签: rust future

async / await功能即将推出,但仍有许多库仍在使用Futures 0.1。我们如何在两者之间转换?

Convert an async future to 0.1 future涵盖了将异步Future转换为0.1 Future。

How do I erase the type of future in the new future API?讨论了一个{0.1}函数,该函数调用0.1 Future并获取结果,但是我可以导入的async宏在哪里?看来它不再编译。

await!()

1 个答案:

答案 0 :(得分:1)

使用Future01CompatExt特性:

use futures01::future as future01;
use futures03::compat::Future01CompatExt;

fn make_future_01() -> impl future01::Future<Item = i32, Error = ()> {
    future01::ok(2)
}

async fn example_03_uses_01() -> Result<i32, ()> {
    let v = make_future_01().compat().await?;
    Ok(v)
}
[dependencies]
futures03 = { package = "futures-preview", version = "0.3.0-alpha.19", features = ["compat"] }
futures01 = { package = "futures", version = "0.1.29" }