簡體   English   中英

如何在 Rust 中的不同 Future 特征之間創建互操作性?

[英]How can I create interoperability between different Future traits in Rust?

我正在嘗試使用binance_async庫、 tokiofutures向 Binance 發出並發訂單。 (請參閱本問題末尾的注釋。)
我使用的binance_async函數返回binance_async::error::Result<impl futures::future::Future<_, _>>類型。 我面臨以下問題,在這兩個示例中進行了說明:

  1. 假設我正在嘗試這樣做:
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 ——只有fut.unwrap()實現的futures::future::Future 有什么幫助嗎?

  1. 好吧,忘記 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`
*/

同樣的錯誤,同樣的問題。

筆記:

  1. 我可能是過度進口。 如果導入所有這些庫看起來有點矯枉過正,甚至是問題的根源,請告訴我!
  2. 我知道binance_async是一個維護不足的庫。 我很可能會完全放棄這種方法,而用 go 代替binance crate。 我只是對這個錯誤很好奇。
  3. 我的主管說這可能與屬性宏有關,但我們倆都只有幾個月的 Rust 經驗,並試圖快速完成這項工作,我們無法深入挖掘。

非常感謝:)

binance_async使用 futures 0.1,它與tokio使用的現在標准化的std::future::Future不兼容。 您可以通過啟用compat功能將 futures 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM