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>(&self, value: T)
pub fn instance<T>(&self, value: T)
Register an already-built value. The container never constructs it.
Sourcepub fn instance_arc<T>(&self, value: Arc<T>)
pub fn instance_arc<T>(&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>(&self) -> Result<Arc<T>, Error>
pub fn resolve<T>(&self) -> Result<Arc<T>, Error>
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>(&self) -> Option<Arc<T>>
pub fn try_resolve<T>(&self) -> Option<Arc<T>>
Resolve T, or None if it is unbound or its factory failed.
Sourcepub fn expect_resolve<T>(&self) -> Arc<T> ⓘ
pub fn expect_resolve<T>(&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.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Container
impl RefUnwindSafe for Container
impl Send for Container
impl Sync for Container
impl Unpin for Container
impl UnsafeUnpin for Container
impl UnwindSafe for Container
Blanket Implementations§
impl<T> Actor for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Event for T
impl<T> Event for T
Source§fn event_name() -> &'static strwhere
Self: Sized,
fn event_name() -> &'static strwhere
Self: Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more