1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
Syrette
The convenient dependency injection & inversion of control framework for Rust.
Namesake
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.
Features
- A dependency injection and inversion of control container
- Autowiring dependencies
- API inspired from the one of InversifyJS
- Helpful error messages
- Enforces the use of interface traits
- Supports generic implementations & generic interface traits
- Binding singletons
- Injection of third-party structs & traits
- Named bindings
- Async factories
Optional features
factory
. Binding factories (Rust nightly required)prevent-circular
. Detection and prevention of circular dependencies. (Enabled by default)async
. Asynchronous support
To use these features, you must enable it in Cargo.
Why inversion of control & dependency injection?
The reason for practing IoC and DI is to write modular & loosely coupled applications.
This is what we're trying to avoid:
impl Foo
{
/// ❌ Bad. Foo knows the construction details of Bar.
pub fn new() -> Self
{
Self {
bar: Bar::new()
}
}
The following is better:
impl Foo
/// ✅ Better. Foo is unaware of how Bar is constructed.
pub fn new(bar: Bar) -> Self
{
Self {
bar
}
}
}
This will however grow quite tiresome sooner or later when you have a large codebase with many dependencies and dependencies of those and so on. Because you will have to specify the dependencies someplace
let foobar = Foobar::new(
Foo:new(
Woof::new(),
Meow::new()),
Bar::new(
Something::new(),
SomethingElse::new(),
SomethingMore::new()
)
)
This is where Syrette comes in.
Motivation
Other DI & IoC libraries for Rust are either unmaintained (di for example), overcomplicated and requires Rust nightly for all functionality (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 & IoC library.
Example usage
use std::error::Error;
use syrette::injectable;
use syrette::di_container::blocking::prelude::*;
use syrette::ptr::TransientPtr;
trait IWeapon
{
fn deal_damage(&self, damage: i32);
}
struct Sword {}
#[injectable(IWeapon)]
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<dyn IWeapon>,
}
#[injectable(IWarrior)]
impl Warrior
{
fn new(weapon: TransientPtr<dyn IWeapon>) -> Self
{
Self { weapon }
}
}
impl IWarrior for Warrior
{
fn fight(&self)
{
self.weapon.deal_damage(30);
}
}
fn main() -> Result<(), Box<dyn Error>>
{
let mut di_container = DIContainer::new();
di_container.bind::<dyn IWeapon>().to::<Sword>()?;
di_container.bind::<dyn IWarrior>().to::<Warrior>()?;
let warrior = di_container.get::<dyn IWarrior>()?.transient()?;
warrior.fight();
println!("Warrior has fighted");
Ok(())
}
For more examples see the examples folder.
Rust version requirements
Syrette requires Rust >= 1.62.1 to work. This is mainly due to the dependency on Linkme.
Todo
- Add support for generic factories
Contributing
You can reach out by joining the mailing list.
This is the place to submit patches, feature requests and to report bugs.