Skip to main content

Crate bus_contract

Crate bus_contract 

Source
Expand description

§bus-contract

github Latest version Documentation Build Status MSRV 1.95 no unsafe no dependencies MIT Apache

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 by after / before / last, then priority; unlisten; forget an owner; alter and collect emits that return every listener’s failure (error, panic, missed deadline); failure sinks; introspection.
  • Typed calls on dyn Bus (declare::<P>, listen::<P>, alter::<P>, collect::<P>, on_failure), written once over the erased methods: an implementation writes only those.
  • A contract table (PointDecl): a library’s points as a const, readable before anything is built, declared with declare_all.
  • Tools on the contract: order_dump (“who runs when”, for reviewed files and debug views) and missing_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 std::sync::Arc;
use bus_contract::{Bus, ListenOpts, Point, PointDecl, PointKind, declare_all};

/// The library's request: a document closes.
pub struct DocClose;
impl Point for DocClose {
    const ID: &'static str = "doc.close";
    const KIND: PointKind = PointKind::Request;
    type Input = String;
    type Value = Vec<String>;
}

/// The library's points, readable before anything is built.
pub const POINTS: &[PointDecl] = &[PointDecl::of::<DocClose>("A document closes.")];

/// The library takes the bus; it never constructs one.
pub struct Documents(Arc<dyn Bus>);

impl Documents {
    pub fn new(bus: Arc<dyn Bus>) -> Documents {
        declare_all(&*bus, POINTS);
        let opts = ListenOpts::new().owner("docs");
        let _ = bus.listen::<DocClose>("docs.autosave", opts, |doc, did| {
            did.push(format!("saved {doc}"));
            Ok(())
        });
        Documents(bus)
    }

    pub fn close(&self, doc: &str) -> Vec<String> {
        self.0.alter::<DocClose>(doc.to_owned(), Vec::new()).value
    }
}

The application creates the bus and passes it to each library:

ⓘ
let bus: Arc<dyn Bus> = Arc::new(egui_pop_hooks::Kernel::new()); // or its own
let docs = Documents::new(bus.clone());
let search = Search::new(bus.clone());
print!("{}", bus_contract::order_dump(&*bus, &["doc.close"]));

§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.

Structs§

Altered
The result of an alter emit.
Collected
The result of a collect emit.
Failure
One listener’s failure: its contribution was dropped (except for a FailureKind::MissingTarget), the other listeners ran.
HookError
The error a listener returns.
ListenOpts
How a listener is placed and limited.
ListenerInfo
A listener as introspection shows it.
PointDecl
One line of a library’s contract table: a point it declares, built from the point’s type in a const (PointDecl::of::<DocClose>(…)), so the application can read what a library will host before building anything. declare_all declares a table on a bus.
PointInfo
A point as introspection shows it.
PointKey
A point as the erased methods of Bus name it: its id, its kind and its type, so an implementation refuses one id registered with two types.
PointSpec
A point’s declaration: its description and limits, for introspection.

Enums§

FailureKind
What went wrong with one listener during an emit.
PointKind
What a point is for: declared, for introspection and review, not enforced (a request is an alter emit over the request’s input).
Refused
Why a declaration or a listener was refused. Nothing was changed.

Constants§

DEFAULT_DEADLINE
The deadline of an in-process listener when neither the listener nor its point sets one.
FAILED_FACT
The name a host gives the fact it publishes for each Failure its sinks receive (Bus::add_failure_sink).
LAST
The after target that places a listener after every other listener of its point (ListenOpts::last): one per point, a second is refused as a Refused::Cycle (each would run after the other).
PLUGIN_DEADLINE
The deadline meant for listeners that answer from another process (a plugin over RPC): give it with ListenOpts::deadline or PointSpec::deadline.

Traits§

Bus
The bus contract: hook points, their listeners in a declared order, emits and introspection. An application supplies one implementation (its own, or one written for it, such as egui-pop-hooks’s Kernel) and hands it to each library at construction; a library never constructs the bus it publishes on.
Point
A hook point: one type per point, so its input and value are checked at compile time. Declared by the crate that owns the result.

Functions§

declare_all
Declares every point of a library’s contract table on bus; the refusals (a point already declared: another library, or a second application on the same bus), in table order.
erase
Erases a typed listener of P.
missing_targets
The ordering constraints of points that name a listener not there: point: listener -> target. A test ends with none.
order_dump
The order the listeners of points run in, one point after the other, each listener with its owner and what it declares: “who runs when”, read rather than remembered.

Type Aliases§

AnyInput
An emit’s input, its type erased: a point’s Point::Input.
AnyListener
A listener, its types erased: it reads the input and edits the value in place.
AnyValue
A value going through an emit, its type erased: a point’s Point::Value.
CloneFn
Copies an erased value of a point’s type (an alter keeps the value as it was when a listener fails).
FailureSink
Receives each failure that dropped a contribution (Failure::dropped).
FreshFn
Makes a fresh erased value of a point’s type (each listener’s part in a collect).