I'm attempting to write a data store in Rust that receives objects from JavaScript across the wasm-bindgen boundary and stores them for later retrieval. This is a simplified version of what I was hoping would work:
static mut MAP: HashMap<i32, String> = HashMap::new();
#[wasm_bindgen]
pub fn add_value(index: i32, value: String) {
unsafe {
MAP.insert(index, value);
}
}
#[wasm_bindgen]
pub fn get_value(index: i32) -> String {
unsafe {
(*MAP.get(&index).unwrap()).clone()
}
}
However I get this error from the Rust compiler:
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
How can I store state so that subsequent calls across the wasm-bindgen boundary can retrieve previously stored values?