injectium 0.1.0

Minimal dependency-injection implementation for Rust
Documentation

Injectium – a minimal dependency-injection implementation for Rust.

This is the user-facing crate that re-exports everything from injectium_core and adds the #[derive(Injectable)] proc-macro.

Quick Start

use injectium::{Injectable, container};

// Define services that need DI
#[derive(Clone, Injectable)]
struct Db {
    conn: String,
}

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

// At startup, build the container
let c = container! {
    singletons: [
        Db { conn: "postgres://localhost".into() },
    ],
};

// Validate everything is wired up
c.validate();

// Later, resolve services
let svc = Service::from_container(&c);

Key Types

  • [Container] – the runtime container holding singletons and factories.
  • [ContainerBuilder] – fluent builder for constructing a container.
  • [Injectable] – trait for types that can construct themselves from a container. Implement via #[derive(Injectable)].
  • [container] – macro for building a container with singletons and factories.
  • [declare_dependency!] – manually declare a type is required (usually automatic via #[derive(Injectable)]).