Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for PartialEq and Eq for objects with an id #18

Merged
merged 8 commits into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions screeps-game-api/src/objects/impls/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl Creep {
js_unwrap!(@{self.as_ref()}.carry[RESOURCE_ENERGY])
}

pub fn owner_name(&self) -> String {
js_unwrap!(@{self.as_ref()}.owner.username)
}

pub fn cancel_order(&self, name: &str) -> ReturnCode {
js_unwrap!(@{self.as_ref()}.cancelOrder(@{name}))
}
Expand Down Expand Up @@ -148,6 +152,7 @@ macro_rules! creep_simple_concrete_action {

simple_accessors! {
Creep;
(id -> id -> String),
(carry_capacity -> carryCapacity -> i32),
(fatigue -> fatigue -> i32),
(hits -> hits -> i32),
Expand All @@ -158,6 +163,14 @@ simple_accessors! {
(ticks_to_live -> ticksToLive -> i32),
}

impl PartialEq for Creep {
fn eq(&self, other: &Creep) -> bool{
self.id() == other.id()
}
}

impl Eq for Creep {}

creep_simple_generic_action! {
(attack(Attackable) -> attack),
(dismantle(StructureProperties) -> dismantle),
Expand Down
9 changes: 9 additions & 0 deletions screeps-game-api/src/objects/impls/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ impl Resource {
simple_accessors! {
Resource;
(amount -> amount -> i32),
(id -> id -> String)
}

impl PartialEq for Resource {
fn eq(&self, other: &Resource) -> bool{
self.id() == other.id()
}
}

impl Eq for Resource {}
8 changes: 8 additions & 0 deletions screeps-game-api/src/objects/impls/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,11 @@ impl Room {
js_unwrap!(@{self.as_ref()}.name)
}
}

impl PartialEq for Room {
fn eq(&self, other: &Room) -> bool{
self.name() == other.name()
}
}

impl Eq for Room {}
8 changes: 8 additions & 0 deletions screeps-game-api/src/objects/impls/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ simple_accessors! {
(id -> id -> String),
(ticks_to_regeneration -> ticksToRegeneration -> u32),
}

impl PartialEq for Source {
fn eq(&self, other: &Source) -> bool{
self.id() == other.id()
}
}

impl Eq for Source {}
9 changes: 9 additions & 0 deletions screeps-game-api/src/objects/impls/tombstone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ simple_accessors! {
(creep -> creep -> Creep),
(death_time -> deathTime -> u32),
(ticks_to_decay -> ticksToDecay -> u32),
(id -> id -> String)
}

impl PartialEq for Tombstone {
fn eq(&self, other: &Tombstone) -> bool{
self.id() == other.id()
}
}

impl Eq for Tombstone {}
30 changes: 30 additions & 0 deletions screeps-game-api/src/objects/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,33 @@ macro_rules! impl_room_object_properties {
)*
);
}


// Macro for mass implementing `StructureProperties`, `PartialEq` and `Eq` for a type.
Copy link
Collaborator

@daboross daboross Sep 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be a doc comment with //! ///, but it's not a big deal.

Edit: I'd completely forgotten the difference between //! and /// while reviewing these prs.

The main reason I like doc comments for this sort of thing is that it syntactically attaches the comment to the macro. Even if it's just internal comments for an internal macro, it makes it even clearer that this comment is for the thing under it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the // for ///, but I'll wait until #19 is merged before moving this one with the other.

//
// This macro accepts a comma-separated list of types on which to implement the unsafe `StructureProperties` trait on
// a screeps object.
// From that implementation, the type gets the `id` method which is used to implement `PartialEq` and `Eq`.
//
// # Example
// ```
// macro_rules!(OwnedStructure, Structure, StructureContainer)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, I think this line was meant to be something else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copy/paste monster is real!

// ```
//
// # Safety
// The macro assumes that it is implementing the trait to a valid `Reference`
// (See `reference_wrapper` macro) which will support all `StructureProperties` methods.
//
macro_rules! impl_structure_properties {
( $( $struct_name:ty ),+ ) => {$(
unsafe impl StructureProperties for $struct_name {}

impl PartialEq for $struct_name {
fn eq(&self, other: &$struct_name) -> bool{
self.id() == other.id()
}
}

impl Eq for $struct_name {}
)*};
}
44 changes: 23 additions & 21 deletions screeps-game-api/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,27 +325,29 @@ impl_room_object_properties! {
Tombstone,
}

unsafe impl StructureProperties for OwnedStructure {}
unsafe impl StructureProperties for Structure {}
unsafe impl StructureProperties for StructureContainer {}
unsafe impl StructureProperties for StructureController {}
unsafe impl StructureProperties for StructureExtension {}
unsafe impl StructureProperties for StructureExtractor {}
unsafe impl StructureProperties for StructureKeeperLair {}
unsafe impl StructureProperties for StructureLab {}
unsafe impl StructureProperties for StructureLink {}
unsafe impl StructureProperties for StructureNuker {}
unsafe impl StructureProperties for StructureObserver {}
unsafe impl StructureProperties for StructurePowerBank {}
unsafe impl StructureProperties for StructurePowerSpawn {}
unsafe impl StructureProperties for StructurePortal {}
unsafe impl StructureProperties for StructureRampart {}
unsafe impl StructureProperties for StructureRoad {}
unsafe impl StructureProperties for StructureSpawn {}
unsafe impl StructureProperties for StructureStorage {}
unsafe impl StructureProperties for StructureTerminal {}
unsafe impl StructureProperties for StructureTower {}
unsafe impl StructureProperties for StructureWall {}
impl_structure_properties!{
OwnedStructure,
Structure,
StructureContainer,
StructureController,
StructureExtension,
StructureExtractor,
StructureKeeperLair,
StructureLab,
StructureLink,
StructureNuker,
StructureObserver,
StructurePowerBank,
StructurePowerSpawn,
StructurePortal,
StructureRampart,
StructureRoad,
StructureSpawn,
StructureStorage,
StructureTerminal,
StructureTower,
StructureWall
}

unsafe impl OwnedStructureProperties for OwnedStructure {}
unsafe impl OwnedStructureProperties for StructureController {}
Expand Down