Struct CommandBuffer
pub struct CommandBuffer<W> { /* private fields */ }Expand description
A bounded FIFO queue of opaque deferred mutations.
Queue storage grows lazily and is retained when commands are removed. Bounds apply to the number of commands, not their payload sizes or execution time.
Implementations§
§impl<W> CommandBuffer<W>
impl<W> CommandBuffer<W>
pub fn new(capacity: usize) -> CommandBuffer<W>
pub fn new(capacity: usize) -> CommandBuffer<W>
Creates an empty queue. Zero capacity is valid and rejects every push.
pub fn push<F>(&mut self, command: F) -> Result<(), F>
pub fn push<F>(&mut self, command: F) -> Result<(), F>
Queues a command without executing it, or returns it unchanged if full.
Rejection neither boxes nor drops the supplied closure. A returned closure may be retried after capacity becomes available; it then joins the tail.
pub fn apply(&mut self, world: &mut W, limit: usize) -> usize
pub fn apply(&mut self, world: &mut W, limit: usize) -> usize
Applies at most limit commands in insertion order, returning the number
that completed. A zero limit does nothing; later commands remain queued.
Has the same panic and partial-application semantics as Self::apply_report.
pub fn apply_report(
&mut self,
world: &mut W,
limit: usize,
) -> CommandApplyOutcome
pub fn apply_report( &mut self, world: &mut W, limit: usize, ) -> CommandApplyOutcome
Applies at most limit oldest commands and reports completed and remaining
work without allocating additional storage. A zero limit does nothing.
§Panics
A command panic propagates; this method returns no outcome in that case.
The panicking command has already been removed and increments started,
but not applied. All later commands remain queued in order, even within
this call’s budget. Earlier changes and the panicking command’s changes
remain in world. Catching a panic does not establish that world is valid
for further processing; the host owns that decision.
use ferravex_engine::commands::CommandBuffer;
use std::panic::{catch_unwind, AssertUnwindSafe};
let mut commands = CommandBuffer::new(3);
assert!(commands.push(|world: &mut Vec<u32>| world.push(1)).is_ok());
assert!(commands.push(|world: &mut Vec<u32>| {
world.push(2);
panic!("host command failed");
}).is_ok());
assert!(commands.push(|world: &mut Vec<u32>| world.push(3)).is_ok());
let mut world = Vec::new();
let result = catch_unwind(AssertUnwindSafe(|| commands.apply_report(&mut world, 3)));
assert!(result.is_err());
assert_eq!(world, [1, 2]);
assert_eq!(commands.len(), 1);
assert_eq!((commands.metrics().started, commands.metrics().applied), (2, 1));
commands.clear(); // The host chooses to discard the unexecuted tail.
assert_eq!(commands.metrics().discarded, 1);pub fn drain_up_to(&mut self, limit: usize) -> CommandDrain<'_, W> ⓘ
pub fn drain_up_to(&mut self, limit: usize) -> CommandDrain<'_, W> ⓘ
Lazily transfers up to limit oldest commands without invoking them or
allocating additional storage. A zero limit yields nothing.
Each iterator step removes one command. Dropping the iterator leaves all
unyielded commands queued in their original order, even within its budget.
Use CommandDrain::outcome before dropping it to inspect progress.
use ferravex_engine::commands::{Command, CommandBuffer, CommandDrainOutcome};
let mut commands = CommandBuffer::new(3);
for value in 1..=3 {
assert!(commands.push(move |world: &mut Vec<u32>| world.push(value)).is_ok());
}
let mut drain = commands.drain_up_to(2);
assert_eq!(drain.len(), 2); // Available within the budget, not total queued.
let first: Command<Vec<u32>> = drain.next().unwrap();
assert_eq!(drain.outcome(), CommandDrainOutcome { drained: 1, remaining: 2 });
drop(drain); // Both unyielded commands remain queued.
let mut world = Vec::new();
first(&mut world);
assert_eq!(commands.metrics().drained, 1);
assert_eq!(commands.metrics().applied, 0); // External execution is not tracked.
assert_eq!(commands.apply(&mut world, usize::MAX), 2);
assert_eq!(world, [1, 2, 3]);pub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Maximum queued command count, independent of backing storage capacity.
pub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Number of additional commands that fit before pushes are rejected.
pub fn metrics(&self) -> CommandBufferMetrics
pub fn metrics(&self) -> CommandBufferMetrics
Copies metrics without consuming, invoking or inspecting opaque closures.
pub fn reset_metrics(&mut self) -> CommandBufferMetrics
pub fn reset_metrics(&mut self) -> CommandBufferMetrics
Returns the previous metrics and starts a new observation interval. Queued commands remain intact; the high-water mark starts at their count.
pub fn clear(&mut self)
pub fn clear(&mut self)
Discards all commands without invoking them, retaining storage and metrics.
Each command is removed and counted before dropping its payload. If a
payload destructor panics, that panic propagates and later commands remain
queued. Use Self::drain_up_to to bound the number of commands discarded.