Crate rudi_dev

Crate rudi_dev 

Source
Expand description

§Rudi

Rudi - an out-of-the-box dependency injection framework for Rust.

Here are links to the most important sections of the docs:

§Feature Flags

  • rudi-macro (Default): Enables the #[Singleton], #[Transient] and #[SingleOwner] attribute macros.
  • auto-register (Default): Enables automatic registration of types and functions.
  • tracing: Adds support for logging with tracing.

§Example

use rudi::{Context, Singleton, Transient};

// Register `fn(cx) -> A { A }` as the constructor for `A`
#[derive(Debug)]
#[Transient]
struct A;

#[derive(Debug)]
struct B(A);

// Register `fn(cx) -> B { B::new(cx.resolve::<A>()) }` as the constructor for `B`
#[Transient]
impl B {
    #[di]
    fn new(a: A) -> B {
        B(a)
    }
}

// Register `fn(cx) -> C { C::B(cx.resolve::<B>()) }` as the constructor for `C`
#[allow(dead_code)]
#[Transient]
enum C {
    A(A),

    #[di]
    B(B),
}

// Register `fn(cx) -> () { Run(cx.resolve::<B>(), cx.resolve::<C>()) }` as the constructor for `()`
#[Singleton]
fn Run(b: B, c: C) {
    println!("{:?}", b);
    assert!(matches!(c, C::B(_)));
}

fn main() {
    // Automatically register all types and functions with the `#[Singleton]`, `#[Transient]` or `#[SingleOwner]` 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()
}

Enums§

Color
Represents the color of the function, i.e., async or sync.
Scope
Represents the scope of the provider.

Attribute Macros§

Propertiesrudi-macro
Define a properties. Please use in conjunction with this macro: #[rudi::Singleton(eager_create = true)] Please add serde_yaml to your dependencies.
SingleOwnerrudi-macro
Define a single owner provider.
Singletonrudi-macro
Define a singleton provider.
Transientrudi-macro
Define a transient provider.