dispatch-core
dispatch-core is a small, no_std, allocation-free command dispatcher for
embedded and other resource-constrained systems. Commands and optional
pipeline stages declare the capabilities they need as function parameters;
the dispatcher validates those accesses, injects the resources, and calls the
functions.
The crate is not an ECS or an application runtime. It is a predictable execution core for static command routers built above drivers, a HAL, an OS, or another source of system capabilities.
Design
The API keeps four application concepts separate:
- Context is mutable state for the current request, such as an opcode, payload, session, or state-machine position.
- Resources are longer-lived system capabilities, such as I/O, crypto, storage, timers, and service handles.
- Commands are ordinary functions whose parameters state exactly which resources they use.
- Pipeline stages are independent before, after, and response functions that apply common policy without being repeated in every command.
Context does not need to grow into a container for every system capability, and the command table contains routing only:
fn version(context: &mut Context)
fn send(context: &mut Context, io: ResMut<Io>)
fn sign(context: &mut Context, crypto: Res<Crypto>, io: ResMut<Io>)
static COMMANDS = [
command!(Opcode::Version => version),
command!(Opcode::Send => send),
command!(Opcode::Sign => sign),
];
Installation
[]
= "0.4"
Complete example
use ;
resources!
;
type Entry = ;
static COMMANDS: & = &;
let dispatcher = new
.with_before
.with_after
.with_response;
let mut context = Context ;
let mut resources = Resources ;
let packet = dispatcher.dispatch;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Execution pipeline
A configured dispatcher runs these stages in order:
- scan the command table and select the first matching entry;
- run
before, if configured; - validate and inject the command parameters;
- execute the command;
- run
after, if configured, with mutable access to the successful output; - run
response, if configured, consuming that output and producing the final return type.
Processing stops at the first error. after is skipped when before or the
command fails, and response is skipped when any earlier stage fails. Response
handling applies to successful command outputs; application errors remain
structured DispatchError values.
The three extensions are independent. Configure only the behavior an application needs:
# use ;
# ;
# ;
# ;
# type Entry = ;
# let commands: & = &;
#
#
#
let only_before = new.with_before;
let only_after = new.with_after;
let only_response = new.with_response;
# let _ = ;
Omitting before or after installs a zero-sized no-op stage. Omitting
response returns the command output unchanged. Commands and all three stage
functions support zero through eight injected resource parameters.
Each stage obtains and releases its resource views before the next stage
starts. Two stages may therefore use the same ResMut<T> sequentially without
conflicting; alias checks apply to the simultaneously live parameters inside
one function.
Static command table
CommandEntry stores only a key and a monomorphized function pointer. A
static entry slice can remain in read-only memory and requires no runtime
registration or heap allocation. Table order defines priority.
The convenience dispatch function uses exact key matching and the identity
pipeline:
# use Infallible;
# use ;
#
# ;
#
# ;
#
# type Entry = ;
let commands: & = &;
let mut context = Context;
let mut resources = Resources;
assert_eq!;
Construct Dispatcher directly when custom matching or pipeline stages are
needed.
Resource parameters
Res<T> requests shared access to a resource. ResMut<T> requests exclusive
access. Both are transparent borrowing wrappers that implement Deref, and
neither allocates memory.
The resources! macro defines the application-owned container and generates
typed providers for its fields:
# use resources;
# ;
# ;
resources!
A resource type may appear once without a tag. Tags distinguish multiple fields with the same type:
# use ;
;
;
;
resources!
# ;
# ;
# ;
Shared views of one field may coexist. A shared and exclusive view, or two
exclusive views, conflict and fail before their function runs. Resource types
and tags used by the standard views are 'static types; the resource
container value itself may live on the stack and is borrowed only for the
current stage.
Safety boundary
Directly producing several field references from one resource container requires a small unsafe core. The boundary is explicit:
dispatchholds an exclusive&mut Resourceswhile each stage prepares its arguments and runs;ResourceProviderconnects a resource type and optional tag to one field;ResandResMutturn that provider into shared or exclusive access;- every pair of accesses is checked before any reference is created;
- only
ResourceProvider::getandSystemParam::fetchreceive raw pointers; resources!generates resource identity and field projection together, so ordinary containers require no application-written unsafe code;- pointers are checked for nullness and alignment before references are made;
- manual
unsafe impl ResourceProviderorSystemParamis reserved for custom backends and carries documented alias and lifetime obligations.
The crate uses #![deny(unsafe_op_in_unsafe_fn)]. Unsafe operations remain in
explicit, locally auditable blocks.
Matching
The convenience dispatch function uses CommandKey and exact equality.
Applications needing ranges, masks, priorities, protocol versions, or other
selection rules can construct a Dispatcher with a custom Matcher:
# use Infallible;
# use ;
#
# ;
# type Entry = ;
# let commands: & = &;
let dispatcher = new;
# let _ = dispatcher;
Matching sees shared references only. Context and resources become mutable after a command has been selected.
Errors
DispatchError<E> preserves the point of failure:
NotFoundmeans no command matched;Param { stage, error }identifies both the pipeline stage and its access or fetch failure;Before(E),Command(E),After(E), andResponse(E)preserve the application error from that function.
No panic is used for ordinary lookup, resource, hook, command, or response failures.
Complexity and guarantees
- Command selection is a deterministic
O(n)linear scan. - Resource validation is
O(p²)per executed stage, wherepis at most eight and uses only a small fixed-size stack array. - Each configured stage runs at most once and only after all preceding stages succeed.
- The crate performs no heap allocation and has no dependencies.
- Static command tables require no mutable registration state.
- Context and Resources remain separate application-owned values.
- Resource dependencies appear only in function signatures, not in the command table or dispatcher configuration.
- Dispatch is synchronous. Async execution, transactions, dynamic plugins, runtime resource ownership, and transport-specific error encoding are intentionally outside this core.
Embedded build
The repository includes a convenience command for Cortex-M0 targets:
cargo build-thumbv6m
Normal cargo build, cargo test, and cargo clippy commands use the host
target, so the test harness remains available.
Minimum supported Rust version
dispatch-core requires Rust 1.85 or newer and uses the Rust 2024 edition.
License
Licensed under either of the following, at your option:
- Apache License, Version 2.0 (LICENSE-APACHE);
- MIT License (LICENSE-MIT).
AI authorship disclosure
See AI_DISCLOSURE.md for the repository's disclosure.