如何在 Rust 中的不同 Future 特征之间创建互操作性?
How can I create interoperability between different Future traits in Rust?
我正在尝试使用 binance_async
库、tokio
和 futures
向 Binance 发出并发订单。 (请参阅此问题末尾的注释。)
我使用的 binance_async
函数 return a
binance_async::error::Result<impl futures::future::Future<_, _>>
类型。我面临以下问题,如以下两个示例所示:
- 假设我正在尝试这样做:
let bn = Binance::with_credential(&api_key, &secret_key);
let fut = bn.limit_sell(&symbol, qty, price);
tokio::spawn(fut.unwrap()); // ERROR
/* Error message:
error[E0277]: `impl futures::future::Future` is not a future
--> src/main.rs:23:5
|
23 | tokio::spawn(fut.unwrap());
| ^^^^^^^^^^^^ `impl futures::future::Future` is not a future
|
= help: the trait `futures::Future` is not implemented for `impl
futures::future::Future`
*/
这太奇怪了。首先,我在任何地方都找不到 futures::Future
——只有 futures::future::Future
,它实现了 fut.unwrap()
。有帮助吗?
- 好吧,忘记 Tokio,让我们试试这个:
let mut orders : Vec<Result<_>> = Vec::new(); // not std::Result
let fut = bn.limit_sell(&symbol, qty, price);
orders.push(fut);
let mut futures = Vec::new(); // I want to unwrap them, to use join_all()
for f in orders.iter() {
match *f {
Ok(x) => {
futures.push(x);
},
Err(e) => {}
}
}
futures::future::join_all(futures).await; // <--- ERROR!
/* Error message (one of 3 similar ones):
error[E0277]: `impl futures::future::Future` is not a future
--> src/main.rs:37:5
|
37 | join_all(futures).await; // <--- ERROR!
| ^^^^^^^^^^^^^^^^^^^^^^^ `impl futures::future::Future` is not a future
|
= help: the trait `futures::Future` is not implemented for `impl
futures::future::Future`
= note: required because of the requirements on the impl of `futures::Future` for
`JoinAll<impl futures::future::Future>`
= note: required by `futures::Future::poll`
*/
同样的错误,同样的问题。
备注:
- 我可能进口过多了。如果导入所有这些库看起来有点矫枉过正甚至是问题的根源,请告诉我!
- 我知道
binance_async
是一个维护不足的库。我很可能会完全放弃这种方法,转而使用 binance
crate。我真的很好奇这个错误。
- 我的主管说这可能与属性宏有关,但我们都只有几个月的 Rust 经验并试图快速完成这项工作,我们无法深入挖掘。
非常感谢:)
binance_async
使用 futures 0.1,它与 tokio
使用的现在标准化的 std::future::Future
不兼容。您可以通过启用 compat
功能将期货 0.1 期货转换为标准期货:
futures = { version = "0.3", features = ["compat"] }
并调用 .compat()
方法:
use futures::compat::Future01CompatExt;
tokio::spawn(fut.unwrap().compat());
use futures::compat::Future01CompatExt;
orders.push(fut.compat());
futures::future::join_all(futures).await;
我正在尝试使用 binance_async
库、tokio
和 futures
向 Binance 发出并发订单。 (请参阅此问题末尾的注释。)
我使用的 binance_async
函数 return a
binance_async::error::Result<impl futures::future::Future<_, _>>
类型。我面临以下问题,如以下两个示例所示:
- 假设我正在尝试这样做:
let bn = Binance::with_credential(&api_key, &secret_key);
let fut = bn.limit_sell(&symbol, qty, price);
tokio::spawn(fut.unwrap()); // ERROR
/* Error message:
error[E0277]: `impl futures::future::Future` is not a future
--> src/main.rs:23:5
|
23 | tokio::spawn(fut.unwrap());
| ^^^^^^^^^^^^ `impl futures::future::Future` is not a future
|
= help: the trait `futures::Future` is not implemented for `impl
futures::future::Future`
*/
这太奇怪了。首先,我在任何地方都找不到 futures::Future
——只有 futures::future::Future
,它实现了 fut.unwrap()
。有帮助吗?
- 好吧,忘记 Tokio,让我们试试这个:
let mut orders : Vec<Result<_>> = Vec::new(); // not std::Result
let fut = bn.limit_sell(&symbol, qty, price);
orders.push(fut);
let mut futures = Vec::new(); // I want to unwrap them, to use join_all()
for f in orders.iter() {
match *f {
Ok(x) => {
futures.push(x);
},
Err(e) => {}
}
}
futures::future::join_all(futures).await; // <--- ERROR!
/* Error message (one of 3 similar ones):
error[E0277]: `impl futures::future::Future` is not a future
--> src/main.rs:37:5
|
37 | join_all(futures).await; // <--- ERROR!
| ^^^^^^^^^^^^^^^^^^^^^^^ `impl futures::future::Future` is not a future
|
= help: the trait `futures::Future` is not implemented for `impl
futures::future::Future`
= note: required because of the requirements on the impl of `futures::Future` for
`JoinAll<impl futures::future::Future>`
= note: required by `futures::Future::poll`
*/
同样的错误,同样的问题。
备注:
- 我可能进口过多了。如果导入所有这些库看起来有点矫枉过正甚至是问题的根源,请告诉我!
- 我知道
binance_async
是一个维护不足的库。我很可能会完全放弃这种方法,转而使用binance
crate。我真的很好奇这个错误。 - 我的主管说这可能与属性宏有关,但我们都只有几个月的 Rust 经验并试图快速完成这项工作,我们无法深入挖掘。
非常感谢:)
binance_async
使用 futures 0.1,它与 tokio
使用的现在标准化的 std::future::Future
不兼容。您可以通过启用 compat
功能将期货 0.1 期货转换为标准期货:
futures = { version = "0.3", features = ["compat"] }
并调用 .compat()
方法:
use futures::compat::Future01CompatExt;
tokio::spawn(fut.unwrap().compat());
use futures::compat::Future01CompatExt;
orders.push(fut.compat());
futures::future::join_all(futures).await;