tiny_ecs 0.9.4

A tiny ECS that tries to avoid unnecessary copy/clones

Tiny ECS

The intention of this crate is that a basic ECS is provided, where you will be required to exercise a little additional control. This is somewhat due to some limitations, and also due to trying to maintain as little overhead as possible - this means no unneccesary copies/clones, and in some use cases you will need to use downcast_ref or downcast_mut.

The basis of this ECS is the use of bitmasks. Each entity ID is in practice an internal index number in to an array which contains bitmasks. The bitmasks themselves keep track of what components the entity has. For the most part, bitmasks are handled for you, and some helper methods are available to hide their use, but there are also methods to get the bitmask for any ID if you are inclined to do some manual management.


Init with a capacity.

This is good to do if you know the size required as it will prevent many reallocs/moves as data is added. This affects both the entity and partmaps allocs (they will be equal in size).

use tiny_ecs::Entities;

let mut entities = Entities::new(Some(1000), Some(24));

Demonstrating use

use tiny_ecs::{Entities, PartMap};
use std::any::Any;
use std::collections::HashMap;
use std::cell::{Ref, RefMut};

// These are the "components" we will use
struct Vector1 { x: i32 }
struct Vector2 { x: i32, y: i32 }
struct Vector3 { x: i32, y: i32, z: i32 }

// Initialize the Entity collection
let mut entities = Entities::new(Some(3), Some(3));

// To create an entity you only need to add the first part using
// a free slot
let entity_1 = entities.get_free_slot().unwrap();
Vector1 { x: 42 }).is_ok());
// And you can add more parts to it
// The entity is only considered newly created if no parts existed before
Vector3 { x: 3, y: 10, z: -12 }).is_ok());

// To add another entity you need another free slot
let entity_2 = entities.get_free_slot().unwrap();
Vector2 { x: 66, y: 6 }).is_ok());
Vector1 { x: 6 }).is_ok());

// To get access to a part belonging to an entity you need
// first to get the partmap created for the part type
// This requires the part type signature
if entities.entity_contains::<Vector3>(entity_1) {
// The return type is Result<Option<T>>
let mut partmap = entities.get_pmap_mut::<Vector3>()
.expect("Mutable borrow failed")
.expect("No part found for entity");
// You can then use the part by getting a reference
let mut part: &Vector3 = partmap.get_part_mut(entity_1).unwrap();
assert_eq!(part.z, -12);

// You can check if an entity contains a part with the type signature
if entities.entity_contains::<Vector1>(entity_1) {
assert_eq!(entities.entity_contains::<Vector1>(entity_1), false);

// Make a system of some form that takes a `RefMut<PartMap>`
fn some_system(mut partmap: RefMut<PartMap>) {
// You can then iterate over the parts directly
for (k, v) in partmap.iter_mut() {
let part = v.downcast_mut::<Vector1>().unwrap();
part.x += 1;
assert!(part.x > *k as i32);
.expect("Mutable borrow failed")
.expect("No part found for entity"));

// Or create a system that fetches the parts for only the entities
// you are interested in.
fn second_system(active: &[u32], mut v1_map: RefMut<PartMap>) {
for id in active {
if let Ok(part) = v1_map.get_part_mut::<Vector1>(*id) {
part.x = 42;
second_system(&[0, 1, 2], entities.get_pmap_mut::<Vector1>()
.expect("Mutable borrow failed")
.expect("No part found for entity"));

// Or a system handles the `Entities` container directly
fn other_system(active_ents: &[u32], entities: &mut Entities) {
// You can mutably borrow multiple part types at once
let mut v1_partmap = entities.get_pmap_mut::<Vector1>()
.expect("Mutable borrow failed")
.expect("No part found for entity");
let mut v2_partmap = entities.get_pmap_mut::<Vector2>()
.expect("Mutable borrow failed")
.expect("No part found for entity");
// But not have a mutable borrow and immutable borrow to the same
// Fails at runtime!
// let v2_partmap = entities.get_pmap_ref::<Vector2>().unwrap().unwrap();
// But you can have multiple immutable references to the same part
for id in active_ents {
if entities.entity_contains::<Vector1>(*id) &&
entities.entity_contains::<Vector2>(*id) {
let v1_part: &mut Vector1 = v1_partmap.get_part_mut(*id).unwrap();
let v2_part = v2_partmap.get_part_mut::<Vector2>(*id).unwrap();
v1_part.x = 42;
assert_ne!(v1_part.x, 43);
assert_eq!(v1_part.x, 42);
other_system(&[0, 1, 2], &mut entities);