dispatch-core
dispatch-core is a small, allocation-free, table-driven dispatcher for
no_std environments. It borrows a dispatch table, selects its first matching
item, and executes that item's handler with an explicit mutable context.
The crate deliberately does not provide middleware, application state ownership, transactions, asynchronous execution, or dynamic route registration. Those policies can be built around the small selection and execution core.
Installation
Add the crate to your project:
[]
= "0.2.0"
Execution model
For each call, the dispatcher:
- scans the borrowed table from beginning to end;
- selects the first item accepted by the matcher;
- invokes the selected item's handler;
- returns the handler output or a structured error.
Table order defines priority. Matching and input access are immutable. Runtime state, services, buffers, and device handles belong in the mutable context. Context changes and other side effects are not rolled back when a handler returns an error.
Example
use Infallible;
use ;
;
let routes = ;
let dispatcher = new;
let mut context = default;
assert_eq!;
assert_eq!;
Matchers can also be closures:
# use Infallible;
# use ;
#
#
let routes = ;
let dispatcher = new;
assert_eq!;
Use [Dispatcher::select] when selection is needed without execution.
Complete example
See examples/command_dispatch.rs for a
complete command router whose table stores handler function pointers. Run it
with:
cargo run --example command_dispatch
Complexity and guarantees
- Selection is a deterministic
O(n)linear scan. - No heap allocation is performed by the crate.
- The crate has no dependencies and forbids unsafe code.
- The dispatch table and input are never mutated.
- The dispatcher only needs a shared reference during dispatch.
- Handlers that need no runtime context can use
Context = ().
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 because it 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
The current public-release implementation, tests, example, documentation, and release configuration were generated by OpenAI Codex under the repository owner's direction. They should not be represented as handwritten human work. See AI_DISCLOSURE.md for the exact scope.