wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Allow mapping HashMap or BTreeMap

Open ctron opened this issue 5 years ago • 1 comments

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.

ctron avatar Oct 29 '20 10:10 ctron

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.

ChristianIvicevic avatar Nov 04 '23 13:11 ChristianIvicevic