bus-contract
Traits and types for an in-process bus of hook points (requests,
events, alter, collect) with ordered listeners. A library declares its
points and listeners against the Bus trait; the application creates
the bus and passes it to each library.
Guarantees in five lines
- Ordered listeners: a point's listeners run in their declared
order (
after,before,last, then priority). - Failure isolation: a failing listener (error, panic, missed deadline) is isolated: its edit is dropped, the others run, and the failure is returned to the caller.
- Veto without rollback: a refusal stops a request, but never rolls back what the listeners before it did.
- Fail-open or fail-closed: a listener failure on a request is
skipped (fail-open), unless the point is declared
fail_closed(relayable requests always are): then it refuses the request. - No global: the application owns the bus and hands it to each library; a library never constructs one.
Each property, with a stable id (core.order.declared,
core.request.veto-stops, …), what is not guaranteed (crossing is
not a distributed transaction, authorities are not a security
boundary, the journal is a window in memory) and the bus-kernel
conformance check that proves it, is in
GUARANTEES.md,
also the guarantees page of the crate docs.
Getting started
This crate has no implementation: add
bus-kernel (the
reference implementation, Kernel) to get started, or implement Bus
yourself (bus-kernel's conformance
feature checks any implementation against the guarantees).
A point has one of four kinds. alter transforms a shared value: each
listener edits what the previous ones left, and the caller gets it
back. collect gathers every listener's answer, each in a fresh value,
and the failures. A request is an alter a listener may refuse; an
event says something happened. PointKind is not only a label:
Request enables refusals, Event gates the journal.
Example
A library with one request point and one listener, and an application that wires it. The whole block compiles as a doctest of the crate.
use Arc;
use ;
/// The library's request: a document closes.
;
/// The library's points, readable before anything is built.
pub const POINTS: & = &;
/// The library takes the bus; it never constructs one.
;
/// The application creates the bus and passes it to each library.
/// `bus` is `Arc::new(bus_kernel::Kernel::new())`, or your own `Bus`.
What is in the workspace
Each crate is released and versioned on its own
(RELEASING.md).
The core names none of the capabilities: each capability crate owns its
trait, its CONTRACT and its facets, and an extension trait gives its
getter on any bus through the core's typed lookup Bus::capability,
so a capability contract can change without a core release.
| Crate | What for | Trait, getter | Details |
|---|---|---|---|
bus-contract |
The core: points, order, alter and collect, requests, local listeners, introspection, the capability lookup | Bus |
this page |
bus-contract-defer |
Code that cannot be re-entered (an interface drawing): requests asked now, run at the next drain | Defer, BusDefer → bus.defer() |
defer |
bus-contract-timings |
Finding slow listeners: times per point and per listener, budgets reported | Timings, BusTimings → bus.timings() |
timings |
bus-contract-journal |
Publishers that must not wait: numbered events, read again, delivered off the publisher (facet JOURNALED) |
Journal, BusJournal → bus.journal() |
journal |
bus-contract-crossing |
One bus over several processes: what crosses, with origin and authorities (requires the journal; facets relayable_event, relayable_request) |
Relay, BusCrossing → bus.relay() |
relay, crossing |
Bus is an object-safe trait (Arc<dyn Bus>). The core also gives
typed calls on dyn Bus (declare::<P>,
listen::<P>, listen_local::<P>, alter::<P>, collect::<P>,
on_failure, on_pending), written once over the erased methods, so an
implementation writes only those; listeners with an owner (forget
removes them all), unlisten, failure sinks and introspection; a
contract table (PointDecl, a const declared with declare_all); and
tools on it: order_dump and missing_targets (an ordering constraint
naming a listener that is not there). No dependencies, no unsafe, no
async runtime.
Upgrading from 0.4: MIGRATION-0.5.md.
Rules, one page each
- Naming points:
ids are
<crate>.<subject>.<moment>, so libraries sharing a bus cannot collide; a badPointIdin aconstis a compile error. - Local listeners:
listen_local::<P>for state that is notSend, bound to its thread, ordered with the others; an emit from another thread queues it for that thread'srun_pending(). - Refusals:
a listener of a request returns
HookError::refuse(reason)and the request stops there; a point declaredfail_closedturns a listener failure into a refusal. - Capabilities:
needing one (
needs, facets: if you mark a pointJOURNALED, the bus must provideJournal, ordeclare_allrefuses it), doing without one, and offering one from an implementation. - Crossing: what may cross a process boundary and what it means: deadlines that fail closed, vetoes with their reason, asks answered once, origin, authorities.
- Journal and Relay: how to publish, read again and relay events, and why they are shaped this way.
Stability: 1.x
bus-contract follows Semantic Versioning. From
1.0 on, no 1.x release breaks code that compiled against an earlier
1.x, on either side of the contract:
- Libraries and applications (the callers): every public item
(traits, types, functions, macros, constants) keeps its name, its
path and its signature; the typed calls on
dyn Busstay. - Implementations of
Bus: a method added toBusin 1.x comes with a default body, so an existing implementation keeps compiling; the required methods do not change. - Behaviour: each property of GUARANTEES.md keeps its id and its meaning. A 1.x release may add properties; it never weakens one, and never reuses an id for something else.
- Point and listener ids: an id the 1.0 rules accept stays valid.
- No dependencies, no
unsafe: both stay for all of 1.x.
What 1.x does not promise:
- Raising the MSRV (1.95 today) is a minor release, never a patch.
- Enums marked
#[non_exhaustive](such asIdError) may gain variants in a minor release. - Text meant for people may change in any release:
DisplayandDebugoutput, error messages, the format oforder_dump. - The capability crates (
bus-contract-defer,-timings,-journal,-crossing) are versioned on their own and are not covered by this promise; each says its own status in its README.
A change that would break any of the above waits for 2.0, with a migration guide like MIGRATION-0.5.md.
Not included
- Transport across processes: a relay reads, takes in and answers; a bridge moves the text (framing, wire format, sockets, reconnection, discovery, back-pressure).
License
MIT OR Apache-2.0, at your option.