Derive Macro coi::Provide

source ·
#[derive(Provide)]
{
    // Attributes available to this derive:
    #[coi]
}
Expand description

Generates an impl for Provide and also generates a “Provider” struct with its own Provide impl.

This derive proc macro impls Provide on the struct it modifies, and also processes #[coi(…)] attributes:

  • #[coi(provides ...)] - It takes the form
#[coi(provides <vis> <ty> with <expr>)]

Multiple provides attributes are not allowed since this is for a specific Provide impl and not for the resolved type.

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 #[coi(inject)] (see the next bullet item). <vis> must match the visibility of <ty> or you will get code that might not compile. If <name> is not provided, the struct name will be used and Provider will be appended to it.

Examples

Private trait and no dependencies

use coi::{Inject, Provide};
trait Priv: Inject {}

#[derive(Inject)]
struct SimpleStruct {
    data: u32
}

impl SimpleStruct {
    fn new(data: u32) -> Self {
        Self { data }
    }
}

impl Priv for SimpleStruct {}

#[derive(Provide)]
#[coi(provides dyn Priv with SimpleStruct::new(self.data))]
struct SimpleStructProvider {
    data: u32,
}

impl SimpleStructProvider {
    fn new(data: u32) -> Self {
        Self { data: 42 }
    }
}