Replies: 1 comment
-
The following example worked for me. There's probably other ways, I don't know if there's a better way. #[wasm_bindgen]
#[derive(Debug)]
struct BindingExample {
data: String,
}
#[wasm_bindgen]
extern "C" {
type CustomMap;
#[wasm_bindgen(catch, method)]
fn get(this: &CustomMap, key: String) -> Result<BindingExample, JsValue>;
#[wasm_bindgen(catch, method)]
fn set(this: &CustomMap, key: String, v: BindingExample) -> Result<JsValue, JsValue>;
}
fn test() {
let v = BindingExample {
data: String::from("hello"),
};
let map: JsValue = js_sys::Map::new().into();
let map: CustomMap = map.into();
let _ = map.set("v".into(), v);
let v: BindingExample = map.get("v".into()).unwrap();
console_log!("{v:?}");
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to pass around instances of my own type from Rust to JS, then back inside some
Map
:then on the JS side I want to have something like:
The problem arises when I want to use the contents of the map from the Rust side - looks like I can't
dyn_into
nordyn_ref
to convert toFoo
, even though it is marked as#[wasm_bindgen]
:This gives the following error:
Is this possible? Should I implement something manually for my type for this to work? Should/can I use some other type like
std::collections::HashMap
?Beta Was this translation helpful? Give feedback.
All reactions