Using async/await with old `Future<Item = X, Error = Y>` types [duplicate]

瘦欲@ 提交于 2020-02-24 05:00:06

问题


I have a function in a crate that returns old style futures.

Imagine something like:

pub fn old_function() -> impl Future<Item = X, Error = Y>
...

I want to use this crate in a new codebase where I don't want to mix things too much.

How can I keep the new implementation clean and use async/await when calling this old_function ?


回答1:


The answer was very easy. I write here in case somebody has a similar problem. It is actually part of the async/await primer :)

You need to enable the compat feature

[dependencies]
futures = { version = "0.3.1", features = ["compat"] }

And later it is possible to do:

let x = old_function().compat().await;


来源:https://stackoverflow.com/questions/59976639/using-async-await-with-old-futureitem-x-error-y-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!