Crate ecs

Source
Expand description

Original, dependency-free entity/component storage for small Ferravex games.

Components live in one sparse, contiguous slot array per Rust type. Entity generations prevent stale handles from reaching reused slots. Iteration visits live entity slots in stable order. Runtime component borrowing uses standard library Ref / RefMut guards, so overlapping queries cannot create aliased mutable references, even if an iterator is retained or abandoned.

use ferravex_ecs::{Entity, World};
let mut world = World::new();
let player = world.spawn((10_i32, 2.0_f32));
for (entity, mut health, speed) in &mut world.query::<(Entity, &mut i32, &f32)>() {
    assert_eq!(entity, player);
    *health += *speed as i32;
}
assert_eq!(*world.get::<&i32>(player).unwrap(), 12);
world.despawn(player).unwrap();
assert!(!world.contains(player));

Query results are borrow guards rather than naked references. Bind mutable results with mut, as above. try_query and try_iter report conflicts as errors; query and iter panic on invalid/overlapping borrows. Structural operations require &mut World, so Rust prevents them while guards exist.

ⓘ
use ferravex_ecs::World;
let mut world = World::new();
let entity = world.spawn((42_i32,));
let component = world.get::<&i32>(entity).unwrap();
world.despawn(entity).unwrap(); // A live guard borrows the world.
println!("{}", *component);

Structs§

Entity
Opaque slot identity. A despawned entity never becomes valid again in its world. Handles are world-local; two separate worlds may issue the same identity.
QueryBorrow
QueryIter
Ref
Wraps a borrowed reference to a value in a RefCell box. A wrapper type for an immutably borrowed value from a RefCell<T>.
RefMut
A wrapper type for a mutably borrowed value from a RefCell<T>.
TryQueryIter
World
Sparse typed storage with generational entities and checked component borrows.

Enums§

Error

Traits§

Access
A single component access (&T or &mut T), or the entity identity itself.
Bundle
Tuple of zero to four owned component values accepted by spawn and insert. Implementations are sealed so validation and insertion cannot disagree.
Query
Query signature: one access type or a tuple containing up to four access types.