首页 > 解决方案 > 返回 Box<[Box<[JsValue]>]> 不起作用,因为 IntoWasmAbi 未实现

问题描述

我正在尝试在JsValue数组上创建一个块方法。有没有办法将修改后的数组传回 JavaScript?

这就是我想要做的:

#[wasm_bindgen]
pub fn chunk(array: Box<[JsValue]>, size: usize) -> Box<[Box<[JsValue]>]> {
    array
        .chunks(size)
        .map(|el| {
            el.iter()
                .map(|el| el.to_owned())
                .collect::<Box<[JsValue]>>()
        })
        .collect()
}

我在编译过程中得到了这个:

the trait `wasm_bindgen::convert::traits::IntoWasmAbi` is not
implemented for
`std::boxed::Box<[std::boxed::Box<[wasm_bindgen::JsValue]>]>`

我也尝试过 js_sys,但它似乎比纯 JS 实现需要更长的时间,因为它只是一个 JS API 包装器

#[wasm_bindgen]
pub fn chunk(array: Box<[JsValue]>, size: usize) -> Array {
    let root = Array::new();

    for chunk in array.chunks(size) {
        let arr = Array::new();

        for ch in chunk {
            arr.push(ch);
        }

        root.push(&arr);
    }

    root
}

标签: rustwasm-bindgen

解决方案


推荐阅读