dependency-injector-derive
Derive macros for dependency-injector.
Overview
This crate provides the #[derive(Inject)] macro for automatic compile-time dependency injection. It generates a from_container() method that resolves dependencies from a Container instance.
Installation
This crate is typically used through the derive feature of dependency-injector:
[]
= { = "0.2", = ["derive"] }
Or directly:
[]
= "0.1"
= "0.2"
Usage
use ;
use Arc;
Attributes
| Attribute | Field Type | Description |
|---|---|---|
#[inject] |
Arc<T> |
Required dependency. Fails if not registered. |
#[inject(optional)] |
Option<Arc<T>> |
Optional dependency. None if not registered. |
| (none) | Any Default type |
Uses Default::default(). |
Generated Code
The macro generates an impl block with a from_container method:
Requirements
- Struct must have named fields (no tuple structs)
#[inject]fields must beArc<T>#[inject(optional)]fields must beOption<Arc<T>>- Non-injected fields must implement
Default
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.