Ralith / hecs

A handy ECS
Apache License 2.0
967 stars 82 forks source link

Add `new` to `Entity` for tests #250

Closed sanbox-irl closed 2 years ago

sanbox-irl commented 2 years ago

Heya,

While testing code that interacts with hecs, I occasionally have code that references entities which would be provided, but which I need to make during a test. I can do the bit shift and use from_bits myself, but it's obviously a lot simply to just expose a new

Ralith commented 2 years ago

Can you elaborate a bit on the use case? Does the particular identity of the Entity matter? If not, maybe something like Entity::dangling() would be most convenient.

sanbox-irl commented 2 years ago

Exactly -- currently I use a grid which is a vec of Vec<TacitcsCell>. TacitcsCell looks like this:

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TacticsCell {
    Empty,
    Unit(Entity),
    Tile,
}

in some unit tests, where I need to generate an Entity, I do this:

TacticsCell::Unit(Entity::from_bits(123456789875).unwrap())

I don't care about that value, I just know it's big enough to satisfy the bit pattern. So changing it to Entity::dangling would be excellent (there isn't even a World that the entity could be a part of)