ru-di
A simple and lightweight dependency injection container for Rust.
Features
- Simple and lightweight
- Thread-safe
- Support for both transient and singleton services
- No runtime overhead
- Zero dependencies
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Usage
Basic Usage
use Di;
// Define your services
// Register services
;
;
// Get service instance
let app = .unwrap;
assert_eq!;
Singleton Services
use Di;
// Register a singleton
register_single;
// Get singleton instance
if let Some =
// The change persists
if let Some =
API Documentation
Registering Services
Di::register<T, F>(factory: F)- Register a transient serviceDi::register_single<T>(instance: T)- Register a singleton service
Getting Services
Di::get<T>() -> Result<T, Box<dyn Error>>- Get a transient service instanceDi::get_single<T>() -> Option<SingleRef<T>>- Get a singleton service instance
Thread Safety
All operations are thread-safe. The container uses Arc, Mutex, and RwLock internally to ensure thread safety.
License
This project is licensed under the MIT License - see the LICENSE file for details.