简单添加console_error_panic_hook::set_once()
会导致错误:
[WARN]: :-) origin crate has no README
[INFO]: Installing wasm-bindgen...
error: cannot shadow already defined class `Error`
Error: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit code: 1
是的,我确实有自己的Error
结构,但为什么使用这个函数会导致";影子;错误
只有当我使用[wasm_bindgen]
导出Error
结构时才会发生错误。
运行wasm-pack build --target web
后出现错误
复制
# cargo.toml
[package]
name = "testing"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
console_error_panic_hook = "0.1.7"
wasm-bindgen = { version = "0.2.76" }
// lib.rs
use wasm_bindgen::prelude::wasm_bindgen;
// Adding this does not help:
// extern crate console_error_panic_hook;
#[wasm_bindgen] // Works if either this line is remove...
struct Error {}
#[wasm_bindgen]
pub fn main() {
console_error_panic_hook::set_once(); // ... or this one
println!("Hello, world!");
}
版本
wasm-pack 0.10.2
cargo 1.58.0 (f01b232bc 2022-01-19)
rustc 1.58.1 (db9d1b20b 2022-01-20)
此代码:
#[wasm_bindgen]
struct Error {}
意味着您希望自己的struct Error
被视为绑定到JavaScriptError
类型的WASM。由于console_error_panic_hook
也需要绑定到JavaScriptError
,这会产生冲突——由于有多个绑定,当JavaScript将其Error
传递给WASM函数时,不清楚应该在Rust中创建什么结构。