[−][src]Derive Macro coi::Inject
#[derive(Inject)] { // Attributes available to this derive: #[provides] #[inject] }
This derive proc macro impls Inject
on the struct it modifies, and also processes two
attributes:
#[provides]
- Only one of these is allowed per#[derive(Inject)]
. It takes the form
#[provides(<vis> <ty> with <expr>)]
It generates a provider struct with visibility <vis>
that impls Provide
with an output type of Arc<<ty>>
. It will construct <ty>
with <expr>
,
and all params to <expr>
must match the struct fields marked with #[inject]
(see the next
bullet item). <vis>
must match the visibility of <ty>
or you will get code that might not
compile.
#[inject]
- All fields marked#[inject]
are resolved in theprovide
fn described above. Given a field<field_name>: <field_ty>
, this attribute will cause the following resolution to be generated:
let <field_name> = container.resolve::<<field_ty>>("<field_name>");
Because of this, it's important that the field name MUST match the string that's used to
register the provider in the ContainerBuilder
.
Examples
Private trait and no dependencies
trait Priv: Inject{} #[derive(Inject)] #[provides(dyn Priv with SimpleStruct)] struct SimpleStruct;
Public trait and dependency
pub trait Pub: Inject; #[derive(Inject)] #[provides(pub dyn Pub with NewStruct::new(dependency)] struct NewStruct { #[inject] dependency: Arc<dyn Dependency>, } impl NewStruct { fn new(dependency: Arc<dyn Dependency>) -> Self { Self { dependency } } }
Struct injection
#[derive(Inject)] #[provides(pub InjectableStruct with InjectableStruct)] struct InjectableStruct;
If you need some form of constructor fn that takes arguments that are not injected, then you
need to manually implement the Provide
trait, and this derive will not be usable.