Skip to main content

Ctx

Struct Ctx 

Source
pub struct Ctx { /* private fields */ }
Expand description

Typed service registry (Cordis ctx container). Service keys are stable: the key is the TypeId of the trait-object type (dyn Greeter) or of a concrete type; values are Arc<dyn Any + Send + Sync> wrappers around Arc<S>.

Also carries the typed-event handler registry (see crate::dispatch): ctx.on* registers handlers, ctx.emit/parallel/serial/waterfall/ bail dispatch them.

Implementations§

Source§

impl Ctx

Source

pub fn new() -> Self

Source

pub fn provide<S: ?Sized + Service>( &self, svc: Arc<S>, ) -> Result<Disposer, PluginError>

Registers svc under the stable service key S and returns the Disposer that removes exactly this registration.

Source

pub fn resolve<S: ?Sized + Service>(&self) -> Option<Arc<S>>

Looks the service for key S up by trait type; None if absent.

Source

pub fn contains_service<S: ?Sized + Service>(&self) -> bool

Whether a service currently occupies the key S.

Source§

impl Ctx

Source

pub fn on<E, F>(&self, handler: F) -> Disposer
where E: Any + Send + Sync, F: for<'a> Fn(&'a E) -> BoxFuture<'a, ()> + Send + Sync + 'static,

Registers an observer for E (used by both emit and parallel). The returned Disposer unregisters exactly this observer.

Source

pub fn on_serial<E, R, F>(&self, handler: F) -> Disposer
where E: Any + Send + Sync, R: Send + 'static, F: for<'a> Fn(&'a E) -> BoxFuture<'a, R> + Send + Sync + 'static,

Registers an ordered handler for E producing R.

Source

pub fn on_waterfall<E, T, M>(&self, middleware: M) -> Disposer
where E: Any + Send + Sync, T: Send + 'static, M: WaterfallMiddleware<E, T>,

Registers one waterfall middleware step for (E, T).

Source

pub fn on_bail<E, T, F>(&self, handler: F) -> Disposer
where E: Any + Send + Sync, T: Send + 'static, F: for<'a> Fn(&'a E) -> BoxFuture<'a, Option<T>> + Send + Sync + 'static,

Registers a bail handler for (E, T): Some(value) bails the dispatch, None defers to the next handler.

Source

pub async fn emit<E>(&self, event: &E)
where E: Any + Send + Sync,

Runs all observers of E in registration order; no results.

Source

pub async fn parallel<E>(&self, event: &E)
where E: Any + Send + Sync,

Runs all observers of E concurrently and awaits every one.

Source

pub async fn serial<E, R>(&self, event: &E) -> Vec<R>
where E: Any + Send + Sync, R: Send + 'static,

Awaits each serial handler of E in registration order and returns every handler’s value, in order.

Source

pub async fn waterfall<E, T>(&self, event: &E, initial: T) -> T
where E: Any + Send + Sync, T: Send + 'static,

Threads initial through the waterfall middleware chain for (E, T) in registration order; the first Flow::Stop short-circuits the chain (later middleware must not run).

Source

pub async fn bail<E, T>(&self, event: &E) -> Option<T>
where E: Any + Send + Sync, T: Send + 'static,

Runs bail handlers of (E, T) in registration order until the first Some value; None if every handler deferred.

Trait Implementations§

Source§

impl Default for Ctx

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for Ctx

§

impl Freeze for Ctx

§

impl Move for Ctx

§

impl RefUnwindSafe for Ctx

§

impl Send for Ctx

§

impl Sync for Ctx

§

impl Unpin for Ctx

§

impl UnsafeUnpin for Ctx

§

impl UnwindSafe for Ctx

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Service for T
where T: Any + Send + Sync + ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more