Is there a way that we can convert from futures 0.1 to the standard library futures?
Asked Answered
M

1

3

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?

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

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(())
}
Meshwork answered 29/10, 2019 at 16:13 Comment(0)
R
4

Use the Future01CompatExt trait:

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" }
Rounders answered 29/10, 2019 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.