pub struct Container { /* private fields */ }Expand description
Type-map IoC service container.
Intended to be shared as Arc<Container> and mounted as an Axum Extension.
§Example
ⓘ
use std::sync::Arc;
use rok_container::Container;
let container = Arc::new(Container::new());
// singleton — same Arc<T> returned every call
container.singleton(MyService::new());
// factory — fresh instance on every make()
container.bind::<Config>(|| Config::from_env());
// resolve
let svc: Arc<MyService> = container.make::<MyService>().unwrap();Implementations§
Source§impl Container
impl Container
Sourcepub fn bind<T, F>(&self, factory: F)
pub fn bind<T, F>(&self, factory: F)
Register a factory for T.
Every call to make::<T> invokes factory and
returns a new Arc<T>.
Sourcepub fn singleton<T>(&self, instance: T)
pub fn singleton<T>(&self, instance: T)
Register a singleton instance for T.
Every call to make::<T> clones the same Arc<T>.
Sourcepub fn make<T>(&self) -> Result<Arc<T>, ContainerError>
pub fn make<T>(&self) -> Result<Arc<T>, ContainerError>
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§
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