necs
necs is a simple Entity Component System (ECS) written in Rust. It is designed primarily for learning purposes to understand how ECS architectures work under the hood.
It provides two distinct storage implementations:
- Default: A simple Struct of Arrays (SoA) storage where components are stored in sparse vectors indexed by Entity ID.
- Archetypes: An optional implementation where entities with the same set of components are grouped together in memory for better data locality.
Features
- Generational Entities: Entity IDs are recycled with a generation counter
- Type-Erased Storage: Components can be any
'statictype - Flexible Querying: Query all components of a specific type mutably or immutably
- Archetype-Based Storage (Optional): Group entities by component composition for cache efficiency
- Bundle API: Create entities with multiple components at once or add/remove sets of components atomically
- No Systems: There are no built-in "Systems" or schedulers. You simply query the
Worldand iterate over data to implement your logic
Installation
Add necs to your Cargo.toml.
[]
= "0.2.0" # Adjust path or git url as necessary
To enable the archetype-based storage:
[]
= { = "0.2.0", = ["archetypes"] }
Usage
Basic World
The default World allows you to spawn entities and attach components freely.
use ;
// 1. Define Components
Archetype World
If you enable the archetypes feature, you can use ArchetypedWorld. This moves entities into specific "Archetypes" (tables) based on their component signature. This makes adding/removing components slightly slower (due to moving memory), but iteration is cache-friendly.
use ;
Using Bundles
The archetypes feature also enables the Bundle API, allowing you to spawn entities with a predefined set of components or add multiple components in a single call. This is more efficient as it reduces the number of archetype migrations.
use ;
;
Advanced Querying
The archetypes feature enables tuple-based queries, allowing you to iterate over entities that have all specific components.
use ;
Disclaimer
necs is not intended to be a replacement for production-ready ECS frameworks like bevy_ecs, specs, or hecs. It lacks advanced features like:
- Parallel system execution.
- Complex query filters (e.g.,
With,Without, Joined queries). - Change detection.
It serves as a reference implementation for educational exploration.