pub struct Container { /* private fields */ }Expand description
A registry of services keyed by their Rust type.
Cheap to share: put it in an Arc and clone the handle. All methods take
&self — bindings are registered through interior mutability, because
service providers register into a container that is already shared.
Implementations§
Source§impl Container
impl Container
Sourcepub fn singleton<T, F>(&self, factory: F)
pub fn singleton<T, F>(&self, factory: F)
Bind a factory that runs once; every resolution afterwards hands
back the same Arc.
Sourcepub fn scoped<T, F>(&self, factory: F)
pub fn scoped<T, F>(&self, factory: F)
Like singleton, but flush_scoped drops the
memoised instance so the next resolution rebuilds it.
This is the binding a long-running worker wants for per-job state: the queue worker flushes scoped bindings between jobs so one job cannot leak state into the next.
Sourcepub fn instance<T: Send + Sync + 'static>(&self, value: T)
pub fn instance<T: Send + Sync + 'static>(&self, value: T)
Register an already-built value. The container never constructs it.
Sourcepub fn instance_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)
pub fn instance_arc<T: Send + Sync + 'static>(&self, value: Arc<T>)
Register an already-built value that is already behind an Arc —
useful when the caller must keep a handle to it too.
Sourcepub fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>
pub fn resolve<T: Send + Sync + 'static>(&self) -> Result<Arc<T>>
Resolve T, building it if necessary.
Fails when T was never bound, when its factory fails, or when
resolving it re-enters itself (a dependency cycle).
Sourcepub fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn try_resolve<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
Resolve T, or None if it is unbound or its factory failed.
Sourcepub fn expect_resolve<T: Send + Sync + 'static>(&self) -> Arc<T> ⓘ
pub fn expect_resolve<T: Send + Sync + 'static>(&self) -> Arc<T> ⓘ
Resolve T, panicking with the resolution error if it is not
available. For bootstrap code and facades, where an unbound service is
a configuration bug rather than a runtime condition.
Sourcepub fn flush_scoped(&self)
pub fn flush_scoped(&self)
Drop the memoised instance of every scoped binding.
The bindings survive; only the built values are discarded.