TraitKit
trait-kit is a lightweight Rust library that defines a standardized module interface and provides a centralized capability & configuration management center (Kit). It gives you a consistent, type-safe way to define modules, inject dependencies, and manage capabilities — without committing to a heavy DI framework.
Features
- Standardized Module Interface — The
Moduletrait defines a uniform contract: every module declares its Config, Requirements, Capability, Error, and Builder. Consistent initialization everywhere. - Type-Safe Capability Management — Register and retrieve capabilities via typed
CapabilityKeys. No stringly-typed lookups. Trait-object-safe with fullSend + Syncsupport. - Thread-Safe Config Center —
ConfigHandle<T>provides live configuration updates with lock-free reads viaarc-swap. Multiple handles share the same underlying storage; updates propagate instantly. - Clean Builder Integration —
.kit(&kit).provide::<K>()builds a module and registers its capability in one fluent chain. Config and requirements are injected before.kit(), enforced at compile time. - Explicit Composition — No magic auto-wiring. You control the initialization order and dependency construction. The code is readable, debuggable, and easy to refactor.
- Minimal Dependencies — Only
arc-swap(internal) andthiserror(public error types). No heavy DI framework, no proc macros, no runtime reflection.
Quick Start
MSRV
Minimum Supported Rust Version: 1.76
Installation
Minimal Example
Define a logger module, register it to Kit, and retrieve it:
use Arc;
use *;
// 1. Define a capability trait
// 2. Define a capability key
;
// 3. Define a module
;
// 4. Define a builder
;
;
// 5. Use it
Usage
Module with Configuration
use Arc;
use *;
;
;
// Usage
let kit = new;
new
.config
.kit
.
.unwrap;
Module with Dependencies
use Arc;
use *;
;
;
;
// Usage
let kit = new;
let logger: = new;
new
.requirements
.kit
.
.unwrap;
Configuration Center
use *;
;
let kit = new;
kit.;
let handle = kit..unwrap;
println!;
// All handles share the same underlying storage
handle.set;
Layered Composition
Build a logger → inject it into storage → inject both into a user service — all managed by Kit:
// See full example: examples/layered_app.rs
let kit = new;
let logger = LoggerModuleBuilder.kit.?;
let storage = new
.logger
.kit
.?;
let user_service = new
.logger
.storage
.kit
.?;
user_service.create_user;
Kit API Overview
| Method | Description |
|---|---|
Kit::new() |
Create an empty Kit. |
kit.provide::<K>() |
Register a capability (fails if key exists). |
kit.replace::<K>() |
Register or overwrite a capability. |
kit.require::<K>() |
Retrieve a capability (fails if missing). |
kit.contains::<K>() |
Check if a capability is registered. |
kit.set_config::<K>() |
Set a configuration value. |
kit.config::<K>() |
Get a shared ConfigHandle for live updates. |
kit.contains_config::<K>() |
Check if a config key exists. |
Crate Feature Flags
trait-kit currently has no optional features. All functionality is available out of the box.
Why trait-kit?
trait-kit sits between "raw manual wiring" and "full DI framework":
| Approach | Pros | Cons |
|---|---|---|
| Manual wiring | Simple, no deps. | Ad-hoc patterns, inconsistent per project. |
| trait-kit | Standard pattern, type-safe, lightweight. | You still wire dependencies explicitly. |
| Full DI (shaku etc.) | Auto-resolved, less glue code. | Heavier deps, magic, harder to debug. |
trait-kit gives you the standardization of a DI framework with the explicitness of manual wiring.
Contributing
Build Requirements
- Rust 1.76 or later (stable).
- No external tooling required (no protoc, no openssl, no system libraries).
Development Commands
# Run all tests
# Run example programs
# Lint
# Format
# Compile-fail tests (trybuild)
Code of Conduct
This project follows the Rust Code of Conduct. All contributors are expected to uphold it.
Pull Request Process
- Ensure all tests pass and Clippy is clean.
- Add tests for new functionality (unit, integration, or compile-fail as appropriate).
- Update examples if public API changes.
- Keep the README in sync with any API changes.
License
This project is licensed under the MIT License.
© 2026 Kirky.X