feat(napi): impl FromNapiValue for HashMap with custom hasher (#1682)

This commit is contained in:
plodsoft 2023-08-11 11:05:04 +08:00 committed by GitHub
parent 66b10ed661
commit 8bf32be7d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::hash::Hash;
use std::hash::{BuildHasher, Hash};
use crate::bindgen_prelude::{Env, Result, ToNapiValue, *};
@ -31,14 +31,15 @@ where
}
}
impl<K, V> FromNapiValue for HashMap<K, V>
impl<K, V, S> FromNapiValue for HashMap<K, V, S>
where
K: From<String> + Eq + Hash,
V: FromNapiValue,
S: Default + BuildHasher,
{
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
let obj = unsafe { Object::from_napi_value(env, napi_val)? };
let mut map = HashMap::new();
let mut map = HashMap::default();
for key in Object::keys(&obj)?.into_iter() {
if let Some(val) = obj.get(&key)? {
map.insert(K::from(key), val);