pub struct Container { /* private fields */ }Expand description
A type-keyed service container for dependency injection.
See the module documentation for usage examples.
Implementations§
Source§impl Container
impl Container
Sourcepub fn register<T: Send + Sync + 'static>(&mut self, service: T) -> &mut Self
pub fn register<T: Send + Sync + 'static>(&mut self, service: T) -> &mut Self
Register a concrete service by value.
Wraps service in Arc<T> and stores it under TypeId::of::<T>().
A subsequent registration for the same T replaces the previous one.
Sourcepub fn provide<T>(&mut self, service: Arc<T>) -> &mut Self
pub fn provide<T>(&mut self, service: Arc<T>) -> &mut Self
Register a pre-built Arc<T>.
Required when registering trait objects, because the concrete type must be erased before storage:
let mut c = Container::new();
c.provide::<dyn Greeter>(Arc::new(Hello));
assert_eq!(c.get::<dyn Greeter>().unwrap().greet(), "hi");Also usable for concrete types when you already have an Arc.
Sourcepub fn register_named<T: Send + Sync + 'static>(
&mut self,
name: impl Into<String>,
service: T,
) -> &mut Self
pub fn register_named<T: Send + Sync + 'static>( &mut self, name: impl Into<String>, service: T, ) -> &mut Self
Register a named concrete service.
Use this when you need multiple instances of the same type (e.g., primary vs. replica database pools).
Sourcepub fn provide_named<T>(
&mut self,
name: impl Into<String>,
service: Arc<T>,
) -> &mut Self
pub fn provide_named<T>( &mut self, name: impl Into<String>, service: Arc<T>, ) -> &mut Self
Register a pre-built Arc<T> under a string name.
Use for named trait-object registrations.
Sourcepub fn get<T>(&self) -> Option<Arc<T>>
pub fn get<T>(&self) -> Option<Arc<T>>
Resolve a service by type.
Works for both concrete types (get::<MyService>()) and trait
objects (get::<dyn MyTrait>()). Returns None if no service of
that type has been registered.
Sourcepub fn get_named<T>(&self, name: &str) -> Option<Arc<T>>
pub fn get_named<T>(&self, name: &str) -> Option<Arc<T>>
Resolve a named service by type and name.
Returns None if no service of that type and name has been registered.
Sourcepub fn contains<T: ?Sized + 'static>(&self) -> bool
pub fn contains<T: ?Sized + 'static>(&self) -> bool
Returns true if a service of type T has been registered (unnamed).
Sourcepub fn contains_named<T: ?Sized + 'static>(&self, name: &str) -> bool
pub fn contains_named<T: ?Sized + 'static>(&self, name: &str) -> bool
Returns true if a named service of type T with name exists.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Container
impl !UnwindSafe for Container
impl Freeze for Container
impl Send for Container
impl Sync for Container
impl Unpin for Container
impl UnsafeUnpin for Container
Blanket Implementations§
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> 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