pub struct Container<'a>(/* private fields */);
Expand description
A container that you can register your services or constructors and handle their dependencies
Implementations§
Source§impl<'a> Container<'a>
impl<'a> Container<'a>
pub fn new() -> Self
Sourcepub fn register_service<T: Clone + 'static>(&self, value: T)
pub fn register_service<T: Clone + 'static>(&self, value: T)
Register a new service.
Panic if error occurred.
Sourcepub fn try_register_service<T: Clone + 'static>(
&self,
value: T,
) -> Result<(), Error<T>>
pub fn try_register_service<T: Clone + 'static>( &self, value: T, ) -> Result<(), Error<T>>
Register a new service.
Sourcepub fn register_constructor<T: Clone + 'static>(
&self,
value: impl Fn(Container<'_>) -> T + 'a,
)
pub fn register_constructor<T: Clone + 'static>( &self, value: impl Fn(Container<'_>) -> T + 'a, )
Register a new constructor or replace the old constructor.
Panic if error occurred.
Sourcepub fn try_register_constructor<T: Clone + 'static>(
&self,
value: impl Fn(Container<'_>) -> T + 'a,
) -> Result<(), Error<T>>
pub fn try_register_constructor<T: Clone + 'static>( &self, value: impl Fn(Container<'_>) -> T + 'a, ) -> Result<(), Error<T>>
Register a new constructor or replace the old constructor.
Sourcepub fn get<T: Clone + 'static>(&self) -> T
pub fn get<T: Clone + 'static>(&self) -> T
Get a service from the container, if the service doesn’t exist but a available constructor exists, it will try to construct it.
Panic if error occurred.
Sourcepub fn try_get<T: Clone + 'static>(&self) -> Result<T, Error<T>>
pub fn try_get<T: Clone + 'static>(&self) -> Result<T, Error<T>>
Get a service from the container, if the service doesn’t exist but a available constructor exists, it will try to construct it.
Sourcepub fn construct<T: Clone + 'static>(&self) -> T
pub fn construct<T: Clone + 'static>(&self) -> T
Just construct a new object, whether it exists or not, it will be constructed and won’t be inserted into container
Panic if error occurred.
Sourcepub fn try_construct<T: Clone + 'static>(&self) -> Result<T, Error<T>>
pub fn try_construct<T: Clone + 'static>(&self) -> Result<T, Error<T>>
Just construct a new object, whether it exists or not, it will be constructed and won’t be inserted into container
Sourcepub fn remove_service<T: Clone + 'static>(&self) -> Result<T, Error<T>>
pub fn remove_service<T: Clone + 'static>(&self) -> Result<T, Error<T>>
Remove a service
Sourcepub fn remove_constructor<T: Clone + 'static>(&self) -> Result<(), Error<T>>
pub fn remove_constructor<T: Clone + 'static>(&self) -> Result<(), Error<T>>
Remove a constructor
Sourcepub fn into_static(self) -> Container<'static>
pub fn into_static(self) -> Container<'static>
Remove all constructors and convert the lifetime to static