From f1b9c7b7dc915c255a2fd57a7e9c2fa2dbb91af4 Mon Sep 17 00:00:00 2001 From: pajotg <33227855+pajotg@users.noreply.github.com> Date: Sat, 6 Jan 2024 20:08:51 +0100 Subject: [PATCH] fix: set_default_public_segment and set_public_segments are no longer panicking (#479) --- CHANGELOG.md | 4 ++++ src/raw_memory.rs | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56eeceda..84bfcff6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ Unreleased - Remove `Resolvable` trait, moving its functionality to `MaybeHasId` - Remove `ObjectWithId` and `ObjectWithMaybeId` enums +### Bugfixes: + +- Fix `raw_memory::set_public_segments` and `set_default_public_segment` argument conversion + 0.19.0 (2023-12-20) =================== diff --git a/src/raw_memory.rs b/src/raw_memory.rs index b1d6032f..2fa2206c 100644 --- a/src/raw_memory.rs +++ b/src/raw_memory.rs @@ -40,10 +40,10 @@ extern "C" { fn set_active_foreign_segment(username: &JsString, segment_id: Option); #[wasm_bindgen(static_method_of = RawMemory, js_name = setDefaultPublicSegment)] - fn set_default_public_segment(segment_id: Option); + fn set_default_public_segment(segment_id: JsValue); #[wasm_bindgen(static_method_of = RawMemory, js_name = setPublicSegments)] - fn set_public_segments(segment_ids: &[u8]); + fn set_public_segments(segment_ids: &Array); } /// Get a [`JsHashMap`] with all of the segments requested on @@ -108,7 +108,11 @@ pub fn set_active_foreign_segment(username: &JsString, segment_id: Option) { /// /// [Screeps documentation](https://docs.screeps.com/api/#RawMemory.setDefaultPublicSegment) pub fn set_default_public_segment(segment_id: Option) { - RawMemory::set_default_public_segment(segment_id) + RawMemory::set_default_public_segment( + segment_id + .map(|f| JsValue::from_f64(f as f64)) + .unwrap_or(JsValue::NULL), + ) } /// Sets which of your memory segments are readable to other players as @@ -116,7 +120,13 @@ pub fn set_default_public_segment(segment_id: Option) { /// /// [Screeps documentation](https://docs.screeps.com/api/#RawMemory.setPublicSegments) pub fn set_public_segments(segment_ids: &[u8]) { - RawMemory::set_public_segments(segment_ids) + let segment_ids: Array = segment_ids + .iter() + .map(|s| *s as f64) + .map(JsValue::from_f64) + .collect(); + + RawMemory::set_public_segments(&segment_ids) } #[wasm_bindgen]