Skip to content

Commit

Permalink
Add JsHashMap::entries method
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbot95 committed Mar 9, 2024
1 parent cfef1c2 commit 3aa17a5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/js_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ where
}
}

impl<K, V> JsHashMap<K, V>
where
K: JsCollectionFromValue,
V: JsCollectionFromValue,
{
pub fn entries(&self) -> impl Iterator<Item=(K, V)> {
let array = Object::entries(self.map.unchecked_ref());

OwnedArrayIter::new(array)
}
}


impl<K, V> JsHashMap<K, V>
where
K: JsCollectionIntoValue,
Expand Down Expand Up @@ -307,3 +320,16 @@ impl JsCollectionFromValue for u8 {
}
}
}

impl<K, V> JsCollectionFromValue for (K, V)
where
K: JsCollectionFromValue,
V: JsCollectionFromValue,
{
fn from_value(val: JsValue) -> Self {
let val: &Array = val.dyn_ref().expect("expected tuple of length 2");
let k = K::from_value(val.get(0));
let v = V::from_value(val.get(1));
(k, v)
}
}

0 comments on commit 3aa17a5

Please sign in to comment.