pub struct ServiceCollection { /* private fields */ }Implementations§
Source§impl ServiceCollection
impl ServiceCollection
pub fn new() -> Self
pub fn singleton<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
pub fn transient<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
pub fn scoped<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Sourcepub fn keyed_singleton<T: ?Sized + Send + Sync + 'static>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn keyed_singleton<T: ?Sized + Send + Sync + 'static>( self, k: impl Into<ServiceKey>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register a keyed singleton service.
String keys (keyed_singleton("name", …)) and typed keys
(keyed_singleton(ServiceKey::new(MyKey::A), …)) are both accepted.
Sourcepub fn keyed_transient<T: ?Sized + Send + Sync + 'static>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn keyed_transient<T: ?Sized + Send + Sync + 'static>( self, k: impl Into<ServiceKey>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register a keyed transient service (new instance each resolution).
Sourcepub fn keyed_scoped<T: ?Sized + Send + Sync + 'static>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn keyed_scoped<T: ?Sized + Send + Sync + 'static>( self, k: impl Into<ServiceKey>, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register a keyed scoped service (shared within a scope).
Sourcepub fn keyed_any_singleton<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn keyed_any_singleton<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register an AnyKey fallback singleton (MEDI KeyedService.AnyKey).
The factory receives the requested key and one instance is created and cached per requested key. Exact-key registrations take precedence.
Sourcepub fn keyed_any_scoped<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn keyed_any_scoped<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register an AnyKey fallback scoped service (one instance per requested
key per scope).
Sourcepub fn keyed_any_transient<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn keyed_any_transient<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver, &ServiceKey) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register an AnyKey fallback transient service (fresh instance per
resolution, receives the requested key).
Sourcepub fn async_singleton<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> Self
pub fn async_singleton<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> Self
Register an async singleton service (initialized once during build_async).
The factory receives Arc<ServiceProvider>. To resolve services from
within the factory, use get_async; to propagate the resolution context
to spawned tasks (enabling cross-task cycle detection), call
ServiceProvider::async_context.
Sourcepub fn async_transient<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> Self
pub fn async_transient<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> Self
Register an async transient service (new async instance each resolution).
Sourcepub fn async_scoped<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> Self
pub fn async_scoped<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> Self
Register an async scoped service (shared within a scope).
Sourcepub fn async_keyed_singleton<T: ?Sized + Send + Sync + 'static>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> Self
pub fn async_keyed_singleton<T: ?Sized + Send + Sync + 'static>( self, k: impl Into<ServiceKey>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> Self
Register an async keyed singleton service.
Sourcepub fn async_keyed_transient<T: ?Sized + Send + Sync + 'static>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> Self
pub fn async_keyed_transient<T: ?Sized + Send + Sync + 'static>( self, k: impl Into<ServiceKey>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> Self
Register an async keyed transient service.
Sourcepub fn async_keyed_scoped<T: ?Sized + Send + Sync + 'static>(
self,
k: impl Into<ServiceKey>,
f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static,
) -> Self
pub fn async_keyed_scoped<T: ?Sized + Send + Sync + 'static>( self, k: impl Into<ServiceKey>, f: impl Fn(Arc<ServiceProvider>) -> Pin<Box<dyn Future<Output = Arc<T>> + Send>> + Send + Sync + 'static, ) -> Self
Register an async keyed scoped service.
pub fn try_add<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Sourcepub fn try_add_singleton<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn try_add_singleton<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register a singleton only if no unkeyed registration for T exists yet.
Sourcepub fn try_add_scoped<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn try_add_scoped<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register a scoped service only if no unkeyed registration for T exists yet.
Sourcepub fn try_add_transient<T: ?Sized + Send + Sync + 'static>(
self,
f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static,
) -> Self
pub fn try_add_transient<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
Register a transient service only if no unkeyed registration for T exists yet.
pub fn add<T: ?Sized + Send + Sync + 'static>( self, lt: ServiceLifetime, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self
pub fn instance<T: Send + Sync + 'static>(self, v: Arc<T>) -> Self
pub fn singleton_value<T: Send + Sync + 'static>(self, v: T) -> Self
pub fn build(self) -> Result<Arc<ServiceProvider>, RdiError>
Sourcepub fn build_with(
self,
options: ServiceProviderOptions,
) -> Result<Arc<ServiceProvider>, RdiError>
pub fn build_with( self, options: ServiceProviderOptions, ) -> Result<Arc<ServiceProvider>, RdiError>
Build with explicit ServiceProviderOptions.
build() equals build_with(ServiceProviderOptions::default()), i.e.
strict scope validation is enabled by default (MEDI ValidateScopes).
Use crate::ServiceProviderOptions::lenient to restore the ≤ 0.7
root-scope behavior where the root provider caches scoped services.
Sourcepub async fn build_async(self) -> Result<Arc<ServiceProvider>, RdiError>
pub async fn build_async(self) -> Result<Arc<ServiceProvider>, RdiError>
Build the provider, executing async factories for singleton services.
Async singleton factories are run during build and their results are cached as singleton instances. Async transient/scoped factories are stored for later resolution.
Use this instead of build when any service requires
async initialization (e.g., database connections, remote config).
Sourcepub async fn build_async_with(
self,
options: ServiceProviderOptions,
) -> Result<Arc<ServiceProvider>, RdiError>
pub async fn build_async_with( self, options: ServiceProviderOptions, ) -> Result<Arc<ServiceProvider>, RdiError>
Async variant of build_with.
Sourcepub fn from_injected() -> Self
pub fn from_injected() -> Self
Build a ServiceCollection from all #[rust_dix::inject] annotations
in the current binary (across all crates).