Struct SystemSchedule

pub struct SystemSchedule<C, E> { /* private fields */ }
Expand description

A reusable sequential runner with one SystemOrder as its dependency graph.

Names obey the graph’s 1..=128 UTF-8 byte limit and exact case-sensitive uniqueness. Its 256-entry bound includes unbound names and flushes. Closure captures are owned ('static), need not be Send, and persist across runs. The context is borrowed only during each call. See the module’s runnable recipe.

Implementations§

§

impl<C, E> SystemSchedule<C, E>

pub fn add(&mut self, name: &str) -> Result<(), OrderError>

Register an unbound system name. Self::prepare rejects it until bound.

use ferravex_engine::systems::{SchedulePrepareError, SystemControl, SystemSchedule};
let mut schedule = SystemSchedule::<u32, ()>::default();
schedule.add("count").unwrap();
assert_eq!(schedule.prepare(), Err(SchedulePrepareError::MissingCallable {
    name: "count".into(),
}));
schedule.set_callback("count", |count| { *count += 1; Ok(SystemControl::Continue) }).unwrap();
schedule.prepare().unwrap();
let mut count = 0;
assert_eq!(schedule.run(&mut count).unwrap().invoked, 1);
assert_eq!(count, 1);

pub fn register<F>(&mut self, name: &str, callback: F) -> Result<(), OrderError>
where F: FnMut(&mut C) -> Result<SystemControl, E> + 'static,

Register a new enabled system and its callback. Rejected closures are dropped. See the module recipe; use Self::set_callback to replace a callable.

pub fn register_flush<F>( &mut self, name: &str, callback: F, ) -> Result<(), OrderError>
where F: FnMut(&mut C) -> Result<SystemControl, E> + 'static,

Register an explicit, named flush boundary, counting toward the same limit.

The callback owns what to flush and any work budget; the runner has no queue. Declare edges on both sides as in the module recipe. Merely naming a flush does not order all producers/consumers around it. Stop/error before the flush leaves pending work to the caller; a flush itself may also stop or fail.

pub fn set_callback<F>( &mut self, name: &str, callback: F, ) -> Result<(), OrderError>
where F: FnMut(&mut C) -> Result<SystemControl, E> + 'static,

Bind or replace a callable without changing its kind, predicate or enablement. The old/rejected closure is dropped. See Self::add for an example.

pub fn set_run_if<F>( &mut self, name: &str, predicate: F, ) -> Result<(), OrderError>
where F: FnMut(&C) -> bool + 'static,

Opt into a predicate, evaluated once per visited, enabled entry on current context. It may retain private state; it is not evaluated during preparation.

use ferravex_engine::systems::{SystemControl, SystemSchedule};
let mut schedule = SystemSchedule::<u32, ()>::default();
schedule.register("count", |n| { *n += 1; Ok(SystemControl::Continue) }).unwrap();
schedule.set_run_if("count", |n| *n < 1).unwrap();
schedule.prepare().unwrap();
let mut n = 0;
assert_eq!(schedule.run(&mut n).unwrap().invoked, 1);
assert_eq!(schedule.run(&mut n).unwrap().skipped, 1);
schedule.clear_run_if("count").unwrap();
schedule.set_enabled("count", false).unwrap();
assert!(!schedule.is_prepared());
schedule.prepare().unwrap();
assert_eq!(schedule.run(&mut n).unwrap().skipped, 1);
schedule.set_enabled("count", true).unwrap();
schedule.prepare().unwrap();
assert_eq!(schedule.run(&mut n).unwrap().invoked, 1);
assert_eq!(n, 2);

pub fn clear_run_if(&mut self, name: &str) -> Result<(), OrderError>

Remove a predicate, leaving enablement unchanged. See Self::set_run_if.

pub fn set_enabled( &mut self, name: &str, enabled: bool, ) -> Result<(), OrderError>

Set explicit enablement. A disabled flush is skipped too; no queue is applied. Disabling does not relax missing-callable validation. See Self::set_run_if.

pub fn before(&mut self, first: &str, second: &str) -> Result<(), OrderError>

Declare an edge, deferring cycle rejection to preparation as SystemOrder::before does. Unknown names are rejected without invalidating the prepared sequence.

use ferravex_engine::systems::{SchedulePrepareError, SystemControl, SystemSchedule};
let mut schedule = SystemSchedule::<(), ()>::default();
schedule.register("loop", |_| Ok(SystemControl::Continue)).unwrap();
schedule.before("loop", "loop").unwrap();
assert!(matches!(schedule.prepare(), Err(SchedulePrepareError::Order(_))));
assert!(!schedule.is_prepared());

pub fn before_checked( &mut self, first: &str, second: &str, ) -> Result<(), OrderDiagnostic>

Declare an edge transactionally through SystemOrder::before_checked. A rejected cycle/missing endpoint preserves preparation. See the module recipe.

pub fn order(&self) -> &SystemOrder

Read the single underlying graph for inspection. No mutable graph escapes.

pub fn len(&self) -> usize

Number of registered names, including flushes and unbound systems.

pub fn is_empty(&self) -> bool

Whether there are no registered names.

pub fn is_prepared(&self) -> bool

Whether the current configuration has a complete, validated sequence.

pub fn prepared_names(&self) -> Option<impl ExactSizeIterator>

Inspect the exact cached sequence without allocation; None until prepared. All entries, even disabled ones, are included. See the module recipe.

pub fn prepare(&mut self) -> Result<(), SchedulePrepareError>

Resolve once and require a callable for every name, including disabled ones.

Uses SystemOrder::resolve_checked, which delegates to the existing sequential SystemOrder::resolve. Graph errors take precedence over the first missing callable in that order. On failure no partial sequence is usable. Repeated preparation without edits is an allocation-free no-op. No callbacks or predicates run here. See Self::add and Self::before.

pub fn run( &mut self, context: &mut C, ) -> Result<ScheduleReport, ScheduleRunError<'_, E>>

Invoke the prepared sequence with no runner allocation, stopping at Stop/Err.

Never prepares implicitly. A run error borrows the failing name and retains the original E plus prefix counts. Successful runs return the same counts; control distinguishes completion from early Stop. No hidden cleanup or final flush runs. A subsequent explicit invocation starts at the beginning.

use ferravex_engine::systems::{ScheduleRunError, SystemControl, SystemSchedule};
let mut schedule = SystemSchedule::<u32, &'static str>::default();
schedule.register("fail", |n| { *n += 1; Err("partial mutation") }).unwrap();
schedule.register_flush("tail", |_| panic!("must not flush")).unwrap();
let mut n = 0;
assert_eq!(schedule.run(&mut n), Err(ScheduleRunError::NotPrepared));
schedule.prepare().unwrap();
let error = schedule.run(&mut n).unwrap_err();
match error {
    ScheduleRunError::Callback { name, error, report, .. } => {
        assert_eq!((name, error), ("fail", "partial mutation"));
        assert_eq!((report.scheduled, report.invoked, report.flushes), (2, 1, 0));
        assert_eq!(report.control, SystemControl::Stop);
    }
    ScheduleRunError::NotPrepared => unreachable!(),
}
assert_eq!(n, 1);

pub fn clear(&mut self)

Reset to an empty, unprepared schedule; drop closures without calling them. Caller-owned context/queues are unaffected. Configuration storage is released. The empty state is installed before dropping captured values (which may panic).

use ferravex_engine::systems::{SystemControl, SystemSchedule};
let mut schedule = SystemSchedule::<(), ()>::default();
schedule.register("old", |_| Ok(SystemControl::Stop)).unwrap();
schedule.prepare().unwrap();
schedule.clear();
assert!(schedule.is_empty());
assert_eq!(schedule.len(), 0);
assert!(schedule.order().resolve().unwrap().is_empty());
assert!(schedule.prepared_names().is_none());
schedule.prepare().unwrap();
assert_eq!(schedule.run(&mut ()).unwrap().control, SystemControl::Continue);

Trait Implementations§

§

impl<C, E> Default for SystemSchedule<C, E>

§

fn default() -> SystemSchedule<C, E>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C, E> Freeze for SystemSchedule<C, E>

§

impl<C, E> !RefUnwindSafe for SystemSchedule<C, E>

§

impl<C, E> !Send for SystemSchedule<C, E>

§

impl<C, E> !Sync for SystemSchedule<C, E>

§

impl<C, E> Unpin for SystemSchedule<C, E>

§

impl<C, E> !UnwindSafe for SystemSchedule<C, E>

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.