Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

rust - Is there a way that we can convert from futures 0.1 to the standard library futures?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...