pub struct Container { /* private fields */ }Expand description
Container = our tiny dependency injection store (v1).
What it does:
- stores values by type (TypeId).
- lets us register services/config once.
- lets us resolve them later.
Why Arc?
- so multiple parts of the app can share the same service safely.
Why RwLock?
- allows safe reads/writes across threads.
- write when registering.
- read when resolving.
Implementations§
Source§impl Container
impl Container
Sourcepub fn new() -> Self
pub fn new() -> Self
Nice helper constructor. Same as Default, just cleaner to read in app code.
pub fn scoped(&self) -> Self
Sourcepub fn register<T>(&self, value: T) -> Result<(), ContainerError>
pub fn register<T>(&self, value: T) -> Result<(), ContainerError>
Register a value/service into the container.
Example: container.register(AppConfig { … })?;
Rules:
- T must be thread-safe (Send + Sync).
- T must be ’static because we store it for the app lifetime.
pub fn replace<T>(&self, value: T) -> Result<(), ContainerError>
pub fn override_value<T>(&self, value: T) -> Result<(), ContainerError>
pub fn is_type_registered_name( &self, type_name: &'static str, ) -> Result<bool, ContainerError>
pub fn register_request_factory<T, F>( &self, factory: F, ) -> Result<(), ContainerError>
pub fn register_transient_factory<T, F>( &self, factory: F, ) -> Result<(), ContainerError>
Sourcepub fn resolve<T>(&self) -> Result<Arc<T>, ContainerError>
pub fn resolve<T>(&self) -> Result<Arc<T>, ContainerError>
Resolve (get back) a registered value/service by type.
Example:
let config = container.resolve::
Returns Arc
pub fn resolve_in_module<T>( &self, module_name: &'static str, ) -> Result<Arc<T>, ContainerError>
Trait Implementations§
Source§impl<T> FromRequestParts<Container> for Inject<T>
impl<T> FromRequestParts<Container> for Inject<T>
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§
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
Mutably borrows from an owned value. Read more