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:
- 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. - Dynamic
TypeMapservice 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: Databaseandwrite_db: Database) silently overwrites the first unless you invent wrapper newtypes. - Hand-rolled
main.rsinitialization chains: Dozens of manualArc::clonecalls 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
- Plain domain types: services, repositories and controllers need no macros or framework traits.
- Compile-time graph checks: unknown dependencies and cycles, such as
service_adepending onservice_bwhich depends onservice_a, failcargo build, each reported at its field and all of them in one compile. Fields are visited in the order they are declared, and the fields each one needs are built before it. - Typed getters:
get_<name>()returns the declared type with nodyn Anyand no forcedArc<T>(anyT: Cloneworks). - Named edges: two dependencies of the same type (
read_dbandwrite_db) are wired by field name, without wrapper newtypes. - Named constructors:
build = constructor(connect)callsType::connect(...)instead ofType::new(...). - Defaults:
build = defaultcallsDefault::default()for a type that only implementsDefault, and#[container(leaf_build = default)]does it for every field withoutdepsor abuildof its own. - Factories:
build = factoryhas the container call abuild_<name>method you write in the generated<Container>Factoriestrait, which receives the field'sdepsby name in one struct; it builds trait-object fields and anything a plain constructor cannot. - Builders:
build = buildercallsType::builder(), passes each of the field'sdepsto the setter of its name, and finishes with.build(). A label,deps = [disk = folder_files], passes a dependency to a setter with another name, andbuild = builder(start = draft, finish = publish)names other functions. Any builder works, written by hand or generated by a crate such asbonortyped-builder; with a typestate builder, a missing setter is a compile error too.
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:
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.