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>

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>
where F: FnOnce(&mut W) + 'static,

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

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

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> ⓘ

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 len(&self) -> usize

Number of commands awaiting application or draining.

pub fn is_empty(&self) -> bool

Whether no commands are queued.

pub fn capacity(&self) -> usize

Maximum queued command count, independent of backing storage capacity.

pub fn remaining_capacity(&self) -> usize

Number of additional commands that fit before pushes are rejected.

pub fn is_full(&self) -> bool

Whether pushes will be rejected. True for a zero-capacity queue.

pub fn metrics(&self) -> CommandBufferMetrics

Copies metrics without consuming, invoking or inspecting opaque closures.

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)

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.

Auto Trait Implementations§

§

impl<W> Freeze for CommandBuffer<W>

§

impl<W> !RefUnwindSafe for CommandBuffer<W>

§

impl<W> !Send for CommandBuffer<W>

§

impl<W> !Sync for CommandBuffer<W>

§

impl<W> Unpin for CommandBuffer<W>

§

impl<W> !UnwindSafe for CommandBuffer<W>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.