Expand description
§Rudi
Rudi - an out-of-the-box dependency injection framework for Rust.
§Quick Links
Here are links to the most important sections of the docs:
Context
: The core of the entire dependency injection framework, responsible for managing all providers.#[Singleton]
/#[Transient]
/#[SingleOwner]
: Three attribute macros used to generate the implementation ofDefaultProvider
, thus registering providers.
§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 withtracing
.
§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()
}
Macros§
- components
- Convert a set of types that implement
DefaultProvider
to a set ofDynProvider
instances - enable
auto-register
- Generate a function to enable auto-registration.
- modules
- Convert a set of types that implement
Module
to a set ofResolveModule
instances. - providers
- Convert a set of instances that implement
Into<DynProvider>
to a set ofDynProvider
instances - register_
provider auto-register
- Register a
Provider
that will be collected byauto_registered_providers
.
Structs§
- Auto
Register Module auto-register
- A module that auto-registers all providers.
- Context
- A context is a container for all the providers and instances.
- Context
Options - Options and flags which can be used to configure how a context is created.
- Definition
- Represents a definition of a provider.
- DynProvider
- Represents a
Provider
that erased its type. - DynSingle
- Represents a
Single
that erased its type. - Key
- Represents a unique key for a provider.
- Provider
- Represents the provider of an instance of type
T
. - Resolve
Module - A type representing a Module, converted from a type that implements
Module
. - Single
- Represents a
Singleton
orSingleOwner
instance. - Single
Owner Async Provider - Represents a specialized
Provider
. - Single
Owner Provider - Represents a specialized
Provider
. - Singleton
Async Provider - Represents a specialized
Provider
. - Singleton
Provider - Represents a specialized
Provider
. - Transient
Async Provider - Represents a specialized
Provider
. - Transient
Provider - Represents a specialized
Provider
. - Type
- Represents a type.
Enums§
- Color
- Represents the color of the function, i.e., async or sync.
- Eager
Create Function - Represents the eager create function.
- Scope
- Represents the scope of the provider.
Traits§
- Default
Provider - A trait for giving a type a default
Provider
. - Future
Ext - An extension trait for
Future
s that provides a convenient adapter. - Module
- Represents a module.
Functions§
- auto_
registered_ providers auto-register
- Returns an iterator over all auto-registered providers.
- single_
owner - create a
SingleOwnerProvider
instance - single_
owner_ async - Create a
SingleOwnerAsyncProvider
instance - singleton
- create a
SingletonProvider
instance - singleton_
async - Create a
SingletonAsyncProvider
instance - transient
- create a
TransientProvider
instance - transient_
async - Create a
TransientAsyncProvider
instance
Type Aliases§
- BoxFuture
- An owned dynamically typed
Future
for use in cases where you can’t statically type your result or need to add some indirection.
Attribute Macros§
- Single
Owner rudi-macro
- Define a single owner provider.
- Singleton
rudi-macro
- Define a singleton provider.
- Transient
rudi-macro
- Define a transient provider.