【问题标题】:Is there a way that we can convert from futures 0.1 to the standard library futures?有没有一种方法可以将期货 0.1 转换为标准库期货?
【发布时间】:2019-10-29 16:13:06
【问题描述】:

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

Convert an async future to 0.1 future 涵盖将异步未来转换为 0.1 未来。

How do I erase the type of future in the new future API? 谈到了一个 async 函数,它调用 0.1 未来并获得结果,但我可以导入的 await!() 宏在哪里?它似乎不再编译。

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

【问题讨论】:

    标签: rust future


    【解决方案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", version = "0.3", features = ["compat"] }
    futures01 = { package = "futures", version = "0.1" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 2011-07-03
      • 2021-11-13
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      相关资源
      最近更新 更多