Skip to main content

ServiceCollection

Struct ServiceCollection 

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

Implementations§

Source§

impl ServiceCollection

Source

pub fn new() -> Self

Source

pub fn singleton<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self

Source

pub fn transient<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self

Source

pub fn scoped<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self

Source

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.

Source

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

Source

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

Source

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.

Source

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

Source

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

Source

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.

Source

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

Source

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

Source

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.

Source

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.

Source

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.

Source

pub fn try_add<T: ?Sized + Send + Sync + 'static>( self, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self

Source

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.

Source

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.

Source

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.

Source

pub fn add<T: ?Sized + Send + Sync + 'static>( self, lt: ServiceLifetime, f: impl Fn(&dyn IServiceResolver) -> Arc<T> + Send + Sync + 'static, ) -> Self

Source

pub fn instance<T: Send + Sync + 'static>(self, v: Arc<T>) -> Self

Source

pub fn singleton_value<T: Send + Sync + 'static>(self, v: T) -> Self

Source

pub fn build(self) -> Result<Arc<ServiceProvider>, RdiError>

Source

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.

Source

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

Source

pub async fn build_async_with( self, options: ServiceProviderOptions, ) -> Result<Arc<ServiceProvider>, RdiError>

Async variant of build_with.

Source

pub fn from_injected() -> Self

Build a ServiceCollection from all #[rust_dix::inject] annotations in the current binary (across all crates).

Trait Implementations§

Source§

impl Default for ServiceCollection

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