Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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(())
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

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" }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...