Skip to main content

Container

Struct Container 

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

Type-indexed dependency container.

Implementations§

Source§

impl Container

Source

pub fn new() -> Self

Creates an empty container.

Source

pub fn request_scope(&self) -> RequestScope<'_>

Creates a request scope for request-lifetime providers.

Source

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

Registers a concrete singleton value.

Source

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

Replaces a singleton provider, intended for explicit test overrides.

Source

pub fn register_factory<T, F>( &mut self, lifetime: ProviderLifetime, factory: F, ) -> Result<()>
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Registers a provider factory.

Source

pub fn register_singleton_factory<T, F>(&mut self, factory: F) -> Result<()>
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Registers a singleton provider factory.

Source

pub fn register_transient<T, F>(&mut self, factory: F) -> Result<()>
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Registers a transient provider factory.

Source

pub fn register_request<T, F>(&mut self, factory: F) -> Result<()>
where T: Send + Sync + 'static, F: Fn(&Container) -> Result<T> + Send + Sync + 'static,

Registers a request-lifetime provider factory.

Source

pub fn register_request_scoped<T, F>(&mut self, factory: F) -> Result<()>
where T: Send + Sync + 'static, F: for<'scope> Fn(&RequestScope<'scope>) -> Result<T> + Send + Sync + 'static,

Registers a request-lifetime provider factory that resolves dependencies through the active request scope.

Source

pub fn inject<T>(&self) -> Result<Inject<T>>
where T: Send + Sync + 'static,

Resolves a typed dependency reference.

Source

pub fn optional<T>(&self) -> Result<Optional<T>>
where T: Send + Sync + 'static,

Resolves an optional typed dependency reference.

Missing providers become Optional::new(None), while registered providers that fail to construct still return their original error.

Source

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

Resolves a shared typed dependency.

Source

pub fn eagerly_resolve_singletons(&self) -> Result<()>

Eagerly constructs every registered singleton provider and caches it.

Singletons are otherwise constructed lazily on first resolution, which uses a blocking Condvar wait when two callers race to construct the same provider. Calling this at startup pre-constructs each singleton so later resolutions (including from async request handlers) hit the cached value and never reach that wait, avoiding an async-runtime worker stalling on first use. Transient and request-lifetime providers are skipped.

A singleton whose factory errors or panics will do so here, failing startup fast instead of on first request.

Trait Implementations§

Source§

impl Default for Container

Source§

fn default() -> Container

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Provider for T
where T: Send + Sync + 'static,

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more