Trait Bus
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: Arc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError> + Sync + Send>,
) -> Result<(), Refused>;
fn listen_local_any(
&self,
point: PointKey,
id: String,
opts: ListenOpts,
run: Rc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError>>,
) -> Result<(), Refused>;
fn run_pending(&self) -> Vec<PendingRun>;
fn add_pending_waker(&self, wake: Arc<dyn Fn(ThreadId) + Sync + Send>);
fn unlisten(&self, point: &str, id: &str) -> bool;
fn forget(&self, owner: &str) -> usize;
fn alter_any(
&self,
point: PointKey,
input: Arc<dyn Any + Sync + Send>,
value: Box<dyn Any + Send>,
clone: fn(&(dyn Any + Send + 'static)) -> Box<dyn Any + Send>,
) -> Altered<Box<dyn Any + Send>>;
fn collect_any(
&self,
point: PointKey,
input: Arc<dyn Any + Sync + Send>,
fresh: fn() -> Box<dyn Any + Send>,
) -> Collected<Box<dyn Any + Send>>;
fn add_failure_sink(&self, sink: Arc<dyn Fn(&Failure) + Sync + Send>);
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§
fn 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.
fn listen_any(
&self,
point: PointKey,
id: String,
opts: ListenOpts,
run: Arc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError> + Sync + Send>,
) -> Result<(), Refused>
fn listen_any( &self, point: PointKey, id: String, opts: ListenOpts, run: Arc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError> + Sync + Send>, ) -> 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.
fn listen_local_any(
&self,
point: PointKey,
id: String,
opts: ListenOpts,
run: Rc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError>>,
) -> Result<(), Refused>
fn listen_local_any( &self, point: PointKey, id: String, opts: ListenOpts, run: Rc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError>>, ) -> 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).
fn 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.
fn add_pending_waker(&self, wake: Arc<dyn Fn(ThreadId) + Sync + Send>)
fn add_pending_waker(&self, wake: Arc<dyn Fn(ThreadId) + Sync + Send>)
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.
fn 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.
fn alter_any(
&self,
point: PointKey,
input: Arc<dyn Any + Sync + Send>,
value: Box<dyn Any + Send>,
clone: fn(&(dyn Any + Send + 'static)) -> Box<dyn Any + Send>,
) -> Altered<Box<dyn Any + Send>>
fn alter_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, value: Box<dyn Any + Send>, clone: fn(&(dyn Any + Send + 'static)) -> Box<dyn Any + Send>, ) -> Altered<Box<dyn Any + Send>>
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.
fn collect_any(
&self,
point: PointKey,
input: Arc<dyn Any + Sync + Send>,
fresh: fn() -> Box<dyn Any + Send>,
) -> Collected<Box<dyn Any + Send>>
fn collect_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, fresh: fn() -> Box<dyn Any + Send>, ) -> Collected<Box<dyn Any + Send>>
Gives each of the point’s listeners, in order, a value from
fresh to fill, and gathers those that succeed.
fn add_failure_sink(&self, sink: Arc<dyn Fn(&Failure) + Sync + Send>)
fn add_failure_sink(&self, sink: Arc<dyn Fn(&Failure) + Sync + Send>)
Adds a sink called with each failure that dropped a contribution, as it happens.
fn points(&self) -> Vec<PointInfo>
fn points(&self) -> Vec<PointInfo>
Every point, in id order, with its listeners in the order they run.
fn listeners(&self, point: &str) -> Option<Vec<ListenerInfo>>
fn listeners(&self, point: &str) -> Option<Vec<ListenerInfo>>
The listeners of the point point, in the order they run; None
for a point nobody declared or listened to.
Provided Methods§
fn 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§
§impl<'a> dyn Bus + 'a
impl<'a> dyn Bus + 'a
pub fn get<C>(&self) -> Option<&C>where
C: 'static + ?Sized,
pub fn get<C>(&self) -> Option<&C>where
C: 'static + ?Sized,
The capability C of this bus, a trait object type such as
dyn Journal (capability).
§impl<'a> dyn Bus + 'a
impl<'a> dyn Bus + 'a
pub fn declare<P>(&self, spec: PointSpec) -> Result<(), Refused>where
P: Point,
pub fn declare<P>(&self, spec: PointSpec) -> Result<(), Refused>where
P: Point,
Declares the point P: its description and deadline. Listeners
may register before or after; declaring twice is refused.
pub fn listen<P>(
&self,
id: impl Into<String>,
opts: ListenOpts,
run: impl Fn(&<P as Point>::Input, &mut <P as Point>::Value) -> Result<(), HookError> + Send + Sync + 'static,
) -> Result<(), Refused>where
P: Point,
pub fn listen<P>(
&self,
id: impl Into<String>,
opts: ListenOpts,
run: impl Fn(&<P as Point>::Input, &mut <P as Point>::Value) -> Result<(), HookError> + Send + Sync + 'static,
) -> Result<(), Refused>where
P: Point,
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.
pub fn listen_local<P>(
&self,
id: impl Into<String>,
opts: ListenOpts,
run: impl Fn(&<P as Point>::Input, &mut <P as Point>::Value) -> Result<(), HookError> + 'static,
) -> Result<(), Refused>where
P: Point,
pub fn listen_local<P>(
&self,
id: impl Into<String>,
opts: ListenOpts,
run: impl Fn(&<P as Point>::Input, &mut <P as Point>::Value) -> Result<(), HookError> + 'static,
) -> Result<(), Refused>where
P: Point,
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.
pub 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.
pub fn alter<P>(
&self,
input: <P as Point>::Input,
value: <P as Point>::Value,
) -> Altered<<P as Point>::Value>
pub fn alter<P>( &self, input: <P as Point>::Input, value: <P as Point>::Value, ) -> Altered<<P as Point>::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.
pub fn collect<P>(
&self,
input: <P as Point>::Input,
) -> Collected<<P as Point>::Value>
pub fn collect<P>( &self, input: <P as Point>::Input, ) -> Collected<<P as Point>::Value>
Gives each of P’s listeners, in order, a fresh default value to
fill, and gathers the values of those that succeed.
pub 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.
Implementations on Foreign Types§
§impl<B> 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 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.