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 at
construction. There is no global.
Contents
- Points are types (
Point): an id (doc.close), the input every listener reads, the value a listener edits, and a declared kind (request,event,alter,collect). Bus, an object-safe trait (Arc<dyn Bus>): declare a point; listen under an id and an owner, ordered byafter/before/last, then priority; unlisten; forget an owner;alterandcollectemits that return every listener's failure (error, panic, missed deadline); failure sinks; introspection.- Local listeners (
listen_local::<P>) for state that is notSend(Rc<RefCell<…>>): bound to the thread that registered them, in the same order as the other listeners. An emit from another thread queues them for their thread'srun_pending(). - Refusals on requests: a listener returns
HookError::refuse(reason)and the request stops there; a point declaredfail_closedturns a listener failure into a refusal. - Optional capabilities, found at run time:
Bus::defer()returns theDefercapability (requests asked now, heard at the next drain on the same thread) orNone. - 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: an implementation writes only those. - A contract table (
PointDecl): a library's points as aconst, readable before anything is built, declared withdeclare_all. - Tools on the contract:
order_dump("who runs when", for reviewed files and debug views) andmissing_targets(an ordering constraint naming a listener that is not there). - No dependencies, no
unsafe, no async runtime.
This crate has no implementation. One is Kernel in
egui-pop-hooks. The
guarantees an implementation must keep are documented on Bus.
Example
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:
let bus: = new; // or its own
let docs = new;
let search = new;
print!;
Local listeners
A listener registered with listen_local::<P> may hold state that is
not Send, such as an interface's Rc<RefCell<…>>. It is bound to the
thread that registered it.
- It is ordered with the other listeners of its point (
after,before,last, priority), keeps its owner and deadline, and shows in introspection as local. - An emit on its thread runs it in that order with the others.
- An emit on another thread runs the other listeners, queues the local
ones and names them in the result (
deferred). The wakers added withon_pendingare called with the thread's id. The thread runs the queued listeners, in order, when it callsrun_pending(), typically once per pass of its loop. unlistenandforgetremove local listeners too, queued runs included.
let panel = new;
let mine = panel.clone;
bus.?;
bus.on_pending;
// each pass of the interface's loop:
bus.run_pending;
Refusals
On a point declared PointKind::Request, a listener may refuse the
request with a reason. The request stops: no later listener runs, the
refusing listener's edit is dropped, and the result names the refusal
(Altered::refused: point, listener, owner, reason). A refusal is not
a failure and does not reach the failure sinks.
By default a failing listener (error, panic, missed deadline) has its
contribution dropped and the others run. On a point declared
fail_closed, the failure refuses the request instead: the failure is
reported as usual and refused is set with failed: true. A policy
listener placed first on a fail-closed point therefore never lets the
handler run when the policy itself breaks.
pub const POINTS: & =
&;
bus.?;
let out = bus.;
if let Some = out.refused
Capabilities
The Bus trait is the core every implementation offers. An
implementation may offer more, each part as a trait of its own, found
at run time through a provided method of Bus that returns None by
default. One dyn Bus type works everywhere, whatever the
implementation offers.
- A library that needs a capability names it in its contract table
(
PointDecl::needs).declare_allrefuses such a point on a bus without it (Refused::Unsupported) and leaves it undeclared. - A library that can do without one calls the method and falls back
when it gets
None. offers(bus, Capability::Defer)tells whether a bus has one.
Defer
Bus::defer() returns Option<&dyn Defer>. A request asked through it
returns at once; its listeners run at the next drain() on the same
thread. It is for code that cannot be re-entered while it runs, such as
an interface drawing a panel with its state borrowed.
- Asks are per thread and run in ask order, each as an alter emit with the listeners registered at the drain. Refusals and fail-closed points apply.
drain()returns what each ask did (PendingRunwithaskset: the listeners that ran, failures, refusal). The asker gets nothing back.- An ask made during a drain waits for the next drain. An immediate emit made after an ask runs before it.
waiting()counts the asks queued for the calling thread, so a host can drain before it exits.
pub const POINTS: & = &;
// while drawing, with the library borrowed:
bus.defer.expect.;
// after drawing, where nothing is borrowed:
for run in bus.defer.map.unwrap_or_default
Not included
- A journal (events with a revision, catch-up from a revision).
- Transport across processes.
License
MIT OR Apache-2.0, at your option.