Alternative to `String` for a `Copy` struct

Hi there!

I'm currently making a WebAssembly module which manipulates large chunks of data, which need to be partly available from a TypeScript application.

So I'm using wasm-bindgen with the #[wasm_bindgen] attribute on every struct that needs to be available from TypeScript, but I currently have an issue:

Several structs contain String values, which do not implement Copy. But, #[wasm_bingen] requires the underyling type to implement Copy, which is a problem. Also, it doesn't work on structs with lifetimes.

As I need to send back some structs with strings inside from the Rust module to the TS application, what solution do I have? Is there an alternative string-like type that implements Copy while being stored on the heap?

Thanks in advance for your help!

A data structure managing any heap-allocated memory cannot possibly be Copy. But you can always return a JsValue instead of a strongly-typed struct directly.

Have you tried annotating the String fields (or the whole struct, that should also work) with #[wasm_bindgen(getter_with_clone)]?

3 Likes

I just found this solution a few minutes ago.

It indeed works perfectly, thanks a lot!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.