Struct Kernel
pub struct Kernel { /* private fields */ }Expand description
The hook kernel: hook points, their listeners, emits and
introspection. Send + Sync: share it in an Arc. Listeners run with
no lock held, so a listener may emit or listen itself.
Implementations§
§impl Kernel
impl Kernel
pub fn new() -> Kernel
pub fn new() -> Kernel
An empty kernel whose listeners get DEFAULT_DEADLINE unless
they or their point set another.
pub fn with_default_deadline(deadline: Duration) -> Kernel
pub fn with_default_deadline(deadline: Duration) -> Kernel
An empty kernel with another default deadline.
pub fn with_journal(self, journal: Arc<dyn Journal>) -> Kernel
pub fn with_journal(self, journal: Arc<dyn Journal>) -> Kernel
The kernel with the host’s journal: it offers the contract’s
Journal capability (BusJournal::journal), so points may be
declared journaled (bus_contract_journal::JOURNALED).
The kernel answers Journal itself: a publish to a point it
does not hold declared journaled, or under another type, is
refused there (bus_contract_journal::not_journaled, Refused::TypeMismatch);
everything else goes to journal. The host’s journal keeps the
entries and runs the pump: each entry of a journaled point is
delivered by a collect emit on this kernel
(Bus::collect_any) from the pump’s thread, never the
publisher’s, so local listeners run at their thread’s
Kernel::run_pending. It reads a point’s declaration with
Kernel::declaration. A journal that needs the kernel to
deliver is built with Arc::new_cyclic, holding a
std::sync::Weak of it:
let kernel = Arc::new_cyclic(|me: &Weak<Kernel>| {
let journal = Arc::new(MyJournal::new(me.clone()));
Kernel::new().with_journal(journal.clone()).with_relay(journal)
});pub fn with_relay(self, relay: Arc<dyn RelayHost>) -> Kernel
pub fn with_relay(self, relay: Arc<dyn RelayHost>) -> Kernel
The kernel with the host’s relay: it offers the contract’s
Relay capability (BusCrossing::relay) when it has a journal too
(Kernel::with_journal), so points may be declared relayable
(bus_contract_crossing::relayable): events, and requests another process
may ask.
The host keeps and reads entries (RelayHost); the kernel
does the rest of the contract’s crossing itself: it holds the
authorities (Relay::add_authority) and checks each crossing
with them, refuses an entry of a point not relayable here or at
another version before the host sees it, writes each point’s
version on the entries served, and answers asks
(Relay::answer) with an alter emit on itself, once per
origin, keeping each answer at least as long as the ask’s
deadline (and 30 s).
pub fn declaration(&self, point: &str) -> Option<(PointKey, PointSpec)>
pub fn declaration(&self, point: &str) -> Option<(PointKey, PointSpec)>
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 declare_point(
&self,
key: PointKey,
spec: PointSpec,
) -> Result<(), Refused>
pub fn declare_point( &self, key: PointKey, spec: PointSpec, ) -> Result<(), Refused>
Declares a point by its key (Bus::declare_point). Refused
when its id breaks the naming rule (Refused::BadId), when a
facet’s contract refuses it (a journaled point that is not an
event, a relayable point that cannot cross), and when it has a
facet of a contract the kernel does not offer here (no journal or
relay from the host), as declare_all would.
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_any(
&self,
key: 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>
pub fn listen_any( &self, key: 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 an erased listener by its point’s key
(Bus::listen_any).
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. It takes its place
in the point’s order; an emit from another thread queues it for
this thread’s Kernel::run_pending.
pub fn listen_local_any(
&self,
key: PointKey,
id: String,
opts: ListenOpts,
run: Rc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError>>,
) -> Result<(), Refused>
pub fn listen_local_any( &self, key: PointKey, id: String, opts: ListenOpts, run: Rc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError>>, ) -> Result<(), Refused>
Registers an erased local listener by its point’s key
(Bus::listen_local_any).
pub fn unlisten(&self, point: &str, id: &str) -> bool
pub fn unlisten(&self, point: &str, id: &str) -> bool
Removes the listener id of the point point; whether it was
there.
pub fn forget(&self, owner: &str) -> usize
pub fn forget(&self, owner: &str) -> usize
Removes every listener of owner, on every point (a plugin
uninstalled, a module switched off); how many were removed.
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 run_pending(&self) -> Vec<PendingRun>
pub 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
(Bus::run_pending). A listener removed since is skipped.
pub fn ask<P>(
&self,
input: <P as Point>::Input,
value: <P as Point>::Value,
) -> u64
pub fn ask<P>( &self, input: <P as Point>::Input, value: <P as Point>::Value, ) -> u64
Asks the request P now: its listeners run at the calling
thread’s next Kernel::drain (Defer). The ask’s number.
pub fn ask_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>,
) -> u64
pub fn ask_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>, ) -> u64
Queues an alter emit of point for the calling thread’s next
Kernel::drain and calls the wakers (Defer::ask_any).
pub fn drain(&self) -> Vec<PendingRun>
pub fn drain(&self) -> Vec<PendingRun>
Runs the requests asked on the calling thread, in ask order, each
as an alter emit with the listeners registered now; what each did
(Defer::drain). An ask made while draining waits for the
next drain.
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.
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 points(&self) -> Vec<PointInfo>
pub fn points(&self) -> Vec<PointInfo>
Every point, in id order, with its listeners in the order they run.
pub fn listeners(&self, point: &str) -> Option<Vec<ListenerInfo>>
pub 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.
§impl Kernel
impl Kernel
pub fn set_timing(&self, on: bool)
pub fn set_timing(&self, on: bool)
Times listener runs (the default) or stops: when off, nothing is counted and no pass is reported over budget; the deadlines still hold. Turning it off keeps the times so far.
pub fn timing(&self) -> bool
pub fn timing(&self) -> bool
Whether listener runs are timed (Kernel::set_timing).
pub fn timings(&self) -> Vec<PointTimings>
pub fn timings(&self) -> Vec<PointTimings>
Every point that ran a pass since the last reset, in id order, its
listeners in the order they run (Timings::points).
pub fn point_timings(&self, point: &str) -> Option<PointTimings>
pub fn point_timings(&self, point: &str) -> Option<PointTimings>
The timings of the point point (Timings::point).
pub fn reset_timings(&self)
pub fn reset_timings(&self)
Starts a new window of times (Timings::reset).
pub fn on_over_budget(&self, sink: impl Fn(&OverBudget) + Send + Sync + 'static)
pub fn on_over_budget(&self, sink: impl Fn(&OverBudget) + Send + Sync + 'static)
Adds a sink called with each pass over its point’s budget, on the
thread that ran it (Timings::add_budget_sink). A sink that
panics is ignored.
Trait Implementations§
§impl Bus for Kernel
impl Bus for Kernel
§fn capability(&self, contract: TypeId) -> Option<Provided<'_>>
fn capability(&self, contract: TypeId) -> Option<Provided<'_>>
Defer, Timings and Trace always; Journal with a journal
(Kernel::with_journal); Relay with a journal and a relay.
The contract crates’ extension traits (BusDefer::defer, …) go
through this lookup, so the two always agree.
§fn declare_point(&self, point: PointKey, spec: PointSpec) -> Result<(), Refused>
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_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>
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>
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>
§fn add_pending_waker(&self, wake: Arc<dyn Fn(ThreadId) + Sync + Send>)
fn add_pending_waker(&self, wake: Arc<dyn Fn(ThreadId) + Sync + Send>)
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
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>>
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>>
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>)
§impl Defer for Kernel
impl Defer for Kernel
§fn ask_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>,
) -> u64
fn ask_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>, ) -> u64
point with input and value for the
calling thread’s next Defer::drain; the ask’s number.§fn drain(&self) -> Vec<PendingRun>
fn drain(&self) -> Vec<PendingRun>
§impl Journal for Kernel
The host’s journal (Kernel::with_journal), behind the kernel’s
own check of what may be published.
impl Journal for Kernel
The host’s journal (Kernel::with_journal), behind the kernel’s
own check of what may be published.
§fn since(&self, seq: u64, limit: usize) -> Since
fn since(&self, seq: u64, limit: usize) -> Since
seq, oldest first, at most limit; a gap when
some were dropped before this read.§fn wait(
&self,
patterns: &[&str],
seq: u64,
timeout: Duration,
) -> Option<JournalEntry>
fn wait( &self, patterns: &[&str], seq: u64, timeout: Duration, ) -> Option<JournalEntry>
seq whose kind matches one of patterns
(kind_matches; empty matches every kind), waiting for it up
to timeout; None when none came. An entry already in the
journal is returned at once.§fn publish_any(
&self,
point: PointKey,
input: Arc<dyn Any + Sync + Send>,
fresh: fn() -> Box<dyn Any + Send>,
cause: Option<u64>,
) -> Result<u64, Refused>
fn publish_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, fresh: fn() -> Box<dyn Any + Send>, cause: Option<u64>, ) -> Result<u64, Refused>
point, caused by the
entry cause; its number. fresh makes the value each listener
fills at delivery. Refused, with nothing recorded, when the point
is not declared journaled on this bus (not_journaled)
or is declared with another type (Refused::TypeMismatch).§impl Relay for Kernel
The host’s relay (Kernel::with_relay) behind the kernel’s own
crossing: authorities, versions and answered asks.
impl Relay for Kernel
The host’s relay (Kernel::with_relay) behind the kernel’s own
crossing: authorities, versions and answered asks.
§fn cursor(&self) -> Cursor
fn cursor(&self) -> Cursor
§fn resume(&self, from: Cursor, limit: usize) -> Resumed
fn resume(&self, from: Cursor, limit: usize) -> Resumed
from, at most limit.§fn ingest(
&self,
origin: &Origin,
entry: RelayedEntry,
) -> Result<u64, IngestError>
fn ingest( &self, origin: &Origin, entry: RelayedEntry, ) -> Result<u64, IngestError>
origin; its number in this
journal.§fn answer(&self, origin: &Origin, ask: RelayedAsk) -> RelayedAnswer
fn answer(&self, origin: &Origin, ask: RelayedAsk) -> RelayedAnswer
origin: runs the request’s listeners
here and says what they did. Blocks while they run.§impl Timings for Kernel
impl Timings for Kernel
§fn points(&self) -> Vec<PointTimings>
fn points(&self) -> Vec<PointTimings>
§fn point(&self, point: &str) -> Option<PointTimings>
fn point(&self, point: &str) -> Option<PointTimings>
point; None when it ran no pass
since the last reset.§fn reset(&self)
fn reset(&self)
§fn add_budget_sink(&self, sink: Arc<dyn Fn(&OverBudget) + Sync + Send>)
fn add_budget_sink(&self, sink: Arc<dyn Fn(&OverBudget) + Sync + Send>)
OVER_BUDGET_FACT fact or logs. A sink that panics is ignored.