FromWasmAbi 没有为 fn(SomeStruct) 实现,而 SomeStruct 是 #[wasm_bindgen]
FromWasmAbi is not implemented for fn(SomeStruct) , while SomeStruct is #[wasm_bindgen]
我定义了 3 个结构和两种类型
type OnMoveEvent = fn(Board) -> ();
type OnGameOverEvent = fn(Player) -> ();
#[wasm_bindgen]
pub struct Game {
on_move: OnMoveEvent,
on_game_over: OnGameOverEvent,
board: Board,
}
#[wasm_bindgen]
pub struct Board {/* ... */}
#[wasm_bindgen]
pub struct Player {/* ... */}
所有 3 个结构都是 wasm_bindgen
,类型不能标记为 wasm_bindgen
。然而,类型只是接受结构并返回 void
的函数
当我将 wasm_bindgen
添加到 Game
impl
时,出现以下错误
--> src/game.rs:16:1
|
16 | #[wasm_bindgen]
| ^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::traits::FromWasmAbi` is not implemented for `fn(board::Board)`
那是因为new
有如下签名
pub fn new(on_move: OnMoveEvent, on_game_over: OnGameOverEvent) -> Game
在我看来,转换类型应该很简单,因为它们是接受 wasm_bindgen
结构的函数,但事实并非如此
这是一个错误还是我遗漏了什么?
完整代码is here.
经过一番研究后回答我自己的问题:
因为我试图将一些函数从 JS 传递给 Rust,所以不能保证这些函数具有特定的签名。
相反 js-sys
crate 提供了一种方法 Receiving JavaScript Closures in Exported Rust Functions
我将代码更改为以下内容:
#[wasm_bindgen]
pub struct Game {
board: Board,
on_cpu_move: js_sys::Function,
on_game_over: js_sys::Function,
}
和 new
签名如下所示:
pub fn new(on_move: js_sys::Function, on_game_over: js_sys::Function) -> Game {
然后我需要调用 JS
消费者将要提供的回调,这样做是这样的:
...
let state = self.board.state();
let this = JsValue::NULL;
if state.game_over {
let _ = self.on_game_over.call0(&this);
} else {
let board = JsValue::from(self.board);
let _ = self.on_cpu_move.call1(&this, &board);
}
...
JS
消费者有责任确保他们提供正确的类型。
希望有人觉得它有用。
我定义了 3 个结构和两种类型
type OnMoveEvent = fn(Board) -> ();
type OnGameOverEvent = fn(Player) -> ();
#[wasm_bindgen]
pub struct Game {
on_move: OnMoveEvent,
on_game_over: OnGameOverEvent,
board: Board,
}
#[wasm_bindgen]
pub struct Board {/* ... */}
#[wasm_bindgen]
pub struct Player {/* ... */}
所有 3 个结构都是 wasm_bindgen
,类型不能标记为 wasm_bindgen
。然而,类型只是接受结构并返回 void
当我将 wasm_bindgen
添加到 Game
impl
时,出现以下错误
--> src/game.rs:16:1
|
16 | #[wasm_bindgen]
| ^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::traits::FromWasmAbi` is not implemented for `fn(board::Board)`
那是因为new
有如下签名
pub fn new(on_move: OnMoveEvent, on_game_over: OnGameOverEvent) -> Game
在我看来,转换类型应该很简单,因为它们是接受 wasm_bindgen
结构的函数,但事实并非如此
这是一个错误还是我遗漏了什么?
完整代码is here.
经过一番研究后回答我自己的问题:
因为我试图将一些函数从 JS 传递给 Rust,所以不能保证这些函数具有特定的签名。
相反 js-sys
crate 提供了一种方法 Receiving JavaScript Closures in Exported Rust Functions
我将代码更改为以下内容:
#[wasm_bindgen]
pub struct Game {
board: Board,
on_cpu_move: js_sys::Function,
on_game_over: js_sys::Function,
}
和 new
签名如下所示:
pub fn new(on_move: js_sys::Function, on_game_over: js_sys::Function) -> Game {
然后我需要调用 JS
消费者将要提供的回调,这样做是这样的:
...
let state = self.board.state();
let this = JsValue::NULL;
if state.game_over {
let _ = self.on_game_over.call0(&this);
} else {
let board = JsValue::from(self.board);
let _ = self.on_cpu_move.call1(&this, &board);
}
...
JS
消费者有责任确保他们提供正确的类型。
希望有人觉得它有用。