Skip to content

Commit

Permalink
Remove Resolvable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden committed Dec 31, 2023
1 parent ca6d864 commit a7b7a83
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
===================
Expand Down
13 changes: 5 additions & 8 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,11 +172,9 @@ pub fn symbols() -> JsHashMap<crate::ResourceType, u32> {
/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
pub fn get_object_by_js_id_typed<T>(id: &JsObjectId<T>) -> Option<T>
where
T: From<JsValue>,
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
Expand All @@ -184,14 +183,12 @@ where
/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
pub fn get_object_by_id_typed<T>(id: &ObjectId<T>) -> Option<T>
where
T: From<JsValue>,
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
Expand Down
2 changes: 1 addition & 1 deletion src/js_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<T> JsObjectId<T> {
/// don't have vision for.
pub fn resolve(&self) -> Option<T>
where
T: Resolvable,
T: MaybeHasId,
{
game::get_object_by_js_id_typed(self)
}
Expand Down
13 changes: 5 additions & 8 deletions src/local/object_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,9 +157,8 @@ impl<T> ObjectId<T> {
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
///
Expand All @@ -168,11 +167,9 @@ impl<T> ObjectId<T> {
///
/// 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<Option<T>, 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)),
Expand All @@ -189,7 +186,7 @@ impl<T> ObjectId<T> {
/// don't have vision for.
pub fn resolve(self) -> Option<T>
where
T: Resolvable,
T: MaybeHasId,
{
game::get_object_by_id_typed(&self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Structure {

impl<T> HasId for T
where
T: AsRef<Structure>,
T: AsRef<Structure> + JsCast,
{
fn js_raw_id(&self) -> JsString {
Structure::id_internal(self.as_ref())
Expand Down
8 changes: 2 additions & 6 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ pub trait HasCooldown {
fn cooldown(&self) -> u32;
}

pub trait Resolvable: From<JsValue> {}

impl<T> Resolvable for T where T: MaybeHasId + From<JsValue> {}

/// 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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -127,7 +123,7 @@ pub trait MaybeHasId {

impl<T> MaybeHasId for T
where
T: HasId,
T: HasId + JsCast,
{
fn try_js_raw_id(&self) -> Option<JsString> {
Some(self.js_raw_id())
Expand Down

0 comments on commit a7b7a83

Please sign in to comment.