pub struct Dispatcher<const N: usize, const E: usize, I: Index = usize> { /* private fields */ }Expand description
Fixed-capacity online dispatcher for up to N time points and E adjacency entries,
storing internal handles as I (see Index).
§Examples
use temporal_control::{DispatchEdge, Dispatcher, PointKind, Ticks, TimePoint};
// 5 <= t_1 - t_0 <= 15
let points = [PointKind::Executable; 2];
let edges = [
DispatchEdge { from: TimePoint(0), to: TimePoint(1), weight: Ticks(15) },
DispatchEdge { from: TimePoint(1), to: TimePoint(0), weight: Ticks(-5) },
];
let mut d = Dispatcher::<2, 4>::new(&points, &edges, &[]).expect("fits");
let mut out = [TimePoint(0); 2];
assert_eq!(d.step(Ticks(0), &[], &mut out), Ok(1)); // t_0 = 0
assert_eq!(d.step(Ticks(4), &[], &mut out), Ok(0)); // too early for t_1
assert_eq!(d.step(Ticks(5), &[], &mut out), Ok(1)); // t_1 = 5
assert!(d.is_complete());Implementations§
Source§impl<const N: usize, const E: usize, I: Index> Dispatcher<N, E, I>
impl<const N: usize, const E: usize, I: Index> Dispatcher<N, E, I>
Sourcepub const EMPTY: Self
pub const EMPTY: Self
A dispatcher with no network loaded, usable in const and static initialisers.
Flight software typically places the dispatcher in statically allocated memory once
and then calls Self::load for every new plan, so the (possibly large) structure is
never built on the stack and returned by value. With no network loaded the dispatcher
has no points, Self::is_complete is true and every point handle is unknown.
§Examples
use temporal_control::{DispatchEdge, Dispatcher, PointKind, Ticks, TimePoint};
// Built at compile time; a flight build would put this behind its own cell type.
static IDLE: Dispatcher<8, 32> = Dispatcher::EMPTY;
assert!(IDLE.is_complete());
let mut d = Dispatcher::<8, 32>::EMPTY;
let edges = [DispatchEdge { from: TimePoint(0), to: TimePoint(1), weight: Ticks(10) }];
d.load(&[PointKind::Executable; 2], &edges, &[]).expect("fits");
assert!(!d.is_complete());Sourcepub fn new(
points: &[PointKind],
edges: &[DispatchEdge],
waits: &[Wait],
) -> Result<Self, DispatchError>
pub fn new( points: &[PointKind], edges: &[DispatchEdge], waits: &[Wait], ) -> Result<Self, DispatchError>
Validates a dispatchable network and loads it into a new dispatcher.
Shorthand for Self::EMPTY followed by Self::load. The dispatcher is returned by
value; for large capacities on a small stack prefer Self::load on a dispatcher in
static memory.
§Errors
As for Self::load.
Sourcepub fn load(
&mut self,
points: &[PointKind],
edges: &[DispatchEdge],
waits: &[Wait],
) -> Result<(), DispatchError>
pub fn load( &mut self, points: &[PointKind], edges: &[DispatchEdge], waits: &[Wait], ) -> Result<(), DispatchError>
Validates a dispatchable network and loads it in place, replacing any earlier network and execution state.
This is the onboard trust boundary: every index, bound and capacity is checked, and
nothing is assumed about how the network was produced. Validation completes before the
first write, so a rejected network leaves the dispatcher exactly as it was (still
executing the previous plan, if any). Loading needs no stack memory proportional to
N or E.
§Errors
DispatchError::CapacityExceededifpoints.len() > Nor2 * edges.len() + waits.len() > E.DispatchError::IndexTypeTooSmallif the point or entry count exceeds the maximum ofI(seeIndex).DispatchError::InvalidNetworkfor any malformed input (see its docs).
Sourcepub fn load_encoded(&mut self, bytes: &[u8]) -> Result<(), DecodeError>
pub fn load_encoded(&mut self, bytes: &[u8]) -> Result<(), DecodeError>
Validates an encoded dispatchable network and loads it in place, without copying it anywhere first.
bytes must be exactly one encoding as written by encoding::encode_into (see
encoding for the format). Header, length and CRC-32 are checked first, then every
record, then the network as in Self::load. As there, a rejected encoding leaves the
dispatcher exactly as it was. Never panics on any input.
§Examples
use temporal_control::dispatch::encoding;
use temporal_control::{DecodeError, DispatchEdge, Dispatcher, PointKind, Ticks, TimePoint};
let points = [PointKind::Executable; 2];
let edges = [DispatchEdge { from: TimePoint(0), to: TimePoint(1), weight: Ticks(10) }];
let mut buf = [0u8; 128];
let len = encoding::encode_into(&points, &edges, &[], &mut buf).expect("fits");
let mut d = Dispatcher::<4, 8>::EMPTY;
d.load_encoded(&buf[..len]).expect("valid");
assert_eq!(d.window(TimePoint(1)), Some((None, None)));
buf[30] ^= 0x10;
assert_eq!(d.load_encoded(&buf[..len]), Err(DecodeError::ChecksumMismatch));§Errors
Any DecodeError; DecodeError::Network wraps the errors of Self::load.
Sourcepub fn observe(
&mut self,
tp: TimePoint,
now: Ticks,
) -> Result<(), DispatchError>
pub fn observe( &mut self, tp: TimePoint, now: Ticks, ) -> Result<(), DispatchError>
Records that contingent point tp was observed at now.
The observation is recorded and propagated even when it violates an assumption, so that monitoring continues; the violation is then reported as an error.
§Errors
DispatchError::UnknownTimePointiftpis not a contingent point of this network.DispatchError::AlreadyHappenediftpwas already observed.DispatchError::TimeWentBackwardsifnowprecedes an earlier call.DispatchError::ContingentOutOfBoundsif the activation has not happened ornowis outside[a + lo, a + hi].DispatchError::ConstraintViolatedif a constraint with an already happened point is violated or a window became empty.DispatchError::Overflowon tick overflow.
Sourcepub fn can_execute(&self, tp: TimePoint, now: Ticks) -> bool
pub fn can_execute(&self, tp: TimePoint, now: Ticks) -> bool
Whether executable point tp may execute at now.
Sourcepub fn execute(
&mut self,
tp: TimePoint,
now: Ticks,
) -> Result<(), DispatchError>
pub fn execute( &mut self, tp: TimePoint, now: Ticks, ) -> Result<(), DispatchError>
Executes tp at now and propagates to its neighbors.
§Errors
DispatchError::NotExecutableifSelf::can_executeis false; nothing changes.DispatchError::ConstraintViolatedif propagation empties a window. The point is executed regardless; this means the network was not dispatchable.DispatchError::Overflowon tick overflow.
Sourcepub fn check_deadlines(&mut self, now: Ticks) -> Result<(), DispatchError>
pub fn check_deadlines(&mut self, now: Ticks) -> Result<(), DispatchError>
Advances the clock to now and reports deadlines that have passed.
§Errors
DispatchError::TimeWentBackwardsifnowprecedes an earlier call.DispatchError::DeadlineMissedfor the first executable point withub < now.DispatchError::ContingentOutOfBoundsfor the first contingent point that has not been observed althoughnow > a + hi.
Sourcepub fn step(
&mut self,
now: Ticks,
observed: &[TimePoint],
out: &mut [TimePoint],
) -> Result<usize, DispatchError>
pub fn step( &mut self, now: Ticks, observed: &[TimePoint], out: &mut [TimePoint], ) -> Result<usize, DispatchError>
Runs one tick with the as-soon-as-possible policy.
Order: record every point in observed at now, check deadlines, then execute every
point that may execute at now, repeating until none is left. Executed points are
written to out; the return value is their number.
§Errors
Any error of Self::observe or Self::check_deadlines, which stops the tick, and
DispatchError::OutputFull if out cannot hold the next executed point (that point
is not executed). A buffer of N entries never fills up.
Sourcepub fn next_wakeup(&self) -> Option<Ticks>
pub fn next_wakeup(&self) -> Option<Ticks>
Earliest time at or after the latest call at which some executable point could execute
without new observations, or None if none can.
An executive may sleep until then unless an observation arrives first.
Sourcepub fn time(&self, tp: TimePoint) -> Option<Ticks>
pub fn time(&self, tp: TimePoint) -> Option<Ticks>
Time at which tp happened, or None if it has not (or is unknown).
Sourcepub fn window(&self, tp: TimePoint) -> Option<(Option<Ticks>, Option<Ticks>)>
pub fn window(&self, tp: TimePoint) -> Option<(Option<Ticks>, Option<Ticks>)>
Current window (lb, ub) of tp; None bounds are infinite. None for unknown handles.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Whether every time point has happened.