screeps-game-api icon indicating copy to clipboard operation
screeps-game-api copied to clipboard

Implement Rust traits for Screeps types

Open ashkitten opened this issue 6 years ago • 3 comments

Things like Index, IndexMut, etc. should be implemented for better integration with Rust code

ashkitten avatar Mar 24 '18 16:03 ashkitten

In general, agreed!

Were there any specific Screeps types you were thinking Index/IndexMut would be appropriate for?

daboross avatar Mar 24 '18 17:03 daboross

I was specifically thinking the dict types (creeps, spawns, memory) should have Index/IndexMut, and probably also be iterable?

ashkitten avatar Mar 24 '18 18:03 ashkitten

Alright- that makes sense.

The current design of the Game.* dicts could be redone so they support implementing traits. Right now they're modules, but we could make them structs with no fields. It could then implement IntoIterator, which would work well, but I'm not so sure about Index.

The main problem I see with implementing Index for them is that Index::index needs to return some sort of reference borrowed from the structure. We don't currently store Creep / Structure / etc. instances in screeps-game-api, and we would need to in order to implement Index. I'm not sure about how to solve this unless we just went ahead and allowed fetching all values into a HashMap<String, Creep> which could then be indexed.


For memory, the main question I have is what type the index should result in. I went with methods for each specific type in order to have checked/typed access.

Do you think introducing a MemoryValue enum with different possible types would be good, or would Index return stdweb::Value?

Even here though, there's still the problem with references. Rust's Index trait seems designed for data structures which really hold their data, and don't fetch it from an external place.

daboross avatar Mar 24 '18 18:03 daboross