如何从WASM_BINDGEN函数返回嵌套数组

发布于 2025-01-31 12:13:33 字数 632 浏览 3 评论 0原文

我有一个Rust程序,我想在JavaScript中以Web组件的身份运行。我有一个要返回3D数组的函数,因此我可以在前端的图表上绘制一些数据,但是我找不到返回此类数据的方法。看看JS_SYS,没有一般数组类型,只有2D数组(例如UINT32Array)的特定类型。

我希望函数签名看起来像这样:

#[wasm_bindgen]
pub fn sk_dataset(input: &str) -> Vec<Vec<i32>>;

我尝试过使用WASM_BINDGEN DECONATOR返回结构:

#[wasm_bindgen]
pub struct DataSet {

    pub data: Vec<Vec<i32>>
}

但是我遇到了一个错误,说明

the trait bound `Vec<i32>: JsObject` is not satisfied
required because of the requirements on the impl of `IntoWasmAbi` for `Box<[Vec<i32>]>`

教程

I have a rust program which I would like to run as web assembly in javascript. I have a function which I want to return a 3D array, so I can plot some data on a graph on the frontend, but I cannot find a way to return such data. Having a look at js_sys there is no general Array type, only specific types for 2D arrays such as Uint32Array.

I would like the function signature to look something like this:

#[wasm_bindgen]
pub fn sk_dataset(input: &str) -> Vec<Vec<i32>>;

I have tried return a struct with the wasm_bindgen decorator like this:

#[wasm_bindgen]
pub struct DataSet {

    pub data: Vec<Vec<i32>>
}

But I get an error stating that

the trait bound `Vec<i32>: JsObject` is not satisfied
required because of the requirements on the impl of `IntoWasmAbi` for `Box<[Vec<i32>]>`

The tutorial over at https://rustwasm.github.io/docs/wasm-bindgen/reference/types/exported-rust-types.html only has examples of structs exported with basic types like i32s, how can I export a struct with more complex members?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文