snapfit

snapfit (snapfit-rs)

Zero-Cost, Compile-Time Verified, Pure-Rust Dependency Injection Framework

Its source, its issues, and how to contribute are in its repository on GitHub.


What is snapfit?

snapfit is a compile-time verified dependency injection framework designed specifically for idiomatic Rust.

Unlike dependency injection frameworks ported from Java (Spring, Dagger) or C# (.NET) that scatter #[inject] and #[component] attributes across your business logic, or dynamic service locators that rely on TypeMap and dyn Any downcasting at runtime, snapfit inverts the dependency declaration: the container struct declares the wiring, leaving your domain structs 100% pure Rust.


Why snapfit?

Most Rust applications eventually face three unsatisfying choices for wiring components:

  1. Invasive component macros (#[inject], #[component]): Every service, repository, and controller has to import DI macros and derive framework traits. Your domain is coupled to the DI framework, and seeing the whole dependency graph requires opening dozens of files.
  2. Dynamic TypeMap service locators (Arc<dyn Any + Send + Sync>): Missing dependencies fail at runtime, resolution pays lookup and downcasting overhead, and registering two instances of the same type (read_db: Database and write_db: Database) silently overwrites the first unless you invent wrapper newtypes.
  3. Hand-rolled main.rs initialization chains: Dozens of manual Arc::clone calls obscure which components are shared singletons and what depends on what.
Guarantee Traditional Component DI Dynamic TypeMap Locators snapfit
Domain pollution Macros and traits on every service Services or handlers pull from locator Zero: domain structs are plain Rust
Where the graph lives Scattered across dozens of files Hidden inside runtime registration calls One central #[container] struct
Multiple instances of one type Requires qualifier macros on structs Overwrites previous entry unless wrapped in newtypes Native: edges name container fields (deps = [read_db, write_db])
Cycle & missing-dep detection Build script or runtime failure Panics or returns None at runtime Compile-time, with the error on the offending field
Resolution cost Vtable or generated factory Hash lookup + dyn Any downcast Zero-cost direct getters (get_<name>())

Releases and The Snapfit Book

The Snapfit Book walks through the framework step by step with complete, compiled Rust programs for each release.

Version Book Edition Release Notes Highlights
v0.1.1 (2026-11-20) Read the Book (v0.1.1) Release notes on GitHub snapfit v0.1.1 release and verified edition of The Snapfit Book.
v0.1.0 (2026-11-18) Read the Book (v0.1.0) Release notes on GitHub Compile-time verified dependency injection with pure-Rust domain types: singletons wired by name, each built with its constructor, Default, a factory or its type's builder, and unknown dependencies and cycles reported at compile time.

Browse all releases and editions.


Quick Look

use std::sync::Arc;
use snapfit::container;

// 1. Your domain types are plain Rust: no macros, no framework traits.
pub struct Config {
    pub database_url: String,
}

impl Config {
    pub fn new() -> Self {
        Self { database_url: "postgres://localhost/shop".to_string() }
    }
}

pub struct Database {
    pub url: String,
}

impl Database {
    pub fn connect(config: Arc<Config>) -> Self {
        Self { url: config.database_url.clone() }
    }
}

pub struct UserService {
    db: Arc<Database>,
}

impl UserService {
    pub fn new(db: Arc<Database>) -> Self {
        Self { db }
    }
}

// 2. The container declares the dependency graph in one place:
#[container]
pub struct AppContainer {
    // Leaf singleton: calls Config::new()
    #[singleton]
    config: Arc<Config>,

    // Named constructor: calls Database::connect(config)
    #[singleton(deps = [config], build = constructor(connect))]
    db: Arc<Database>,

    // Calls UserService::new(db)
    #[singleton(deps = [db])]
    users: Arc<UserService>,
}

// 3. Build once, then use the typed getters:
fn main() {
    let container = AppContainer::new();
    let users = container.get_users();
    assert!(Arc::ptr_eq(&container.get_db(), &users.db));
}

What 0.1 Supports


Minimum Supported Rust Version (MSRV)

This crate requires Rust 1.85.0 or higher (Rust 2024 edition), which CI checks.


License

Dual-licensed under either of:

at your option.