screeps/
inter_shard_memory.rs

1//! Interface for Screeps inter-shard memory, allowing communication between
2//! instances of your code running on different shards.
3//!
4//! [Screeps documentation](https://docs.screeps.com/api/#InterShardMemory)
5use js_sys::JsString;
6use wasm_bindgen::prelude::*;
7
8#[wasm_bindgen]
9extern "C" {
10    type InterShardMemory;
11
12    #[wasm_bindgen(static_method_of = InterShardMemory, js_name = getLocal)]
13    fn get_local() -> Option<JsString>;
14
15    #[wasm_bindgen(static_method_of = InterShardMemory, js_name = setLocal)]
16    fn set_local(val: &JsString);
17
18    #[wasm_bindgen(static_method_of = InterShardMemory, js_name = getRemote)]
19    fn get_remote(shard: &JsString) -> Option<JsString>;
20}
21
22/// Get the current local [`JsString`] intershard memory for this shard.
23///
24/// [Screeps documentation](https://docs.screeps.com/api/#InterShardMemory.getLocal)
25pub fn get_local() -> Option<JsString> {
26    InterShardMemory::get_local()
27}
28
29/// Overwrite the current shard's intershard memory segment with new
30/// contents.  Maximum allowed length of [`INTER_SHARD_MEMORY_SIZE_LIMIT`]
31/// UTF-16 units.
32///
33/// [Screeps documentation](https://docs.screeps.com/api/#InterShardMemory.setLocal)
34///
35/// [`INTER_SHARD_MEMORY_SIZE_LIMIT`]:
36/// crate::constants::INTER_SHARD_MEMORY_SIZE_LIMIT
37pub fn set_local(val: &JsString) {
38    InterShardMemory::set_local(val)
39}
40
41/// Get the data that another shard's code instance has written to its
42/// intershard memory segment.
43///
44/// [Screeps documentation](https://docs.screeps.com/api/#InterShardMemory.getRemote)
45pub fn get_remote(shard: &JsString) -> Option<JsString> {
46    InterShardMemory::get_remote(shard)
47}