有没有办法可以将 futures 0.1 转换为标准库 futures?
Is there a way that we can convert from futures 0.1 to the standard library futures?
async
/await
功能即将推出,但仍有许多库仍在使用 futures 0.1。我们如何在两者之间进行转换?
涵盖将异步未来转换为 0.1 未来。
谈到一个 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(())
}
使用 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" }
async
/await
功能即将推出,但仍有许多库仍在使用 futures 0.1。我们如何在两者之间进行转换?
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(())
}
使用 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" }