wasm-bindgen
wasm-bindgen copied to clipboard
Allow mapping HashMap or BTreeMap
Motivation
Having to deal with a structure of:
export type State = {|
styles: {|
[key: string]: $Shape<CSSStyleDeclaration>,
|},
attributes: {|
[key: string]: { [key: string]: string | boolean },
|}
}
It would be helpful to have support, mapping this to a map type, like HashMap or BTreeMap.
Proposed Solution
Implement IntoWasmAbi for HashMap, BTreeMap, or both.
My use case is that I initially tried to use the following pattern which isn't supported by wasm-bindgen at the moment:
enum ItemKind {
VariantOne,
VariantTwo,
}
pub fn consume_from_js(values: impl Into<HashMap<ItemKind, usize>>) {}
My current workaround is to use an automatically generated struct based on the enum that behaves like an exhaustive HashMap using a bunch of derive_builder magic and has the following shape:
struct ItemKindStruct {
variant_one: Option<usize>,
variant_two: Option<usize>,
}
Without this the most "idiomatic" approach is most likely to work with JsValue across the boundary and trying to (de)serialize it, however it's not type-safe at the JS call-site and having to handle parsing in the Rust code is also inconvenient. Therefore I'd be really happy to see at least HashMap being supported at some point if possible.