1

简单地添加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结构,但为什么使用这个函数会导致“影子”错误?

仅当我Error使用[wasm_bindgen].

运行后出现错误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)
4

1 回答 1

0

这段代码:

#[wasm_bindgen]
struct Error {}

...意味着您希望自己struct Error的被视为 WASM 绑定到JavaScriptError类型。由于也console_error_panic_hook需要绑定到 JavaScript ,这会产生冲突 - 有多个绑定,当 JavaScript 将其传递给 WASM 函数Error时,不清楚应该在 Rust 中创建什么结构。Error

于 2022-02-10T03:32:18.177 回答