Skip to main content

Context

Struct Context 

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

Root and child dependency containers for Cordis plugins.

Cloning a context is cheap. Child contexts share all runtime services while carrying immutable scope/intercept/metadata overlays and the current fiber.

Implementations§

Source§

impl Context

Source

pub fn new() -> Self

Create a root context and install the core reflection, registry, event, and logger services.

Source

pub fn same_root(&self, other: &Context) -> bool

Return whether two contexts belong to the same root application.

Source

pub fn root(&self) -> Context

Return the root context.

Source

pub fn fiber(&self) -> Result<Fiber>

Return this context’s owning plugin fiber.

Source

pub fn base_url(&self) -> Option<&str>

Return the base URL inherited by this context, when set.

Source

pub fn with_base_url(&self, base_url: impl Into<Arc<str>>) -> Context

Derive a child context with a new base URL.

Source

pub fn extend<T>(&self, name: impl Into<String>, value: T) -> Context
where T: Send + Sync + 'static,

Derive a child context carrying arbitrary metadata.

Source

pub fn extend_value(&self, name: impl Into<String>, value: Value) -> Context

Derive a child context carrying type-erased metadata.

Source

pub fn metadata<T>(&self, name: &str) -> Result<Option<Arc<T>>>
where T: Send + Sync + 'static,

Read typed context metadata.

Source

pub fn new_isolation(&self) -> Isolation

Allocate a globally unique isolation label.

Source

pub fn isolate(&self, name: impl Into<String>) -> Context

Create a child context with a fresh independent service scope for name.

Source

pub fn isolate_with(&self, name: impl Into<String>, label: Isolation) -> Context

Create a child context using a supplied scope label. Reusing the label joins otherwise separate context branches to the same service scope.

A scope holds one implementation per slot, and every name isolated with the same label maps to that one slot: only one of those names can be provided there, and providing a second fails with DuplicateService.

Source

pub fn isolate_many( &self, names: impl IntoIterator<Item = impl Into<String>>, label: Option<Isolation>, ) -> Context

Create a child context with several service names isolated together.

All names share one scope label and therefore one implementation slot, matching the upstream single-slot scope model: provide exactly one of them in this branch — providing a second name fails with DuplicateService — and inject only that name from sibling plugins. Use separate labels (or isolate) when several of the names need independent implementations.

Source

pub fn intercept<T>(&self, name: impl Into<String>, config: T) -> Context
where T: Send + Sync + 'static,

Add service-specific intercept configuration below this context.

Source

pub fn intercept_value(&self, name: impl Into<String>, config: Value) -> Context

Add type-erased intercept configuration.

Source

pub fn intercepts<T>(&self, name: &str) -> Result<Vec<Arc<T>>>
where T: Send + Sync + 'static,

Return typed intercept configs in ancestor-to-descendant order.

Source

pub fn with_filter<F>(&self, filter: F) -> Context
where F: Fn(&Context) -> bool + Send + Sync + 'static,

Attach an event listener filter to a derived context.

Source

pub fn events(&self) -> EventsService

Return the events service bound to this context.

Source

pub fn reflect(&self) -> ReflectService

Return the reflection/service store bound to this context.

Source

pub fn registry(&self) -> RegistryService

Return the plugin registry bound to this context.

Source

pub fn logger(&self) -> Logger

Return a logger named from the current fiber and intercepts.

Source

pub fn named_logger(&self, name: impl Into<String>) -> Logger

Return an explicitly named logger.

Source

pub fn logger_service(&self) -> LoggerService

Return the logger service bound to this context.

Source

pub fn effect<F>( &self, label: impl Into<String>, dispose: F, ) -> Result<EffectHandle>
where F: FnOnce() -> Result<()> + Send + 'static,

Register a synchronous cleanup operation on the current fiber.

Source

pub fn effect_infallible<F>( &self, label: impl Into<String>, dispose: F, ) -> Result<EffectHandle>
where F: FnOnce() + Send + 'static,

Register an infallible synchronous cleanup operation.

Source

pub fn effect_async<F, Fut>( &self, label: impl Into<String>, dispose: F, ) -> Result<EffectHandle>
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register an asynchronous cleanup operation.

Source

pub fn provide<T>( &self, name: impl Into<String>, value: T, ) -> Result<EffectHandle>
where T: Send + Sync + 'static,

Provide a concrete service in this context’s isolation scope.

Source

pub fn provide_arc<T>( &self, name: impl Into<String>, value: Arc<T>, ) -> Result<EffectHandle>
where T: Send + Sync + 'static,

Provide an existing Arc without wrapping it in a second Arc.

Source

pub fn get<T>(&self, name: &str) -> Result<Option<Arc<T>>>
where T: Send + Sync + 'static,

Read a service without enforcing an inject declaration.

Source

pub fn get_unchecked<T>(&self, name: &str) -> Result<Option<Arc<T>>>
where T: Send + Sync + 'static,

Read a service even while its provider is loading or unloading.

Source

pub fn require<T>(&self, name: &str) -> Result<Arc<T>>
where T: Send + Sync + 'static,

Require a currently active service.

Source

pub fn set<T>(&self, name: &str, value: T) -> Result<()>
where T: Send + Sync + 'static,

Replace a service value. Only its providing fiber may do this.

Source

pub fn notify<I, S>(&self, names: I) -> Vec<Fiber>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Re-evaluate dependency availability for the named services.

Source

pub fn accessor( &self, name: impl Into<String>, accessor: Accessor, ) -> Result<EffectHandle>

Register a dynamic computed property.

Source

pub fn on<F>( &self, name: impl Into<String>, listener: F, ) -> Result<EffectHandle>
where F: Fn(Event) -> EventResult + Send + Sync + 'static,

Register an event listener owned by this context’s fiber.

Source

pub fn on_with<F>( &self, name: impl Into<String>, listener: F, options: EventOptions, ) -> Result<EffectHandle>
where F: Fn(Event) -> EventResult + Send + Sync + 'static,

Register a listener with placement and filtering options.

Source

pub fn on_async<F, Fut>( &self, name: impl Into<String>, listener: F, ) -> Result<EffectHandle>
where F: Fn(Event) -> Fut + Send + Sync + 'static, Fut: Future<Output = EventResult> + Send + 'static,

Register an asynchronous event listener.

Source

pub fn once<F>( &self, name: impl Into<String>, listener: F, ) -> Result<EffectHandle>
where F: Fn(Event) -> EventResult + Send + Sync + 'static,

Register a one-shot event listener.

Source

pub fn emit( &self, name: impl Into<String>, args: impl IntoIterator<Item = EventValue>, ) -> Result<()>

Emit an event synchronously.

Source

pub async fn parallel( &self, name: impl Into<String>, args: impl IntoIterator<Item = EventValue>, ) -> Result<()>

Run all matching listeners concurrently.

Source

pub async fn serial( &self, name: impl Into<String>, args: impl IntoIterator<Item = EventValue>, ) -> EventResult

Await listeners in order until one returns a bail value.

Source

pub fn bail( &self, name: impl Into<String>, args: impl IntoIterator<Item = EventValue>, ) -> EventResult

Run listeners synchronously until one returns a bail value.

Source

pub fn waterfall<F>( &self, name: impl Into<String>, args: impl IntoIterator<Item = EventValue>, inner: F, ) -> EventResult
where F: Fn() -> EventResult + Send + Sync + 'static,

Compose listeners around an innermost synchronous callback.

Source

pub fn plugin<P, C>(&self, plugin: P, config: C) -> Fiber
where P: IntoPlugin, C: Send + Sync + 'static,

Start a plugin and return its lifecycle fiber.

Source

pub fn plugin_default<P>(&self, plugin: P) -> Fiber
where P: IntoPlugin,

Start a plugin with unit configuration.

Source

pub fn plugin_object<P, C>(&self, plugin: P, config: C) -> Fiber
where P: Plugin, C: Send + Sync + 'static,

Wrap and start a concrete Plugin implementation.

Source

pub fn plugin_object_default<P>(&self, plugin: P) -> Fiber
where P: Plugin,

Start a concrete plugin implementation with unit configuration.

Source

pub fn inject<F>(&self, inject: Inject, callback: F) -> Fiber
where F: Fn(Context) -> Result<PluginOutput> + Send + Sync + 'static,

Start an inline callback after all listed services become available.

Source

pub fn log_error(&self, error: impl ToString)

Log an error through the current context logger.

Source§

impl Context

Source

pub fn provide_service<S>(&self, service: S) -> Result<EffectHandle>
where S: Service,

Provide a typed service under Service::NAME.

Source

pub fn provide_service_arc<S>(&self, service: Arc<S>) -> Result<EffectHandle>
where S: Service,

Provide an existing shared typed service.

Source

pub fn resolve_service_config<T, F>( &self, name: &str, base: T, merge: F, ) -> Result<T>
where T: Send + Sync + 'static, F: FnMut(T, &T) -> T,

Resolve service intercepts from root to leaf with a caller-supplied merge operation.

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Context

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.