Skip to main content

Container

Struct Container 

Source
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

Source

pub fn new() -> Self

Create an empty container.

Source

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.

Source

pub fn provide<T>(&mut self, service: Arc<T>) -> &mut Self
where T: ?Sized + Send + Sync + 'static,

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.

Source

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).

Source

pub fn provide_named<T>( &mut self, name: impl Into<String>, service: Arc<T>, ) -> &mut Self
where T: ?Sized + Send + Sync + 'static,

Register a pre-built Arc<T> under a string name.

Use for named trait-object registrations.

Source

pub fn get<T>(&self) -> Option<Arc<T>>
where T: ?Sized + 'static, Arc<T>: Clone,

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.

Source

pub fn get_named<T>(&self, name: &str) -> Option<Arc<T>>
where T: ?Sized + 'static, Arc<T>: Clone,

Resolve a named service by type and name.

Returns None if no service of that type and name has been registered.

Source

pub fn contains<T: ?Sized + 'static>(&self) -> bool

Returns true if a service of type T has been registered (unnamed).

Source

pub fn contains_named<T: ?Sized + 'static>(&self, name: &str) -> bool

Returns true if a named service of type T with name exists.

Source

pub fn len(&self) -> usize

Total number of unnamed registrations.

Source

pub fn is_empty(&self) -> bool

Returns true if no services have been registered.

Source

pub fn into_arc(self) -> Arc<Self>

Seal this container into an Arc<Container> for sharing across threads and handler closures.

Typical usage with App::with_state:

let mut c = Container::new();
// c.register(...);
let app = routes! { App::with_state(c.into_arc()), };

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more