Skip to content

Commit

Permalink
prototype non-generic HasId
Browse files Browse the repository at this point in the history
  • Loading branch information
asquared31415 committed Dec 30, 2023
1 parent 1c6aafc commit ca6d864
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 106 deletions.
85 changes: 1 addition & 84 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,89 +118,6 @@ pub enum CooldownObject {
StructureTerminal,
}

#[enum_dispatch(HasId)]
pub enum ObjectWithId {
Deposit,
Mineral,
Nuke,
Resource,
Ruin,
#[cfg(feature = "score")]
ScoreCollector,
#[cfg(feature = "score")]
ScoreContainer,
Source,
StructureContainer,
StructureController,
StructureExtension,
StructureExtractor,
StructureFactory,
StructureInvaderCore,
StructureKeeperLair,
StructureLab,
StructureLink,
StructureNuker,
StructureObserver,
StructurePortal,
StructurePowerBank,
StructurePowerSpawn,
StructureRampart,
StructureRoad,
StructureSpawn,
StructureStorage,
StructureTerminal,
StructureTower,
StructureWall,
#[cfg(feature = "symbols")]
SymbolContainer,
#[cfg(feature = "symbols")]
SymbolDecoder,
Tombstone,
}

#[enum_dispatch(MaybeHasId)]
pub enum ObjectWithMaybeId {
ConstructionSite,
Creep,
Deposit,
Mineral,
Nuke,
PowerCreep,
Resource,
Ruin,
#[cfg(feature = "score")]
ScoreCollector,
#[cfg(feature = "score")]
ScoreContainer,
Source,
StructureContainer,
StructureController,
StructureExtension,
StructureExtractor,
StructureFactory,
StructureInvaderCore,
StructureKeeperLair,
StructureLab,
StructureLink,
StructureNuker,
StructureObserver,
StructurePortal,
StructurePowerBank,
StructurePowerSpawn,
StructureRampart,
StructureRoad,
StructureSpawn,
StructureStorage,
StructureTerminal,
StructureTower,
StructureWall,
#[cfg(feature = "symbols")]
SymbolContainer,
#[cfg(feature = "symbols")]
SymbolDecoder,
Tombstone,
}

#[enum_dispatch(HasPosition)]
pub enum ObjectWithPosition {
ConstructionSite,
Expand Down Expand Up @@ -273,7 +190,7 @@ pub enum StoreObject {

/// Enum used for converting a [`Structure`] into a typed object of its specific
/// structure type.
#[enum_dispatch(OwnedStructureProperties, HasId)]
#[enum_dispatch(OwnedStructureProperties)]
pub enum OwnedStructureObject {
StructureController,
StructureExtension,
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/construction_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl ConstructionSite {
}
}

impl MaybeHasId<ConstructionSite> for ConstructionSite {
impl MaybeHasId for ConstructionSite {
/// The Object ID of the [`ConstructionSite`], or `None` if it was created
/// this tick.
///
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ impl HasHits for Creep {
}
}

impl MaybeHasId<Creep> for Creep {
impl MaybeHasId for Creep {
/// The Object ID of the [`Creep`], or `None` if it began spawning this
/// tick.
///
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl HasCooldown for Deposit {
}
}

impl HasId<Deposit> for Deposit {
impl HasId for Deposit {
fn js_raw_id(&self) -> JsString {
self.id_internal()
}
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/mineral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern "C" {
pub fn ticks_to_regeneration(this: &Mineral) -> Option<u32>;
}

impl HasId<Mineral> for Mineral {
impl HasId for Mineral {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/nuke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern "C" {
pub fn time_to_land(this: &Nuke) -> u32;
}

impl HasId<Nuke> for Nuke {
impl HasId for Nuke {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/power_creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ impl HasHits for PowerCreep {
}
}

impl HasId<PowerCreep> for PowerCreep {
impl HasId for PowerCreep {
fn js_raw_id(&self) -> JsString {
self.id_internal()
}
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern "C" {
pub fn resource_type(this: &Resource) -> ResourceType;
}

impl HasId<Resource> for Resource {
impl HasId for Resource {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/ruin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl CanDecay for Ruin {
}
}

impl HasId<Ruin> for Ruin {
impl HasId for Ruin {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" {
pub fn ticks_to_regeneration(this: &Source) -> Option<u32>;
}

impl HasId<Source> for Source {
impl HasId for Source {
fn js_raw_id(&self) -> JsString {
Self::id_internal(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 @@ -76,7 +76,7 @@ impl Structure {
}
}

impl<T> HasId<T> for T
impl<T> HasId for T
where
T: AsRef<Structure>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/tombstone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl CanDecay for Tombstone {
}
}

impl HasId<Tombstone> for Tombstone {
impl HasId for Tombstone {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
Expand Down
32 changes: 21 additions & 11 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ pub trait HasCooldown {

pub trait Resolvable: From<JsValue> {}

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

/// Trait for all game objects which have an associated unique identifier.
#[enum_dispatch]
pub trait HasId<T> {
pub trait HasId: MaybeHasId {
/// 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<T> {
fn id(&self) -> ObjectId<Self>
where
Self: Sized,
{
self.raw_id().into()
}

Expand All @@ -74,7 +76,10 @@ pub trait HasId<T> {
/// 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<T> {
fn js_id(&self) -> JsObjectId<Self>
where
Self: Sized,
{
self.js_raw_id().into()
}

Expand All @@ -85,12 +90,14 @@ pub trait HasId<T> {

/// Trait for all game objects which may (or may not) have an associated unique
/// identifier.
#[enum_dispatch]
pub trait MaybeHasId<T> {
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_id(&self) -> Option<ObjectId<T>> {
fn try_id(&self) -> Option<ObjectId<Self>>
where
Self: Sized,
{
self.try_raw_id().map(Into::into)
}

Expand All @@ -105,7 +112,10 @@ pub trait MaybeHasId<T> {
/// 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<JsObjectId<T>> {
fn try_js_id(&self) -> Option<JsObjectId<Self>>
where
Self: Sized,
{
self.try_js_raw_id().map(Into::into)
}

Expand All @@ -115,9 +125,9 @@ pub trait MaybeHasId<T> {
fn try_js_raw_id(&self) -> Option<JsString>;
}

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

0 comments on commit ca6d864

Please sign in to comment.