Rust's HashMap type gets translated to the Map type in typescript. It is possible to use a struct containing a map as a parameter. However the generated glue code incorrectly uses JSON.stringify to convert the parameter into a string. This doesn't work on the Map member as the contained values are ignored. The code thus outputs an empty objects which doesn't contain the values of the Map.
Rust code:
use deno_bindgen::deno_bindgen;
use std::collections::HashMap;
#[deno_bindgen]
struct MyStruct {
my_map: HashMap<String, String>,
}
#[deno_bindgen]
fn my_fn(my_struct: MyStruct) {
println!("{:?}", my_struct.my_map);
}
Generated binding for struct:
export type MyStruct = {
my_map: Map<string, string>
}
Faulty binding using JSON.stringify:
export function my_fn(a0: MyStruct) {
const a0_buf = encode(JSON.stringify(a0))
let rawResult = _lib.symbols.my_fn(a0_buf, a0_buf.byteLength)
const result = rawResult
return result
}
As such the follow code will not output {a:'b'} but instead {}.
import { my_fn } from "./bindings/bindings.ts";
my_fn({ my_map: new Map([["a", "b"]]) });
This could be mitigated by using Record<T, U> instead of Map<T, U> as it refers to a plain JS-Object which can be properly converted by JSON.stringify.
Rust's
HashMap
type gets translated to theMap
type in typescript. It is possible to use a struct containing a map as a parameter. However the generated glue code incorrectly usesJSON.stringify
to convert the parameter into a string. This doesn't work on theMap
member as the contained values are ignored. The code thus outputs an empty objects which doesn't contain the values of theMap
.Rust code:
Generated binding for struct:
Faulty binding using
JSON.stringify
:As such the follow code will not output
{a:'b'}
but instead{}
.This could be mitigated by using
Record<T, U>
instead ofMap<T, U>
as it refers to a plain JS-Object which can be properly converted byJSON.stringify
.