有什么方法可以将Futures 0.1转换为标准库Futures? [英] Is there a way that we can convert from futures 0.1 to the standard library futures?

查看:54
本文介绍了有什么方法可以将Futures 0.1转换为标准库Futures?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The async/await feature is coming soon, but there are a lot of libraries still using futures 0.1. How do we convert between the two?

将异步未来转换为0.1未来涵盖了转换异步未来到0.1未来.

Convert an async future to 0.1 future covers converting an async future to 0.1 future.

我如何删除新的Future API中的Future类型?讨论了调用<0.1 Future并获取结果的async函数,但是我可以导入的await!()宏在哪里?看来它不再编译.

How do I erase the type of future in the new future API? talks about an async function that calls an 0.1 future and gets the result, but where is the await!() macro I can import? It seems it no longer compiles.

struct A_future01;

impl A_future01 {
    pub fn exec1() -> Box<dyn Future<Item=String, Error=()>> {
        Box::new(futures::future::result("ok"))
    }

    pub fn exec2() -> Box<dyn Future<Item=String, Error=()>> {
        Box::new(call().unit_error().boxed().compat()) //Like this## Heading ##?
    }
}

async fn call() -> Result<(), Box<dyn std::error::Error>> {
    let result_from_a = A_future01::exec().await(); //how can I achieve this ?
    Ok(())
}

推荐答案

使用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", version = "0.3", features = ["compat"] }
futures01 = { package = "futures", version = "0.1" }

这篇关于有什么方法可以将Futures 0.1转换为标准库Futures?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆