entity-trait-system-1.0.1 has been yanked.
entity-trait-system
An alternative to ECS. Here is a video summary.
Setup
Requires the nightly compiler and specialization feature - it's only used in a sound way.
// explicitly opt in to language feature
// declare world, entities, and traits which the entities could have
world!;
let mut world = default;
// directly access arena member
let player_id = world.player.insert;
// compile time type accessor of arena member (similar)
world..insert;
// visit all arenas with types that implement trait (likely static dispatch)
world.par_visit_test_trait;
world.visit_test_trait;
// runtime type API - access type-erased (Any) arena
let arena_id = ;
let player_arena = world.any_arena_mut;
// unwrap: I know that this is a player and that the reference is valid
let player = player_arena
.get_mut.unwrap
..unwrap;
player.do_something_else;
API Overview
Per-Trait Methods (generated for each trait)
visit_<trait>- Immutable iteration over implementing entitiesvisit_mut_<trait>- Mutable iteration over implementing entitiesvisit_key_<trait>- Immutable iteration with(Key, &Value)tuplesvisit_key_mut_<trait>- Mutable iteration with(Key, &mut Value)tuplesretain_<trait>- Keep entities matching predicateretain_with_default_<trait>- Keep with control over non-implementing typesdiff_<trait>- Gather diff vector from immutable view, to apply laterdiff_mut_<trait>- Same as previous, but from mutable viewdiff_apply_<trait>- Apply diff vectorclear_<trait>- Clear all arenas implementing traitlen_<trait>- Count entities implementing traitany_arenas_<trait>- Get array of type-erased arenas implementing traitany_arenas_mut_<trait>- Mutable version of above
World Methods
arena<T>()/arena_mut<T>()- Compile-time typed arena accessany_arena()/any_arena_mut()- Runtime type-erased accessarena_id<T>()- Get stable arena identifier - serializes to type name.clear()- Clear all arenaslen()- Total entity count across all arenas
Rayon Support
Parallel operations exposed via par_* variants.
Serde Support
Both map (serde_json) and seq (bincode) style ser/deserialization.
Performance Note
This can be found in the implementation of visit_*:
The handler is typically inlined and devirtualized to erase dynamic dispatch, since the type is known at compile time and is type erased just before use. This means that static dispatch is reliant on compiler optimization; likely but not guaranteed.
License: MIT