我想用Cargo-wasi编译以下代码。
// reqwest = { version = "0.11", features = ["json"] }
// tokio = { version = "1", features = ["full"] }
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let resp = reqwest::get("https://httpbin.org/ip")
.await?
.json::<HashMap<String, String>>()
.await?;
println!("{:#?}", resp);
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
尝试编译后,出现以下错误,因为mio 目前不支持 WASI
$ cargo wasi run
Compiling mio v0.7.9
Compiling parking_lot v0.11.1
Compiling serde_json v1.0.64
Compiling idna v0.2.2
error[E0432]: unresolved import `crate::sys::IoSourceState`
--> /home/ducaale/.cargo/registry/src/github.com-1ecc6299db9ec823/mio-0.7.9/src/io_source.rs:12:5
|
12 | use crate::sys::IoSourceState;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ no `IoSourceState` in `sys`
... errors omitted
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,到目前为止我发现的示例都没有使用 async/await。有什么我可以替换的东西tokio,以便我的代码可以在 WASI 中编译吗?
我尝试运行这个,似乎 reqwests 板条箱无法使用 Cargo wasi 或 wasm-pack 正确构建,因为它无法编译 mio (在本机编译时由 tokio 使用)。github 上有一些提到 reqwests 可以与 wasm 一起使用,但它尚未完全支持,而且我找不到太多关于如何使其工作的信息。听起来目前WASI上的HTTP请求还没有太多解决方案,但是可以使用web-sys通过Node.js或浏览器发出请求。
tokio 似乎需要特定的功能标志才能与 Web 程序集一起使用。此问题在底部提到了同步和 rt 标志: https: //github.com/tokio-rs/tokio/issues/1597但为了也使用 #[tokio:main],您将需要“rt-multi” -thread”和“macros”功能标志也是如此。
也可以使用 wasm bindgen 将未来转换为承诺,但这可能不适用于 WASI: https: //rustwasm.github.io/wasm-bindgen/api/wasm_bindgen_futures/