Skip to main content

Container

Struct Container 

Source
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

Source

pub fn new() -> Self

Create an empty container.

Source

pub fn bind<T, F>(&self, factory: F)
where T: Any + Send + Sync + 'static, F: Fn() -> T + Send + Sync + 'static,

Register a factory for T.

Every call to make::<T> invokes factory and returns a new Arc<T>.

Source

pub fn singleton<T>(&self, instance: T)
where T: Any + Send + Sync + 'static,

Register a singleton instance for T.

Every call to make::<T> clones the same Arc<T>.

Source

pub fn make<T>(&self) -> Result<Arc<T>, ContainerError>
where T: Any + Send + Sync + 'static,

Resolve T, returning Arc<T>.

§Errors

Returns ContainerError::NotRegistered if T was never bound.

Source

pub fn extend<T, F>(&self, extender: F) -> Result<(), ContainerError>
where T: Any + Send + Sync + 'static, F: FnOnce(Arc<T>) -> T,

Decorate an existing singleton by transforming it.

Useful for wrapping a service with a decorator or logging layer.

§Errors

Returns an error if T is not registered.

Source

pub fn swap<T>(&self, instance: T)
where T: Any + Send + Sync + 'static,

Replace an existing binding with a new singleton.

Useful in tests to swap a real service for a test double.

Trait Implementations§

Source§

impl Default for Container

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.