pub trait IServiceResolver: Send + Sync {
Show 17 methods
// Required methods
fn get_any(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>;
fn get_keyed_any(
&self,
key: &str,
variant: &str,
) -> Option<Arc<dyn Any + Send + Sync>>;
// Provided methods
fn get_by_type_id(&self, _tid: TypeId) -> Option<Arc<dyn Any + Send + Sync>> { ... }
fn get_keyed_by_type_id(
&self,
_tid: TypeId,
_key: &str,
) -> Option<Arc<dyn Any + Send + Sync>> { ... }
fn get_all_any(&self, _key: &str) -> Vec<Arc<dyn Any + Send + Sync>> { ... }
fn provider_arc(&self) -> Option<Arc<ServiceProvider>> { ... }
fn get<T: ?Sized + Sync + Send + 'static>(&self) -> Result<Arc<T>, RdiError>
where Self: Sized { ... }
fn try_get<T: ?Sized + Sync + Send + 'static>(&self) -> Option<Arc<T>>
where Self: Sized { ... }
fn get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Result<Arc<T>, RdiError>
where Self: Sized { ... }
fn try_get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Option<Arc<T>>
where Self: Sized { ... }
fn get_all<T: ?Sized + Sync + Send + 'static>(&self) -> Vec<Arc<T>>
where Self: Sized { ... }
fn get_owned_any(&self, _key: &str) -> Option<Arc<dyn Any + Send + Sync>> { ... }
fn get_keyed_owned_any(
&self,
_key: &str,
_variant: &str,
) -> Option<Arc<dyn Any + Send + Sync>> { ... }
fn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>
where Self: Sized { ... }
fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>
where Self: Sized { ... }
fn get_keyed_owned<T: Send + Sync + 'static>(
&self,
variant: &str,
) -> Result<T, RdiError>
where Self: Sized { ... }
fn try_get_keyed_owned<T: Send + Sync + 'static>(
&self,
variant: &str,
) -> Option<T>
where Self: Sized { ... }
}Expand description
Service resolver trait — the core DI resolution interface.
Provides both type-erased and generic resolution methods.
Required Methods§
fn get_any(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>
fn get_keyed_any( &self, key: &str, variant: &str, ) -> Option<Arc<dyn Any + Send + Sync>>
Provided Methods§
Sourcefn get_by_type_id(&self, _tid: TypeId) -> Option<Arc<dyn Any + Send + Sync>>
fn get_by_type_id(&self, _tid: TypeId) -> Option<Arc<dyn Any + Send + Sync>>
TypeId 直查(默认 None,ServiceProvider 等覆盖为 O(1) 直查)。
外部实现者若需优化可覆盖;不覆盖则 Self::get / Self::try_get
等默认实现 fallback 到 Self::get_any 字符串路径。
Sourcefn get_keyed_by_type_id(
&self,
_tid: TypeId,
_key: &str,
) -> Option<Arc<dyn Any + Send + Sync>>
fn get_keyed_by_type_id( &self, _tid: TypeId, _key: &str, ) -> Option<Arc<dyn Any + Send + Sync>>
TypeId 直查 keyed 版本(默认 None)。
Sourcefn get_all_any(&self, _key: &str) -> Vec<Arc<dyn Any + Send + Sync>>
fn get_all_any(&self, _key: &str) -> Vec<Arc<dyn Any + Send + Sync>>
Type-erased polymorphic resolution: return all registered instances (default + keyed) of the given type.
Used by Vec<Arc<T>> field injection (#[inject] on Vec<Arc<T>>)
to collect all implementations — equivalent to C# MEDI
IEnumerable<IFoo> constructor injection.
Default empty Vec keeps external implementors backward-compatible.
Sourcefn provider_arc(&self) -> Option<Arc<ServiceProvider>>
fn provider_arc(&self) -> Option<Arc<ServiceProvider>>
Return an Arc<ServiceProvider> for on-demand service resolution.
Used by #[inject(provider)] to give a service access to the DI container
so it can resolve scoped/owned instances on-demand (e.g., per-request
DbContext in a web handler).
Returns None if the resolver is not backed by a ServiceProvider
(e.g., ValidationResolver during build-time validation).
Default None keeps external implementors backward-compatible.
Sourcefn get<T: ?Sized + Sync + Send + 'static>(&self) -> Result<Arc<T>, RdiError>where
Self: Sized,
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.
优先走 get_by_type_id(O(1) TypeId 直查),未覆盖时 fallback
到 get_any 字符串路径。
Sourcefn try_get<T: ?Sized + Sync + Send + 'static>(&self) -> Option<Arc<T>>where
Self: Sized,
fn try_get<T: ?Sized + Sync + Send + 'static>(&self) -> Option<Arc<T>>where
Self: Sized,
Resolve a service by type, returning None if not registered.
Sourcefn get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Result<Arc<T>, RdiError>where
Self: Sized,
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.
Sourcefn try_get_keyed<T: ?Sized + Sync + Send + 'static>(
&self,
variant: &str,
) -> Option<Arc<T>>where
Self: Sized,
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.
Sourcefn get_all<T: ?Sized + Sync + Send + 'static>(&self) -> Vec<Arc<T>>where
Self: Sized,
fn get_all<T: ?Sized + Sync + Send + 'static>(&self) -> Vec<Arc<T>>where
Self: Sized,
Resolve all registered instances of T (default + keyed).
Equivalent to C# MEDI GetServices<T>(). Types implementing IServiceResolver
get this for free via get_all_any; types with an inherent get_all<T>
method (e.g. ServiceProvider, Scope) use that instead — Rust’s method
resolution gives inherent methods priority over trait methods.
Sourcefn get_owned_any(&self, _key: &str) -> Option<Arc<dyn Any + Send + Sync>>
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>>).
- Singleton →
None(shared singleton cannot be owned). - Scoped/Transient → factory invoked directly (no cache read/write),
caller downcasts +
Arc::try_unwrapto obtain ownedT.
Default None keeps external implementors backward-compatible.
fn get_keyed_owned_any( &self, _key: &str, _variant: &str, ) -> Option<Arc<dyn Any + Send + Sync>>
Sourcefn get_owned<T: Send + Sync + 'static>(&self) -> Result<T, RdiError>where
Self: Sized,
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.
Sourcefn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>where
Self: Sized,
fn try_get_owned<T: Send + Sync + 'static>(&self) -> Option<T>where
Self: Sized,
Resolve owned T, returning None if not registered or Singleton.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".