Skip to main content

ServiceProvider

Struct ServiceProvider 

Source
pub struct ServiceProvider { /* private fields */ }

Implementations§

Source§

impl ServiceProvider

Source

pub fn register_disposable(&self, cleanup: Box<dyn FnOnce() + Send>)

Register a cleanup closure for scoped services/owned resources.

Closures run (in reverse registration order) when Self::dispose is called — either explicitly or by Scope’s Drop for scope providers.

Source

pub fn dispose(&self)

Dispose this provider: release cached instances in reverse creation order, run registered cleanups in reverse registration order, and mark the provider unusable.

  • Root provider: also marks the whole provider tree disposed (existing scopes can no longer resolve), drains the singleton cache, and clears named registrations. Singleton instances are released in reverse creation order (LIFO), so dependents drop before their dependencies.
  • Scope provider: releases the scoped instances created in that scope and runs its cleanups. Singletons are untouched.

Idempotent. Instances are released by dropping the container’s strong references; a service’s Drop runs when no other owner keeps it alive. Services obtained via get_owned are caller-owned and are not tracked.

The caller must not resolve services concurrently with disposal (same contract as MEDI’s ServiceProvider.Dispose). A panicking cleanup closure does not stop the remaining cleanups; the first panic is re-raised after disposal (and swallowed when the thread is already unwinding, e.g. during Scope’s Drop).

Source

pub fn get<T: ?Sized + Send + Sync + 'static>(&self) -> Result<Arc<T>, RdiError>

Resolve a service by type. Works uniformly for concrete types and trait objects. Returns Err(RdiError::ServiceNotFound) if not registered.

ScopeFactory is always available from a built provider (no manual registration required). An explicit singleton::<dyn ScopeFactory>(...) registration still takes precedence. Later registrations override earlier ones (MEDI semantics).

Source

pub fn get_optional<T: ?Sized + Send + Sync + 'static>(&self) -> Option<Arc<T>>

Resolve a service by type, returning None if it is not registered or not resolvable at this point (e.g. a scoped service under root scope validation, or a disposed provider). Use Self::get when you need the concrete error.

Source

pub async fn get_async<T: ?Sized + Send + Sync + 'static>( &self, ) -> Result<Arc<T>, RdiError>

Resolve an async service (transient or scoped) by type.

  • Singletons are resolved synchronously (already cached during build_async).
  • Scoped services are cached in this provider’s scoped cache (one instance per scope).
  • Transient services call the async factory fresh each time.
Source

pub async fn get_keyed_async<T: ?Sized + Send + Sync + 'static>( &self, key: &str, ) -> Result<Arc<T>, RdiError>

Resolve an async keyed service by type and key.

Source

pub fn get_keyed<T: ?Sized + Send + Sync + 'static>( &self, key: &str, ) -> Result<Arc<T>, RdiError>

Resolve a keyed service by type and key. Returns Err(RdiError::KeyedServiceNotFound) if not found.

Source

pub fn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>

Resolve owned T (bypass cache, fresh each call). Returns Err if not registered or the service is Singleton.

Owned resolution hands the instance to the caller: it is not tracked by the container and stays valid after dispose. Scoped ownership bypasses the scope cache, so a root scope-validation error is not raised for the owned instance itself; its dependencies still obey scope validation.

Source

pub fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>

Resolve owned T, returning None if not registered, a Singleton, or the provider/scope is disposed.

Source

pub fn get_keyed_owned<T: Send + Sync + 'static>( &self, key: &str, ) -> Result<T, RdiError>

Resolve owned keyed T. Returns Err if not found or Singleton.

Source

pub fn try_get_keyed_owned<T: Send + Sync + 'static>( &self, key: &str, ) -> Option<T>

Resolve owned keyed T, returning None if not found, a Singleton, or the provider/scope is disposed.

Source

pub fn get_all<T: ?Sized + Send + Sync + 'static>(&self) -> Vec<Arc<T>>

Return all unkeyed registered instances of the given type, in registration order (MEDI GetServices<T>() / IEnumerable<T> parity).

Keyed implementations are excluded — use Self::get_all_keyed for those. Under root scope validation, scoped services are not resolvable at the root and are omitted; other resolution failures panic with a typed RdiError payload (mirroring MEDI, where enumeration throws).

Source

pub fn get_all_keyed<T: ?Sized + Send + Sync + 'static>(&self) -> Vec<Arc<T>>

Return all keyed registered instances of the given type, in registration order (MEDI GetKeyedServices<T>(KeyedService.AnyKey) parity).

Source

pub fn scope(self: &Arc<Self>) -> Scope

Create a new service scope.

Analogous to IServiceProvider.CreateScope() in MEDI. Scoped-lifetime services are cached within the returned scope.

Source

pub fn is_registered<T: ?Sized + Send + Sync + 'static>(&self) -> bool

Whether T has a resolvable registration (or is a built-in container service). MEDI IServiceProviderIsService parity.

Reports registration state only: it is still true after Self::dispose.

Source

pub fn is_registered_keyed<T: ?Sized + Send + Sync + 'static>( &self, key: &str, ) -> bool

Whether T has a registration under key.

Source

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

Cross-DLL safe named service resolution (generic).

Source

pub fn get_named_any(&self, name: &str) -> Option<Arc<dyn Any + Send + Sync>>

Non-generic named resolution; returns Arc<dyn Any> for trait-object dispatch.

Source

pub fn register_named<T: Send + Sync + 'static>( &self, name: &str, service: Arc<T>, )

Register a named service for cross-DLL plugin access.

No-op after the provider/scope has been disposed (the named registry is cleared by Self::dispose).

Source

pub fn remove_named(&self, name: &str)

Remove a named service (for plugin unload).

Source

pub fn rdi_register_named( &self, name: &str, service: Arc<dyn Any + Send + Sync>, )

Register a named service (for IProvider trait).

No-op after the provider/scope has been disposed.

Source

pub fn rdi_remove_named(&self, name: &str)

Remove a named service (for IProvider trait).

Trait Implementations§

Source§

impl IProvider for ServiceProvider

Source§

fn get_named_any(&self, name: &str) -> Option<Arc<dyn Any + Send + Sync>>

Source§

fn rdi_register_named(&self, name: &str, service: Arc<dyn Any + Send + Sync>)

Source§

fn rdi_remove_named(&self, name: &str)

Source§

impl IServiceResolver for ServiceProvider

Source§

fn try_get_any_result( &self, key: &str, ) -> Result<Option<Arc<dyn Any + Send + Sync>>, RdiError>

Infallible lookup: reports absence instead of panicking for scope violations / disposal (still returns Err for factory failures so callers could distinguish, but IServiceResolver::try_get maps it to None).

Source§

fn try_get_keyed_any_result( &self, key: &str, variant: &str, ) -> Result<Option<Arc<dyn Any + Send + Sync>>, RdiError>

Source§

fn get_any(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>

Source§

fn get_keyed_any( &self, key: &str, variant: &str, ) -> Option<Arc<dyn Any + Send + Sync>>

Source§

fn get_by_type_id(&self, tid: TypeId) -> Option<Arc<dyn Any + Send + Sync>>

TypeId 直查(默认 None,ServiceProvider 等覆盖为 O(1) 直查)。 Read more
Source§

fn get_keyed_by_type_id( &self, tid: TypeId, key: &str, ) -> Option<Arc<dyn Any + Send + Sync>>

TypeId 直查 keyed 版本(默认 None)。 Read more
Source§

fn get_all_any(&self, key: &str) -> Vec<Arc<dyn Any + Send + Sync>>

Type-erased polymorphic resolution: return all unkeyed registered instances of the given type, in registration order. Read more
Source§

fn get_all_keyed_any(&self, key: &str) -> Vec<Arc<dyn Any + Send + Sync>>

Type-erased polymorphic resolution over keyed registrations only (MEDI GetKeyedServices<T>(KeyedService.AnyKey)). Read more
Source§

fn get_all_any_result( &self, key: &str, ) -> Result<Vec<Arc<dyn Any + Send + Sync>>, RdiError>

Error-reporting variant of Self::get_all_any, used by the inject macros for Vec<Arc<T>> fields. Read more
Source§

fn get_owned_any(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>

Type-erased owned resolution: bypass cache, call factory fresh, return Arc<dyn Any + Send + Sync> (concrete Arc<Arc<T>>). Read more
Source§

fn get_keyed_owned_any( &self, key: &str, variant: &str, ) -> Option<Arc<dyn Any + Send + Sync>>

Source§

fn provider_arc(&self) -> Option<Arc<ServiceProvider>>

Return an Arc<ServiceProvider> for on-demand service resolution. Read more
Source§

fn get<T: ?Sized + Sync + Send + 'static>(&self) -> Result<Arc<T>, RdiError>
where Self: Sized,

Resolve a service by type (concrete or dyn Trait). Returns Err(RdiError::ServiceNotFound) if not registered. Read more
Source§

fn try_get<T: ?Sized + Sync + Send + 'static>(&self) -> Option<Arc<T>>
where Self: Sized,

Resolve a service by type, returning None if it is not registered or not resolvable at this point (e.g. a scoped service under root scope validation, or a disposed provider/scope). Read more
Source§

fn get_keyed<T: ?Sized + Sync + Send + 'static>( &self, variant: &str, ) -> Result<Arc<T>, RdiError>
where Self: Sized,

Resolve a keyed service by type and key. Returns Err if not found.
Source§

fn try_get_keyed<T: ?Sized + Sync + Send + 'static>( &self, variant: &str, ) -> Option<Arc<T>>
where Self: Sized,

Resolve a keyed service by type and key, returning None if not found or not resolvable at this point (e.g. root scope validation, disposed provider/scope). Never panics for framework-level failures.
Source§

fn get_all<T: ?Sized + Sync + Send + 'static>(&self) -> Vec<Arc<T>>
where Self: Sized,

Resolve all unkeyed registered instances of T, in registration order. Read more
Source§

fn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>
where Self: Sized,

Resolve owned T (bypass cache, fresh each call). Returns Err if not registered or the service is Singleton.
Source§

fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>
where Self: Sized,

Resolve owned T, returning None if not registered or Singleton.
Source§

fn get_keyed_owned<T: Send + Sync + 'static>( &self, variant: &str, ) -> Result<T, RdiError>
where Self: Sized,

Resolve owned keyed T. Returns Err if not found or Singleton.
Source§

fn try_get_keyed_owned<T: Send + Sync + 'static>( &self, variant: &str, ) -> Option<T>
where Self: Sized,

Resolve owned keyed T, returning None if not found or Singleton.
Source§

impl ScopeFactory for ServiceProvider

Source§

fn create_scope(&self) -> Scope

创建一个独立 scope,共享 root 的 store/singleton_cache/named, 独立 scoped 缓存和 disposables。

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> 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, 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.