From 2b49b646735b31be3262553552031c79679731cb Mon Sep 17 00:00:00 2001 From: scottbot95 Date: Wed, 13 Mar 2024 18:27:05 -0700 Subject: [PATCH] Add `JsHashMap::entries` method (#511) --- CHANGELOG.md | 1 + src/js_collections.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7364e26..5dc79580 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Unreleased which returns a `Result<&'static [ResourceType], StoreObjectConversionError>` - Add missing `StoreObject::Reactor` to the `seasonal-season-5` feature - Implement `Serialize` and `Deserialize` for `RoomStatus` +- Add function `JsHashMap::entries` ### Bugfixes: diff --git a/src/js_collections.rs b/src/js_collections.rs index 8be65c58..c1d5ecab 100644 --- a/src/js_collections.rs +++ b/src/js_collections.rs @@ -59,6 +59,18 @@ where } } +impl JsHashMap +where + K: JsCollectionFromValue, + V: JsCollectionFromValue, +{ + pub fn entries(&self) -> impl Iterator { + let array = Object::entries(self.map.unchecked_ref()); + + OwnedArrayIter::new(array) + } +} + impl JsHashMap where K: JsCollectionIntoValue, @@ -307,3 +319,16 @@ impl JsCollectionFromValue for u8 { } } } + +impl 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) + } +}