From 26e5820f7c0d6cdd9dff5e92e67f42da75a61bc1 Mon Sep 17 00:00:00 2001 From: Shane Madden Date: Sat, 30 Dec 2023 11:41:24 -0700 Subject: [PATCH] Simplification of ID traits down to HasId and MaybeHasId --- CHANGELOG.md | 5 + src/objects/impls/construction_site.rs | 4 +- src/objects/impls/creep.rs | 4 +- src/objects/impls/deposit.rs | 4 +- src/objects/impls/mineral.rs | 4 +- src/objects/impls/nuke.rs | 4 +- src/objects/impls/power_creep.rs | 4 +- src/objects/impls/reactor.rs | 4 +- src/objects/impls/resource.rs | 4 +- src/objects/impls/ruin.rs | 4 +- src/objects/impls/score_collector.rs | 4 +- src/objects/impls/score_container.rs | 4 +- src/objects/impls/source.rs | 4 +- src/objects/impls/structure.rs | 4 +- src/objects/impls/symbol_container.rs | 4 +- src/objects/impls/symbol_decoder.rs | 4 +- src/objects/impls/tombstone.rs | 4 +- src/traits.rs | 130 ++++++++----------------- 18 files changed, 80 insertions(+), 119 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2545bdf4..4e54af2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Unreleased ========== +- Remove `HasNativeId`, `MaybeHasNativeId`, `HasTypedId`, and `MaybeHasTypedId` traits, adding + 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 + 0.19.0 (2023-12-20) =================== diff --git a/src/objects/impls/construction_site.rs b/src/objects/impls/construction_site.rs index f65a28e9..85910029 100644 --- a/src/objects/impls/construction_site.rs +++ b/src/objects/impls/construction_site.rs @@ -85,12 +85,12 @@ impl ConstructionSite { } } -impl MaybeHasNativeId for ConstructionSite { +impl MaybeHasId for ConstructionSite { /// The Object ID of the [`ConstructionSite`], or `None` if it was created /// this tick. /// /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.id) - fn try_native_id(&self) -> Option { + fn try_js_raw_id(&self) -> Option { self.id_internal() } } diff --git a/src/objects/impls/creep.rs b/src/objects/impls/creep.rs index be462c24..041670c8 100644 --- a/src/objects/impls/creep.rs +++ b/src/objects/impls/creep.rs @@ -523,8 +523,8 @@ impl HasHits for Creep { } } -impl MaybeHasNativeId for Creep { - fn try_native_id(&self) -> Option { +impl MaybeHasId for Creep { + fn try_js_raw_id(&self) -> Option { self.id_internal() } } diff --git a/src/objects/impls/deposit.rs b/src/objects/impls/deposit.rs index 474cff23..cd8d4275 100644 --- a/src/objects/impls/deposit.rs +++ b/src/objects/impls/deposit.rs @@ -57,8 +57,8 @@ impl HasCooldown for Deposit { } } -impl HasNativeId for Deposit { - fn native_id(&self) -> JsString { +impl HasId for Deposit { + fn js_raw_id(&self) -> JsString { self.id_internal() } } diff --git a/src/objects/impls/mineral.rs b/src/objects/impls/mineral.rs index 90d2649d..fb8c11c7 100644 --- a/src/objects/impls/mineral.rs +++ b/src/objects/impls/mineral.rs @@ -49,8 +49,8 @@ extern "C" { pub fn ticks_to_regeneration(this: &Mineral) -> Option; } -impl HasNativeId for Mineral { - fn native_id(&self) -> JsString { +impl HasId for Mineral { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/nuke.rs b/src/objects/impls/nuke.rs index 8fa9ce1e..4b709f56 100644 --- a/src/objects/impls/nuke.rs +++ b/src/objects/impls/nuke.rs @@ -33,8 +33,8 @@ extern "C" { pub fn time_to_land(this: &Nuke) -> u32; } -impl HasNativeId for Nuke { - fn native_id(&self) -> JsString { +impl HasId for Nuke { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/power_creep.rs b/src/objects/impls/power_creep.rs index 3e867f67..d86ac49a 100644 --- a/src/objects/impls/power_creep.rs +++ b/src/objects/impls/power_creep.rs @@ -329,8 +329,8 @@ impl HasHits for PowerCreep { } } -impl HasNativeId for PowerCreep { - fn native_id(&self) -> JsString { +impl HasId for PowerCreep { + fn js_raw_id(&self) -> JsString { self.id_internal() } } diff --git a/src/objects/impls/reactor.rs b/src/objects/impls/reactor.rs index d471c2a3..4d5e33d1 100644 --- a/src/objects/impls/reactor.rs +++ b/src/objects/impls/reactor.rs @@ -54,8 +54,8 @@ extern "C" { pub fn owner(this: &Reactor) -> Option; } -impl HasNativeId for Reactor { - fn native_id(&self) -> JsString { +impl HasId for Reactor { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/resource.rs b/src/objects/impls/resource.rs index b147b1f7..64ea8244 100644 --- a/src/objects/impls/resource.rs +++ b/src/objects/impls/resource.rs @@ -33,8 +33,8 @@ extern "C" { pub fn resource_type(this: &Resource) -> ResourceType; } -impl HasNativeId for Resource { - fn native_id(&self) -> JsString { +impl HasId for Resource { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/ruin.rs b/src/objects/impls/ruin.rs index 7b0b9621..5a63c45f 100644 --- a/src/objects/impls/ruin.rs +++ b/src/objects/impls/ruin.rs @@ -58,8 +58,8 @@ impl CanDecay for Ruin { } } -impl HasNativeId for Ruin { - fn native_id(&self) -> JsString { +impl HasId for Ruin { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/score_collector.rs b/src/objects/impls/score_collector.rs index 520c014a..82d291a0 100644 --- a/src/objects/impls/score_collector.rs +++ b/src/objects/impls/score_collector.rs @@ -34,8 +34,8 @@ extern "C" { pub fn store(this: &ScoreCollector) -> Store; } -impl HasNativeId for ScoreCollector { - fn native_id(&self) -> JsString { +impl HasId for ScoreCollector { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/score_container.rs b/src/objects/impls/score_container.rs index 0d250b07..df885bdf 100644 --- a/src/objects/impls/score_container.rs +++ b/src/objects/impls/score_container.rs @@ -47,8 +47,8 @@ impl CanDecay for ScoreContainer { } } -impl HasNativeId for ScoreContainer { - fn native_id(&self) -> JsString { +impl HasId for ScoreContainer { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/source.rs b/src/objects/impls/source.rs index 256353c3..6279b6d0 100644 --- a/src/objects/impls/source.rs +++ b/src/objects/impls/source.rs @@ -53,8 +53,8 @@ extern "C" { pub fn ticks_to_regeneration(this: &Source) -> Option; } -impl HasNativeId for Source { - fn native_id(&self) -> JsString { +impl HasId for Source { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/structure.rs b/src/objects/impls/structure.rs index e231d63b..f96db229 100644 --- a/src/objects/impls/structure.rs +++ b/src/objects/impls/structure.rs @@ -76,11 +76,11 @@ impl Structure { } } -impl HasNativeId for T +impl HasId for T where T: AsRef, { - fn native_id(&self) -> JsString { + fn js_raw_id(&self) -> JsString { Structure::id_internal(self.as_ref()) } } diff --git a/src/objects/impls/symbol_container.rs b/src/objects/impls/symbol_container.rs index daa335f9..d6963b8a 100644 --- a/src/objects/impls/symbol_container.rs +++ b/src/objects/impls/symbol_container.rs @@ -56,8 +56,8 @@ impl CanDecay for SymbolContainer { } } -impl HasNativeId for SymbolContainer { - fn native_id(&self) -> JsString { +impl HasId for SymbolContainer { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/symbol_decoder.rs b/src/objects/impls/symbol_decoder.rs index 08db9019..1f323a83 100644 --- a/src/objects/impls/symbol_decoder.rs +++ b/src/objects/impls/symbol_decoder.rs @@ -37,8 +37,8 @@ extern "C" { pub fn score_multiplier(this: &SymbolDecoder) -> u32; } -impl HasNativeId for SymbolDecoder { - fn native_id(&self) -> JsString { +impl HasId for SymbolDecoder { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/objects/impls/tombstone.rs b/src/objects/impls/tombstone.rs index 62623acb..d032590c 100644 --- a/src/objects/impls/tombstone.rs +++ b/src/objects/impls/tombstone.rs @@ -57,8 +57,8 @@ impl CanDecay for Tombstone { } } -impl HasNativeId for Tombstone { - fn native_id(&self) -> JsString { +impl HasId for Tombstone { + fn js_raw_id(&self) -> JsString { Self::id_internal(self) } } diff --git a/src/traits.rs b/src/traits.rs index 5878347f..eea21c75 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -49,122 +49,78 @@ pub trait HasCooldown { fn cooldown(&self) -> u32; } -pub trait HasNativeId { - fn native_id(&self) -> JsString; -} - -pub trait MaybeHasNativeId { - fn try_native_id(&self) -> Option; -} - -impl MaybeHasNativeId for T -where - T: HasNativeId, -{ - fn try_native_id(&self) -> Option { - Some(::native_id(self)) - } -} - pub trait Resolvable: From {} -impl Resolvable for T where T: MaybeHasTypedId + From {} - -#[enum_dispatch] -pub trait HasId { - /// Object ID of the object, which can be used to efficiently fetch a - /// fresh reference to the object on subsequent ticks. - fn raw_id(&self) -> RawObjectId; -} - -impl HasId for T -where - T: HasNativeId, -{ - fn raw_id(&self) -> RawObjectId { - let id: String = self.native_id().into(); - - RawObjectId::from_str(&id).expect("expected object ID to be parseable") - } -} +impl Resolvable for T where T: MaybeHasId + From {} +/// Trait for all game objects which have an associated unique identifier. #[enum_dispatch] -pub trait HasTypedId { - /// Object ID of the object, which can be used to efficiently fetch a - /// fresh reference to the object on subsequent ticks. - fn id(&self) -> ObjectId; - - fn js_id(&self) -> JsObjectId; -} - -impl HasTypedId for T -where - T: HasId + HasNativeId, -{ +pub trait HasId { + /// Object ID of the object stored in Rust memory, which can be used to + /// efficiently fetch a fresh reference to the object on subsequent + /// ticks. fn id(&self) -> ObjectId { self.raw_id().into() } - fn js_id(&self) -> JsObjectId { - self.native_id().into() - } -} + /// Object ID of the object stored in Rust memory, without its associated + /// type information. + fn raw_id(&self) -> RawObjectId { + let id: String = self.js_raw_id().into(); -impl HasTypedId for &T -where - T: HasId + HasNativeId, -{ - fn id(&self) -> ObjectId { - self.raw_id().into() + RawObjectId::from_str(&id).expect("expected object ID to be parseable") } + /// Object ID of the object stored in JavaScript memory, which can be used + /// to efficiently fetch a fresh reference to the object on subsequent + /// ticks. fn js_id(&self) -> JsObjectId { - self.native_id().into() + self.js_raw_id().into() } + + /// Object ID of the object stored in JavaScript memory, without its + /// associated type information. + fn js_raw_id(&self) -> JsString; } +/// Trait for all game objects which may (or may not) have an associated unique +/// identifier. #[enum_dispatch] -pub trait MaybeHasId { +pub trait MaybeHasId { /// 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. - fn try_raw_id(&self) -> Option; -} + /// object doesn't currently have an ID. + fn try_id(&self) -> Option> { + self.try_raw_id().map(Into::into) + } -impl MaybeHasId for T -where - T: MaybeHasNativeId, -{ + /// Object ID of the object, without its associated type information, or + /// `None` if the object doesn't currently have an ID. fn try_raw_id(&self) -> Option { - self.try_native_id() + self.try_js_raw_id() .map(String::from) .map(|id| RawObjectId::from_str(&id).expect("expected object ID to be parseable")) } -} - -#[enum_dispatch] -pub trait MaybeHasTypedId { - /// 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. - fn try_id(&self) -> Option>; -} -impl MaybeHasTypedId for T -where - T: MaybeHasId, -{ - fn try_id(&self) -> Option> { - self.try_raw_id().map(Into::into) + /// Object ID of the object stored in JavaScript memory, 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. + fn try_js_id(&self) -> Option> { + self.try_js_raw_id().map(Into::into) } + + /// Object ID of the object stored in JavaScript memory, without its + /// associated type information, or `None` if the object doesn't currently + /// have an ID. + fn try_js_raw_id(&self) -> Option; } -impl MaybeHasTypedId for &T +impl MaybeHasId for T where - T: MaybeHasId, + T: HasId, { - fn try_id(&self) -> Option> { - self.try_raw_id().map(Into::into) + fn try_js_raw_id(&self) -> Option { + Some(self.js_raw_id()) } }