Rudi - an out-of-the-box dependency injection framework for Rust.
```rust use rudi::{Context, Singleton, Transient};
// Register fn(cx) -> A { A }
as the constructor for A
struct A;
struct B(A);
// Register fn(cx) -> B { B(cx.resolve::<A>()) }
as the constructor for B
impl B { fn new(a: A) -> B { B(a) } }
// Register fn(cx) -> () { Run(cx.resolve::<B>()) }
as the constructor for ()
fn Run(b: B) { println!("{:?}", b); }
fn main() {
// Automatically register all types and functions with the #[Singleton]
or #[Transient]
attribute.
let mut cx = Context::auto_register();
// Get an instance of `()` from the `Context`, which will call the `Run` function.
// This is equivalent to `cx.resolve::<()>();`
cx.resolve()
} ```
singleton
and transient
.```rust use std::{fmt::Debug, rc::Rc};
use rudi::{Context, Singleton, Transient};
// Register async function and specify name
async fn Number() -> i32 { 42 }
struct Foo { #[di("number")] // Specify the name of the dependency number: i32, }
struct Bar(Foo);
impl Bar {
fn into_debug(self) -> Rc
Debug
trait and the trait object of the Debug
traitimpl Bar { async fn new(#[di("foo")] f: Foo) -> Bar { // Register async constructor Bar(f) } }
async fn run(bar: Bar, debug: Rc
async fn main() { let mut cx = Context::auto_register();
cx.resolve_async().await
} ```
More examples can be found in the examples and tests directories.
Thanks for your help improving the project! We are so happy to have you!
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.