pub trait Bus: Send + Sync {
Show 13 methods
// Required methods
fn declare_point(
&self,
point: PointKey,
spec: PointSpec,
) -> Result<(), Refused>;
fn listen_any(
&self,
point: PointKey,
id: String,
opts: ListenOpts,
run: AnyListener,
) -> Result<(), Refused>;
fn listen_local_any(
&self,
point: PointKey,
id: String,
opts: ListenOpts,
run: AnyLocalListener,
) -> Result<(), Refused>;
fn run_pending(&self) -> Vec<PendingRun>;
fn add_pending_waker(&self, wake: PendingWaker);
fn unlisten(&self, point: &str, id: &str) -> bool;
fn forget(&self, owner: &str) -> usize;
fn alter_any(
&self,
point: PointKey,
input: AnyInput,
value: AnyValue,
clone: CloneFn,
) -> Altered<AnyValue>;
fn collect_any(
&self,
point: PointKey,
input: AnyInput,
fresh: FreshFn,
) -> Collected<AnyValue>;
fn add_failure_sink(&self, sink: FailureSink);
fn points(&self) -> Vec<PointInfo>;
fn listeners(&self, point: &str) -> Option<Vec<ListenerInfo>>;
// Provided method
fn capability(&self, contract: TypeId) -> Option<Provided<'_>> { ... }
}Expand description
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 bus-kernel’s Kernel)
and hands it to each
library at construction; a library never constructs the bus it
publishes on.
The methods are object safe and their values erased, so a library
holds an Arc<dyn Bus>; the typed calls (declare::<P>,
listen::<P>, alter::<P>, collect::<P>, on_failure) are methods
of dyn Bus, written once here over these.
What an implementation promises:
- Listeners run ordered by
after/beforefirst (the targetLASTplaces one after every other of its point, one per point), then by priority (higher first), then by registration. A cycle is refused at registration (Refused::Cycle); a target not there is reported (FailureKind::MissingTarget) and ignored. - A listener’s edit counts only when it returns
Okwithin its deadline without panicking; otherwise it is dropped, the others still run, and the failure is returned and sent to the sinks. - The value an emit returns is of the point’s type: the typed calls panic otherwise, as a defect of the implementation.
- Listeners run with no lock held, so a listener may emit or listen.
Every property, with its stable id, what it does not guarantee and
the check that proves it: guarantees.
Refusals, on a point declared PointKind::Request:
- A listener that returns
HookError::Refusestops the request: its own edit is dropped, no later listener runs (on this thread or another: nothing is queued), and the result names the refusal (Altered::refused,Collected::refused). It is not a failure and does not reach the failure sinks. - On a point declared fail-closed (
PointSpec::fail_closed), a listener that fails (error, panic, missed deadline) stops the request the same way: the failure is reported as usual and the result names a refusal withRefusal::failedset. A threaded listener that misses its deadline stops it at once. - On the other kinds,
HookError::Refusecounts as a failure (FailureKind::Errorwith the reason) andfail_closedis ignored.
Capabilities: an implementation may offer more than this core, each
a contract of its own, defined in a crate of its own
(bus-contract-defer, -timings, -journal, -crossing) and found
at run time through Bus::capability, which returns None by
default. Each contract crate adds the typed call on every bus
(use bus_contract_journal::BusJournal; then bus.journal()). A
library that needs one names its Contract in its table
(PointDecl::needs), or a Facet of it
(PointDecl::facets); declare_all then refuses the point on a
bus without it (Refused::Unsupported). A library that can do
without checks the call and falls back.
Ids: a point’s id is a PointId, checked when made; a listener’s
id is checked by the bus when it registers (Refused::BadId), and
a point key whose id breaks the rule is refused at declaration
(check_id).
Local listeners (Bus::listen_local_any) are for state that is not
Send (Rc, RefCell), such as an interface’s. Each is bound to the
thread that registered it:
- It takes its place in the same order as the other listeners of its
point (
after/before/LAST, priority, registration), keeps its owner and deadline, and shows in introspection (ListenerInfo::local). - An emit on its thread runs it in that order, inline, with the others.
- An emit on another thread runs every other listener in order and
queues the local ones for their thread; the result names them
(
Altered::deferred,Collected::deferred) and the wakers (Bus::add_pending_waker) are called. The thread runs them when it callsBus::run_pending, in their order, emits in the order they were queued. A deferred alter’s listeners edit a copy of the value the emit returned; a deferred collect’s listeners each fill a fresh value. What they do is reported byBus::run_pending, not to the emitter. - A listener removed (
Bus::unlisten,Bus::forget), from any thread, does not run again, queued or not.
Required Methods§
Sourcefn declare_point(&self, point: PointKey, spec: PointSpec) -> Result<(), Refused>
fn declare_point(&self, point: PointKey, spec: PointSpec) -> Result<(), Refused>
Declares a point: its description and deadline. Listeners may register before or after; declaring twice is refused.
Sourcefn listen_any(
&self,
point: PointKey,
id: String,
opts: ListenOpts,
run: AnyListener,
) -> Result<(), Refused>
fn listen_any( &self, point: PointKey, id: String, opts: ListenOpts, run: AnyListener, ) -> Result<(), Refused>
Registers a listener on a point under id. Refused, with nothing
changed, when the id is taken on the point, when its constraints
close a cycle, or when the point is registered with another type.
Sourcefn listen_local_any(
&self,
point: PointKey,
id: String,
opts: ListenOpts,
run: AnyLocalListener,
) -> Result<(), Refused>
fn listen_local_any( &self, point: PointKey, id: String, opts: ListenOpts, run: AnyLocalListener, ) -> Result<(), Refused>
Registers a local listener on a point under id, bound to the
calling thread: it runs only on that thread. Refused as
Bus::listen_any is, and when opts asks for a thread of its
own (Refused::Threaded).
Sourcefn run_pending(&self) -> Vec<PendingRun>
fn run_pending(&self) -> Vec<PendingRun>
Runs the local listeners queued for the calling thread by emits on other threads, emit by emit in the order they were queued; what each emit’s deferred part did. Empty when nothing waits.
Sourcefn add_pending_waker(&self, wake: PendingWaker)
fn add_pending_waker(&self, wake: PendingWaker)
Adds a waker called when an emit queues local listeners for a
thread, with that thread’s id: where the host asks that thread to
call Bus::run_pending (a repaint request, a message to its
loop). A waker that panics is ignored.
Sourcefn unlisten(&self, point: &str, id: &str) -> bool
fn unlisten(&self, point: &str, id: &str) -> bool
Removes the listener id of the point point; whether it was
there.
Sourcefn forget(&self, owner: &str) -> usize
fn forget(&self, owner: &str) -> usize
Removes every listener of owner, on every point; how many.
Sourcefn alter_any(
&self,
point: PointKey,
input: AnyInput,
value: AnyValue,
clone: CloneFn,
) -> Altered<AnyValue>
fn alter_any( &self, point: PointKey, input: AnyInput, value: AnyValue, clone: CloneFn, ) -> Altered<AnyValue>
Passes value through the point’s listeners in order, each
editing what the previous ones left; clone copies it so a
failing listener leaves it as it found it.
Sourcefn collect_any(
&self,
point: PointKey,
input: AnyInput,
fresh: FreshFn,
) -> Collected<AnyValue>
fn collect_any( &self, point: PointKey, input: AnyInput, fresh: FreshFn, ) -> Collected<AnyValue>
Gives each of the point’s listeners, in order, a value from
fresh to fill, and gathers those that succeed.
Sourcefn add_failure_sink(&self, sink: FailureSink)
fn add_failure_sink(&self, sink: FailureSink)
Adds a sink called with each failure that dropped a contribution, as it happens.
Provided Methods§
Sourcefn capability(&self, contract: TypeId) -> Option<Provided<'_>>
fn capability(&self, contract: TypeId) -> Option<Provided<'_>>
Looks a capability up by the TypeId of its trait object type
(TypeId::of::<dyn Journal>()): the bus as Any and the cast
to that trait (Provided), or None (the default) when the
implementation does not offer it. The typed call is
capability (capability::<dyn Journal, _>(bus), or
bus.get::<dyn Journal>() on a dyn Bus).
This is how a bus offers a capability whose trait the core does
not name: an extension contract defines its trait and its
Contract, a bus answers for its TypeId, and a library reaches
it through capability or the contract crate’s extension trait.
use std::any::{Any, TypeId};
use bus_contract::{Cast, Provided};
fn as_journal(bus: &dyn Any) -> Option<&(dyn Journal + 'static)> {
bus.downcast_ref::<MyBus>().map(|b| b as _)
}
static AS_JOURNAL: Cast<dyn Journal> = as_journal;
// in `impl Bus for MyBus`:
fn capability(bus: &MyBus, contract: TypeId) -> Option<Provided<'_>> {
(contract == TypeId::of::<dyn Journal>())
.then(|| Provided::new::<dyn Journal>(bus, &AS_JOURNAL))
}Implementations§
Source§impl<'a> dyn Bus + 'a
impl<'a> dyn Bus + 'a
Sourcepub fn declare<P: Point>(&self, spec: PointSpec) -> Result<(), Refused>
pub fn declare<P: Point>(&self, spec: PointSpec) -> Result<(), Refused>
Declares the point P: its description and deadline. Listeners
may register before or after; declaring twice is refused.
Sourcepub fn listen<P: Point>(
&self,
id: impl Into<String>,
opts: ListenOpts,
run: impl Fn(&P::Input, &mut P::Value) -> Result<(), HookError> + Send + Sync + 'static,
) -> Result<(), Refused>
pub fn listen<P: Point>( &self, id: impl Into<String>, opts: ListenOpts, run: impl Fn(&P::Input, &mut P::Value) -> Result<(), HookError> + Send + Sync + 'static, ) -> Result<(), Refused>
Registers a listener on P under id. It reads the emit’s input
and edits the value; its edit counts only when it returns Ok
within its deadline without panicking.
Refused, with nothing changed, when the id is taken on the point
or when its after/before close a cycle. A constraint naming a
listener not (yet) registered is accepted, reported by each emit
and by introspection, and ignored.
Sourcepub fn listen_local<P: Point>(
&self,
id: impl Into<String>,
opts: ListenOpts,
run: impl Fn(&P::Input, &mut P::Value) -> Result<(), HookError> + 'static,
) -> Result<(), Refused>
pub fn listen_local<P: Point>( &self, id: impl Into<String>, opts: ListenOpts, run: impl Fn(&P::Input, &mut P::Value) -> Result<(), HookError> + 'static, ) -> Result<(), Refused>
Registers a local listener on P under id, bound to the calling
thread: it may hold state that is not Send (Rc<RefCell<…>>).
It takes its place in the point’s order with the other listeners;
an emit from another thread queues it for this thread’s next
Bus::run_pending. See Bus for the guarantees.
Sourcepub fn on_pending(&self, wake: impl Fn(ThreadId) + Send + Sync + 'static)
pub fn on_pending(&self, wake: impl Fn(ThreadId) + Send + Sync + 'static)
Adds a waker called, with the thread’s id, when an emit queues local listeners for that thread. A waker that panics is ignored.
Sourcepub fn alter<P: Point>(
&self,
input: P::Input,
value: P::Value,
) -> Altered<P::Value>
pub fn alter<P: Point>( &self, input: P::Input, value: P::Value, ) -> Altered<P::Value>
Passes value through P’s listeners in order: each edits the
value the previous ones left. A listener that fails leaves it as
it found it.
Sourcepub fn collect<P: Point>(&self, input: P::Input) -> Collected<P::Value>
pub fn collect<P: Point>(&self, input: P::Input) -> Collected<P::Value>
Gives each of P’s listeners, in order, a fresh default value to
fill, and gathers the values of those that succeed.
Sourcepub fn on_failure(&self, sink: impl Fn(&Failure) + Send + Sync + 'static)
pub fn on_failure(&self, sink: impl Fn(&Failure) + Send + Sync + 'static)
Adds a sink called with each failure that dropped a contribution
(Failure::dropped), as it happens: where the host publishes
its FAILED_FACT fact or logs. A sink that panics is ignored.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<B: Bus + ?Sized> Bus for Arc<B>
A shared bus is a bus: &Arc<dyn Bus> goes where &dyn Bus is
asked, and an application may hand out Arc<MyBus> as it is.
impl<B: Bus + ?Sized> Bus for Arc<B>
A shared bus is a bus: &Arc<dyn Bus> goes where &dyn Bus is
asked, and an application may hand out Arc<MyBus> as it is.