Skip to main content

Container

Struct Container 

Source
pub struct Container { /* private fields */ }
Expand description

A runtime dependency-injection container.

Container stores one provider per type.

A provider can be a closure that constructs a fresh value, or an [Arc] that clones and returns shared state. Consumers use get to retrieve an owned value regardless of how that provider is implemented.

Containers are built through ContainerBuilder (or the container! macro) and are typically wrapped in an Arc for sharing across threads.

§Example

use injectium_core::{Container, container};

let c = container! {
    providers: [|_: &Container| 42_u32, |_: &Container| "hello"],
};

assert_eq!(c.get::<u32>(), 42);
assert_eq!(c.get::<&str>(), "hello");

Implementations§

Source§

impl Container

Source

pub fn builder() -> ContainerBuilder

Returns a new ContainerBuilder.

Equivalent to ContainerBuilder::new.

Source

pub fn builder_with_capacity(capacity: usize) -> ContainerBuilder

Returns a new ContainerBuilder with preallocated storage capacity.

Source

pub fn get<T: SyncBounds>(&self) -> T

Returns the current value for type T.

§Panics

Panics if no provider of type T has been registered. Use try_get for a non-panicking alternative.

Source

pub fn try_get<T: SyncBounds>(&self) -> Option<T>

Returns the current value for type T, or None if no provider is registered.

Source

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

Returns true if a provider is registered for T.

Source

pub fn validate(&self)

Validates that every dependency declared via declare_dependency! is registered in this container.

Available when the validation feature is enabled.

Intended to be called once at application startup, immediately after the container is built. If any declared dependency is absent, the method panics with a message that lists every missing type by name, making misconfiguration easy to diagnose.

#[derive(Injectable)] automatically calls declare_dependency! for each field type, so this check covers all structs that use the derive macro without any manual bookkeeping.

§Panics

Panics if one or more declared dependencies are not registered, printing the names of all missing types.

Source

pub fn provider_count(&self) -> usize

Returns the number of registered providers.

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

impl<T> SyncBounds for T
where T: Send + Sync + 'static,