Module ecs_commands
Expand description
Bounded deferred ECS authoring, with buffer-local, epoch-local spawn tokens.
Queue while query guards exist; apply only after they drop. Spawns use
World::try_spawn_batch with one owned bundle, so the caller must explicitly
prepare entity and component sparse-index budgets. Insert uses checked
World::insert and may grow ordinary component storage. No world identity
is added: existing Entity handles and resolved tokens remain world-local.
Apply every partial flush in an epoch to the same world; replacing the world
requires EcsCommandBuffer::reset first.
Command capacity bounds pending operations; spawn capacity bounds all accepted spawns in an epoch, including failed and already claimed spawns. Slots are not recycled until reset. A token can target many later commands, but its spawn is executed once and its output mapping can be taken once. Taking the mapping does not break queued dependencies. Budgets, errors and panics preserve the unvisited FIFO tail. A failed/panicking spawn never supplies a dependency.
This adapter shares task 205’s CommandFailurePolicy, CommandFailure
and FallibleCommandApplyOutcome. Its small executor additionally borrows
the world and token map separately, retains typed rejected bundles and records
successes. The generic closure queue cannot do that directly without an owned
world or shared interior-mutability context. No arbitrary callback is queued.
Storage is bounded, not universally allocation-free: construction reserves
the queue, mapping and success log, and creates one reference-counted identity.
Accepted bundle commands allocate one box; remove/despawn commands may also
allocate. Rejection occurs before boxing. Apply/report/map bookkeeping does
not allocate; ECS insert/despawn, component destructors and callbacks may.
Bundles need only Bundle + 'static, with no Clone/Debug/Send requirement.
Reported spawn errors retain the original bundle. Dependency errors retain
insert bundles too. Once passed to World::insert, an invalid/duplicate bundle
is consumed by that API. Stale insert targets are rejected with the bundle
retained before that call. Removed components are dropped, not returned. No
operation, destructor, allocation or callback panic is caught or rolled back.
A panic can leave partial ECS mutations; inspect the world before continuing.
§Tick-boundary recipe
use ferravex_engine::{ecs::{Entity, World}, ecs_commands::EcsCommandBuffer};
use ferravex_engine::commands::CommandFailurePolicy;
struct Lifetime(u32);
let mut world = World::new();
world.prepare_entities(8).unwrap();
world.prepare_component::<Lifetime>(8).unwrap();
let old = world.spawn((Lifetime(0),));
let mut commands = EcsCommandBuffer::new(8, 4);
let token;
{
let query = world.query::<(Entity, &Lifetime)>();
for (entity, lifetime) in &query {
if lifetime.0 == 0 { commands.despawn(10, entity).unwrap(); }
}
// Author the next actor while shared world/query borrows still exist.
token = commands.spawn(11, (Lifetime(3),)).unwrap_or_else(|_| panic!("full"));
commands.insert(12, &token, (true,)).unwrap_or_else(|_| panic!("full"));
} // All ECS guards end before mutable apply.
let mut failures = 0;
let mut report = commands.apply_report(&mut world, 8, CommandFailurePolicy::Continue,
|_| failures += 1).unwrap();
assert_eq!(failures, 0);
assert_eq!(report.commands.applied, 3);
let actor = report.take_spawn(&token).unwrap();
// Emit feedback only for these accepted mutations, once per returned report.
let feedback: Vec<_> = report.accepted().iter().map(|event| event.id).collect();
assert_eq!(feedback, [10, 11, 12]);
assert!(!world.contains(old));
assert!(world.contains(actor));
assert!(*world.get::<&bool>(actor).unwrap());Rust prevents applying while a query is still used:
use ferravex_engine::{ecs::World, ecs_commands::EcsCommandBuffer};
use ferravex_engine::commands::CommandFailurePolicy;
let mut world = World::new();
let mut commands = EcsCommandBuffer::new(1, 1);
let query = world.query::<&u32>();
commands.apply_report(&mut world, 1, CommandFailurePolicy::Continue, |_| {}).unwrap();
assert_eq!(query.iter().count(), 0);Tokens cannot be constructed from a guessed slot or entity:
use ferravex_engine::ecs_commands::SpawnToken;
let token = SpawnToken { epoch: 0, slot: 7 };Structs§
- Applied
Entity Command - Accepted mutation for host feedback/correlation. IDs are caller labels and may repeat, including zero; the buffer does not deduplicate host events.
- EcsApply
Report - A flush’s bounded accepted-event slice and the epoch’s one-shot spawn map. Dropping the report preserves unclaimed mappings and queued dependencies. A later apply replaces the accepted-event log, including on a zero budget.
- EcsCommand
Buffer - Typed, bounded structural changes for one ECS world at a tick boundary. See the module recipe for preparation, query lifetimes and accepted feedback.
- EcsCommand
Error - Owned failure. Use
Self::into_bundleto recover a retained typed bundle. A wrong requested type returns the entire error unchanged. - Rejected
Bundle - A rejected owned bundle, returned without cloning, boxing or dropping it.
- Spawn
Token - An opaque reference to one accepted spawn. Clone retains identity, not a spawn. No serializable/raw constructor is supplied; this is not a world entity.
Enums§
- Buffer
Error - Reset cannot wrap its epoch. A panicking reset disables apply/authoring until another reset finishes; no remaining old-epoch command can then be executed.
- EcsCommand
Error Kind - Domain failure from one consumed command. No automatic retry or rollback.
- Enqueue
Error - Authoring rejection. Rejected operations never enter the queue.
- Entity
Command Kind - Kind of successfully completed mutation.
- Entity
Target - Existing world-local identity or an earlier spawn in this buffer epoch.
- Token
Error - Token provenance, dependency or one-shot mapping failure.