Skip to main content

Injectable

Derive Macro Injectable 

Source
#[derive(Injectable)]
Expand description

Derives an implementation of the injectium::Injectable trait.

When applied to a named struct, this macro generates:

  1. An implementation of from_container(&Container) -> Self that resolves each field from the container using container.get::<T>().
  2. An implementation of try_from_container(&Container) -> Option<Self> that uses container.try_get::<T>() for graceful missing-dependency handling.
  3. A injectium::declare_dependency! call for each field type, enabling Container::validate to check all dependencies at startup.

§Requirements

  • The struct must be a named struct (not a tuple struct or enum).
  • All field types must be 'static.

§Example

use injectium::Injectable;
use std::sync::Arc;

#[derive(Clone)]
struct Database {
    conn: Arc<Connection>,
}

#[derive(Injectable)]
struct Service {
    db: Database,
}