Skip to content

Commit

Permalink
Simplification of ID traits down to HasId and MaybeHasId
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden committed Dec 30, 2023
1 parent 5ca1ac7 commit 26e5820
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 119 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
===================

Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/construction_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ impl ConstructionSite {
}
}

impl MaybeHasNativeId for ConstructionSite {
impl MaybeHasId<ConstructionSite> 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<JsString> {
fn try_js_raw_id(&self) -> Option<JsString> {
self.id_internal()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ impl HasHits for Creep {
}
}

impl MaybeHasNativeId for Creep {
fn try_native_id(&self) -> Option<JsString> {
impl MaybeHasId<Creep> for Creep {
fn try_js_raw_id(&self) -> Option<JsString> {
self.id_internal()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl HasCooldown for Deposit {
}
}

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

impl HasNativeId for Mineral {
fn native_id(&self) -> JsString {
impl HasId<Mineral> for Mineral {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/nuke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Nuke> for Nuke {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/power_creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ impl HasHits for PowerCreep {
}
}

impl HasNativeId for PowerCreep {
fn native_id(&self) -> JsString {
impl HasId<PowerCreep> for PowerCreep {
fn js_raw_id(&self) -> JsString {
self.id_internal()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/reactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ extern "C" {
pub fn owner(this: &Reactor) -> Option<Owner>;
}

impl HasNativeId for Reactor {
fn native_id(&self) -> JsString {
impl HasId<Reactor> for Reactor {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern "C" {
pub fn resource_type(this: &Resource) -> ResourceType;
}

impl HasNativeId for Resource {
fn native_id(&self) -> JsString {
impl HasId<Resource> for Resource {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/ruin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ impl CanDecay for Ruin {
}
}

impl HasNativeId for Ruin {
fn native_id(&self) -> JsString {
impl HasId<Ruin> for Ruin {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/score_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ extern "C" {
pub fn store(this: &ScoreCollector) -> Store;
}

impl HasNativeId for ScoreCollector {
fn native_id(&self) -> JsString {
impl HasId<ScoreCollector> for ScoreCollector {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/score_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl CanDecay for ScoreContainer {
}
}

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

impl HasNativeId for Source {
fn native_id(&self) -> JsString {
impl HasId<Source> for Source {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ impl Structure {
}
}

impl<T> HasNativeId for T
impl<T> HasId<T> for T
where
T: AsRef<Structure>,
{
fn native_id(&self) -> JsString {
fn js_raw_id(&self) -> JsString {
Structure::id_internal(self.as_ref())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/symbol_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl CanDecay for SymbolContainer {
}
}

impl HasNativeId for SymbolContainer {
fn native_id(&self) -> JsString {
impl HasId<SymbolContainer> for SymbolContainer {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/impls/symbol_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ extern "C" {
pub fn score_multiplier(this: &SymbolDecoder) -> u32;
}

impl HasNativeId for SymbolDecoder {
fn native_id(&self) -> JsString {
impl HasId<SymbolDecoder> for SymbolDecoder {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
4 changes: 2 additions & 2 deletions src/objects/impls/tombstone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl CanDecay for Tombstone {
}
}

impl HasNativeId for Tombstone {
fn native_id(&self) -> JsString {
impl HasId<Tombstone> for Tombstone {
fn js_raw_id(&self) -> JsString {
Self::id_internal(self)
}
}
Expand Down
130 changes: 43 additions & 87 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsString>;
}

impl<T> MaybeHasNativeId for T
where
T: HasNativeId,
{
fn try_native_id(&self) -> Option<JsString> {
Some(<Self as HasNativeId>::native_id(self))
}
}

pub trait Resolvable: From<JsValue> {}

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

#[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<T> 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<T> Resolvable for T where T: MaybeHasId<T> + From<JsValue> {}

/// Trait for all game objects which have an associated unique identifier.
#[enum_dispatch]
pub trait HasTypedId<T> {
/// 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<T>;

fn js_id(&self) -> JsObjectId<T>;
}

impl<T> HasTypedId<T> for T
where
T: HasId + HasNativeId,
{
pub trait HasId<T> {
/// 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> {
self.raw_id().into()
}

fn js_id(&self) -> JsObjectId<T> {
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<T> HasTypedId<T> for &T
where
T: HasId + HasNativeId,
{
fn id(&self) -> ObjectId<T> {
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<T> {
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<T> {
/// 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<RawObjectId>;
}
/// object doesn't currently have an ID.
fn try_id(&self) -> Option<ObjectId<T>> {
self.try_raw_id().map(Into::into)
}

impl<T> 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<RawObjectId> {
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<T> {
/// 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>>;
}

impl<T> MaybeHasTypedId<T> for T
where
T: MaybeHasId,
{
fn try_id(&self) -> Option<ObjectId<T>> {
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<JsObjectId<T>> {
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<JsString>;
}

impl<T> MaybeHasTypedId<T> for &T
impl<T> MaybeHasId<T> for T
where
T: MaybeHasId,
T: HasId<T>,
{
fn try_id(&self) -> Option<ObjectId<T>> {
self.try_raw_id().map(Into::into)
fn try_js_raw_id(&self) -> Option<JsString> {
Some(self.js_raw_id())
}
}

Expand Down

0 comments on commit 26e5820

Please sign in to comment.