The convenient dependency injection framework for Rust.
From the syrette Wikipedia article.
A syrette is a device for injecting liquid through a needle. It is similar to a syringe except that it has a closed flexible tube (like that typically used for toothpaste) instead of a rigid tube and piston.
factory
. Binding factories (Rust nightly required)prevent-circular
. Detection and prevention of circular dependencies. (Enabled by default)To use these features, you must enable it in Cargo.
Other DI/IOC libraries for Rust are either unmaintained (di for example), overcomplicated and or bloated (anthill-di for example) or has a weird API (teloc for example).
The goal of Syrette is to be a simple, useful, convenient and familiar DI library.
```rust use syrette::{injectable, DIContainer}; use syrette::ptr::TransientPtr;
trait IWeapon { fn deal_damage(&self, damage: i32); }
struct Sword {}
impl Sword { fn new() -> Self { Self {} } }
impl IWeapon for Sword { fn deal_damage(&self, damage: i32) { println!("Sword dealt {} damage!", damage); } }
trait IWarrior { fn fight(&self); }
struct Warrior {
weapon: TransientPtr
impl Warrior
{
fn new(weapon: TransientPtr
impl IWarrior for Warrior { fn fight(&self) { self.weapon.deal_damage(30); } }
fn main() { let mut di_container = DIContainer::new();
di_container.bind::<dyn IWeapon>().to::<Sword>().unwrap();
di_container.bind::<dyn IWarrior>().to::<Warrior>().unwrap();
let warrior = di_container.get::<dyn IWarrior>().unwrap();
warrior.fight();
println!("Warrior has fighted");
} ```
For more examples see the examples folder.
You can reach out by joining the mailing list.
This is the place to submit patches, feature requests and to report bugs.