我想编译以下代码。
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct Dummy {}
#[wasm_bindgen]
pub fn test() -> Vec<Dummy> {
vec![]
}但是,编译器不允许我这样做。
error[E0277]: the trait bound `std::boxed::Box<[Dummy]>: wasm_bindgen::convert::traits::IntoWasmAbi` is not satisfied
--> xxxx
|
XX | #[wasm_bindgen]
| ^^^^^^^^^^^^^^^ the trait `wasm_bindgen::convert::traits::IntoWasmAbi` is not implemented for `std::boxed::Box<[Dummy]>`
|
= help: the following implementations were found:
<std::boxed::Box<[f32]> as wasm_bindgen::convert::traits::IntoWasmAbi>
<std::boxed::Box<[f64]> as wasm_bindgen::convert::traits::IntoWasmAbi>
<std::boxed::Box<[i16]> as wasm_bindgen::convert::traits::IntoWasmAbi>
<std::boxed::Box<[i32]> as wasm_bindgen::convert::traits::IntoWasmAbi>
and 9 others
= note: required because of the requirements on the impl of `wasm_bindgen::convert::traits::IntoWasmAbi` for `std::vec::Vec<Dummy>`我使用的是最新版本的wasm_bindgen (v0.2.55)。我认为这应该是可能的,对吧?
发布于 2019-11-22 22:47:12
看起来还不太可能,还有一个相关的问题here。提到的一些解决方法包括通过Serde序列化/反序列化字节或JSON。但是转换成JS Array看起来像是Pauan添加的一个更好的变通方法。通过您的示例,看看这是否适用于您:
use js_sys::Array;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct Dummy {}
#[wasm_bindgen]
pub fn test() -> Array {
let dummies: Vec<Dummy> = vec![];
dummies.into_iter().map(JsValue::from).collect()
}https://stackoverflow.com/questions/58987115
复制相似问题