I'm working on a Rust app that needs to compile for WebAssembly, which means exposing quite a lot of internal & external structs for use from TypeScript. I want to write a macro to automate this job as it's quite tedious.
I want is a macro wasm_wrap_type!(WasmStruct, ExternalCrateStruct)
which produces a struct with the wasm_bindgen
attribute called WasmStruct
that implements all the public methods of ExternalCrateStruct
(and added JavaScript getters & setters for properties).
I know that libraries like serde iterate over members using a derive
macro, but I don't think that's possible to use in this situation.
My end goal is that calling this:
wasm_wrap_type!(WasmStruct, ExternalCrateStruct)
Generates this:
#[wasm_bindgen]
pub struct WasmStruct(ExternalCrateStruct);
#[wasm_bindgen]
impl WasmStruct {
// For each member of ExternalCrateStruct...
#[wasm_bindgen(setter)]
pub fn member(&self) -> TypeOfSetter {
&self.0.member
}
#[wasm_bindgen(getter)]
pub fn set_member(&mut self, new_value: TypeOfSetter) {
self.0.member = new_value;
}
// For each ecsFunction of ExternalCrateStruct...
pub fn ecsFunction(&self, args...) -> ReturnType? {
self.0.ecsFunction(args...)
}
}
Is it possible to iterate over the member and public functions of a struct defined in a separate crate?