Expand description
§nexus-rt
Runtime primitives for single-threaded, event-driven systems.
nexus-rt provides the building blocks for constructing runtimes where
user code runs as handlers dispatched over shared state. It is not an
async runtime — there is no task scheduler, no work stealing, no Future
polling. Instead, it provides:
-
World —
Worldis a unified type-erased singleton store. Each registered type gets a dense index (ResourceId) for ~3-cycle dispatch-time access. -
Resources —
ResandResMutare what users see in handler function signatures. They deref to the inner value transparently. -
Handlers — The
Paramtrait resolves state at build time and produces references at dispatch time.IntoHandlerconverts plain functions intoHandlertrait objects for type-erased dispatch. -
Pipeline —
PipelineStartbegins a typed per-event composition chain. Steps transform data usingOptionandResultfor flow control.PipelineimplementsHandlerfor direct or boxed dispatch.BatchPipelineowns a pre-allocated input buffer and runs each item through the same chain independently — errors on one item don’t affect subsequent items. -
DAG Pipeline —
DagStartbuilds a monomorphized data-flow graph with fan-out and merge. Topology is encoded in the type system — no vtable dispatch, no arena allocation.DagimplementsHandlerfor direct or boxed dispatch.BatchDagowns a pre-allocated input buffer and runs each item through the same DAG independently. -
Installer —
Installeris the install-time trait for event sources.install()registers resources intoWorldBuilderand returns a concrete poller whosepoll()method drives the event lifecycle. -
Templates —
HandlerTemplateandCallbackTemplateresolve parameters once, then stamp out handlers viagenerate()by copying pre-resolved state (~1 cycle). Use when handlers are created repeatedly on the hot path (IO re-registration, timer rescheduling). -
Plugin —
Pluginis a composable unit of resource registration.WorldBuilder::install_pluginconsumes a plugin to configure state.
§Quick Start
use nexus_rt::{WorldBuilder, ResMut, IntoHandler, Handler};
let mut builder = WorldBuilder::new();
builder.register::<u64>(0);
let mut world = builder.build();
fn tick(mut counter: ResMut<u64>, event: u32) {
*counter += event as u64;
}
let mut handler = tick.into_handler(world.registry());
handler.run(&mut world, 10u32);
assert_eq!(*world.resource::<u64>(), 10);§Safety
The low-level get / get_mut methods on World are unsafe and
intended for framework internals. The caller must ensure:
- The ID was obtained from the same builder that produced the container.
- The type parameter matches the type registered at that ID.
- No mutable aliasing — at most one
&mut Texists per value at any time.
User-facing APIs (resource, resource_mut, Handler::run) are fully safe.
§Bevy Analogies
nexus-rt borrows heavily from Bevy ECS’s system model. If you know Bevy, these mappings may help:
| nexus-rt | Bevy | Notes |
|---|---|---|
Param | SystemParam | Two-phase init/fetch |
Res<T> | Res<T> | Shared resource read |
ResMut<T> | ResMut<T> | Exclusive resource write |
Local<T> | Local<T> | Per-handler state |
Handler<E> | System trait | Object-safe dispatch |
IntoHandler | IntoSystem | fn → handler conversion |
Plugin | Plugin | Composable registration |
World | World | Singletons only (no ECS) |
HandlerTemplate | (no equivalent) | Resolve-once, stamp-many |
Re-exports§
pub use dag::BatchDag;pub use dag::Dag;pub use dag::DagStart;pub use dag::resolve_arm;pub use pipeline::BatchPipeline;pub use pipeline::IntoStep;pub use pipeline::Pipeline;pub use pipeline::PipelineBuilder;pub use pipeline::PipelineOutput;pub use pipeline::PipelineStart;pub use pipeline::resolve_step;pub use scheduler::MAX_SYSTEMS;pub use scheduler::SchedulerInstaller;pub use scheduler::SchedulerTick;pub use scheduler::SystemId;pub use scheduler::SystemScheduler;pub use shutdown::Shutdown;pub use shutdown::ShutdownHandle;pub use system::IntoSystem;pub use system::System;pub use system::SystemFn;pub use template::Blueprint;pub use template::CallbackBlueprint;pub use template::CallbackTemplate;pub use template::HandlerTemplate;pub use template::TemplatedCallback;pub use template::TemplatedHandler;
Modules§
- dag
- DAG pipeline — monomorphized data-flow graphs with fan-out and merge.
- pipeline
- Pre-resolved pipeline dispatch using
Paramsteps. - scheduler
- DAG scheduler with boolean propagation.
- shutdown
- Cooperative shutdown flag for event loops.
- system
- Reconciliation systems with boolean propagation.
- template
- Resolve-once, stamp-many handler templates.
- testing
- Testing utilities for nexus-rt handlers and timer drivers.
Macros§
- callback_
blueprint - Declares a callback
CallbackBlueprintkey struct. - fan_out
- Constructs a
FanOutcombinator from 2-8 handlers. - handler_
blueprint - Declares a handler
Blueprintkey struct.
Structs§
- Adapt
- Lightweight adapter that decodes a wire-format event into a domain type before dispatching to an inner handler.
- Broadcast
- Dynamic fan-out combinator. Takes ownership of an event, borrows
it, and dispatches
&Eto N handlers, where N is determined at runtime. - ByRef
- Owned-to-reference adapter. Wraps a
Handler<&E>and implementsHandler<E>— the event is borrowed before dispatch. - Callback
- Unified dispatch type. Stores per-callback context alongside pre-resolved resource access.
- Catch
Assert Unwind Safe - Panic-catching wrapper for
Handlerimplementations. - Cloned
- Reference-to-owned adapter. Wraps a
Handler<E>and implementsHandler<&E>— the event is cloned before dispatch. - FanOut
- Static fan-out combinator. Takes ownership of an event, borrows it,
and dispatches
&Eto N handlers. - Local
- Per-handler local state. Stored inside the dispatch wrapper (e.g.
Callbackor pipelineStage), not inWorld. - Owned
- Reference-to-owned adapter via
ToOwned. Wraps aHandler<E::Owned>and implementsHandler<&E>— the event is converted viato_owned()before dispatch. - Registry
- Type-to-index mapping shared between
WorldBuilderandWorld. - Registry
Ref - Read-only access to the
Registryduring handler dispatch. - Res
- Shared reference to a resource in
World. - ResMut
- Mutable reference to a resource in
World. - Resource
Id - Dense index identifying a resource type within a
Worldcontainer. - Sequence
- Monotonic event sequence number for change detection.
- World
- Frozen singleton resource storage.
- World
Builder - Builder for registering resources before freezing into a
Worldcontainer.
Traits§
- Handler
- Object-safe dispatch trait for event handlers.
- Installer
- Install-time trait for event sources.
- Into
Callback - Converts a named function into a
Callback. - Into
Handler - Converts a plain function into a
Handler. - Param
- Trait for types that can be resolved from a
Registryat build time and fetched fromWorldat dispatch time. - Plugin
- Composable unit of resource registration.