use std::collections::HashMap; use std::hash::Hash; use crate::bindgen_prelude::{Env, Result, ToNapiValue, *}; impl TypeName for HashMap { fn type_name() -> &'static str { "HashMap" } fn value_type() -> ValueType { ValueType::Object } } impl ToNapiValue for HashMap where K: AsRef, V: ToNapiValue, { unsafe fn to_napi_value(raw_env: sys::napi_env, val: Self) -> Result { let env = Env::from(raw_env); let mut obj = env.create_object()?; for (k, v) in val.into_iter() { obj.set(k.as_ref(), v)?; } Object::to_napi_value(raw_env, obj) } } impl FromNapiValue for HashMap where K: From + Eq + Hash, V: FromNapiValue, { unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result { let obj = Object::from_napi_value(env, napi_val)?; let mut map = HashMap::new(); for key in Object::keys(&obj)?.into_iter() { if let Some(val) = obj.get(&key)? { map.insert(K::from(key), val); } } Ok(map) } }