对旧的"Future< Item = X,Error = Y>"类型使用异步/等待 [英] Using async/await with old `Future<Item = X, Error = Y>` types
本文介绍了对旧的"Future< Item = X,Error = Y>"类型使用异步/等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的箱子里有个函数,可以返回旧样式的期货.
I have a function in a crate that returns old style futures.
想象一下:
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.
如何在调用此old_function
时保持新实现的清洁并使用async/await
?
How can I keep the new implementation clean and use async/await
when calling this old_function
?
推荐答案
答案很简单.如果有人遇到类似问题,我在这里写.它实际上是async/await入门的一部分:)
The answer was very easy. I write here in case somebody has a similar problem. It is actually part of the async/await primer :)
您需要启用兼容功能
[dependencies]
futures = { version = "0.3.1", features = ["compat"] }
随后可以执行以下操作:
And later it is possible to do:
let x = old_function().compat().await;
这篇关于对旧的"Future< Item = X,Error = Y>"类型使用异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文