diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c9fcad7..658bab05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Unreleased their functions to the `HasId` and `MaybeHasId` traits - Renamed `native_id`/`try_native_id` to `js_raw_id`/`try_js_raw_id` for consistency with the other trait functions +- Remove `Resolvable` trait, moving its functionality to `MaybeHasId` 0.19.0 (2023-12-20) =================== diff --git a/src/game.rs b/src/game.rs index 4be905bf..b036f3c3 100644 --- a/src/game.rs +++ b/src/game.rs @@ -19,6 +19,7 @@ use crate::{ js_collections::{JsHashMap, JsObjectId}, local::{ObjectId, RawObjectId, RoomName}, objects::{AccountPowerCreep, ConstructionSite, Creep, Flag, Room, RoomObject, StructureSpawn}, + traits::MaybeHasId, }; pub mod cpu; @@ -171,11 +172,9 @@ pub fn symbols() -> JsHashMap { /// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById) pub fn get_object_by_js_id_typed(id: &JsObjectId) -> Option where - T: From, + T: MaybeHasId, { - Game::get_object_by_id(&id.raw) - .map(JsValue::from) - .map(Into::into) + Game::get_object_by_id(&id.raw).map(JsCast::unchecked_into) } /// Get the typed object represented by a given [`ObjectId`], if it's still @@ -184,14 +183,12 @@ where /// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById) pub fn get_object_by_id_typed(id: &ObjectId) -> Option where - T: From, + T: MaybeHasId, { // construct a reference to a javascript string using the id data let js_str = JsString::from(id.to_string()); - Game::get_object_by_id(&js_str) - .map(JsValue::from) - .map(Into::into) + Game::get_object_by_id(&js_str).map(JsCast::unchecked_into) } /// Get the [`RoomObject`] represented by a given [`RawObjectId`], if it's diff --git a/src/js_collections.rs b/src/js_collections.rs index 554335b4..575b2682 100644 --- a/src/js_collections.rs +++ b/src/js_collections.rs @@ -241,7 +241,7 @@ impl JsObjectId { /// don't have vision for. pub fn resolve(&self) -> Option where - T: Resolvable, + T: MaybeHasId, { game::get_object_by_js_id_typed(self) } diff --git a/src/local/object_id.rs b/src/local/object_id.rs index e08014d4..e4f8733d 100644 --- a/src/local/object_id.rs +++ b/src/local/object_id.rs @@ -10,7 +10,7 @@ use arrayvec::ArrayString; use serde::{Deserialize, Serialize}; use wasm_bindgen::JsCast; -use crate::{game, Resolvable, RoomObject}; +use crate::{game, objects::RoomObject, traits::MaybeHasId}; mod errors; mod raw; @@ -157,9 +157,8 @@ impl ObjectId { self.raw.to_array_string() } - /// Resolves this object ID into an object. - /// - /// This is a shortcut for [`game::get_object_by_id_typed(id)`][1] + /// Resolves this object ID into an object, verifying that the returned + /// object matches the expected type. /// /// # Errors /// @@ -168,11 +167,9 @@ impl ObjectId { /// /// Will return `Ok(None)` if the object no longer exists, or is in a room /// we don't have vision for. - /// - /// [1]: crate::game::get_object_by_id_typed pub fn try_resolve(self) -> Result, RoomObject> where - T: Resolvable + JsCast, + T: MaybeHasId, { match game::get_object_by_id_erased(&self.raw) { Some(v) => v.dyn_into().map(|v| Some(v)), @@ -189,7 +186,7 @@ impl ObjectId { /// don't have vision for. pub fn resolve(self) -> Option where - T: Resolvable, + T: MaybeHasId, { game::get_object_by_id_typed(&self) } diff --git a/src/objects/impls/structure.rs b/src/objects/impls/structure.rs index 1016ebad..aacf7ef7 100644 --- a/src/objects/impls/structure.rs +++ b/src/objects/impls/structure.rs @@ -78,7 +78,7 @@ impl Structure { impl HasId for T where - T: AsRef, + T: AsRef + JsCast, { fn js_raw_id(&self) -> JsString { Structure::id_internal(self.as_ref()) diff --git a/src/traits.rs b/src/traits.rs index 9094bb03..389756d5 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -49,10 +49,6 @@ pub trait HasCooldown { fn cooldown(&self) -> u32; } -pub trait Resolvable: From {} - -impl Resolvable for T where T: MaybeHasId + From {} - /// Trait for all game objects which have an associated unique identifier. pub trait HasId: MaybeHasId { /// Object ID of the object stored in Rust memory, which can be used to @@ -90,7 +86,7 @@ pub trait HasId: MaybeHasId { /// Trait for all game objects which may (or may not) have an associated unique /// identifier. -pub trait MaybeHasId { +pub trait MaybeHasId: JsCast { /// Object ID of the object, which can be used to efficiently fetch a /// fresh reference to the object on subsequent ticks, or `None` if the /// object doesn't currently have an ID. @@ -127,7 +123,7 @@ pub trait MaybeHasId { impl MaybeHasId for T where - T: HasId, + T: HasId + JsCast, { fn try_js_raw_id(&self) -> Option { Some(self.js_raw_id())