[][src]Derive Macro async_injector_derive::Provider

#[derive(Provider)]
{
    // Attributes available to this derive:
    #[provider]
    #[dependency]
}

Helper derive to implement a provider.

Examples

use async_injector::{Provider, Injector, Key, async_trait};
use serde::Serialize;

/// Fake database connection.
#[derive(Clone)]
struct Database;

/// Provider that describes how to construct a database.
#[derive(Serialize)]
pub enum Tag {
    DatabaseUrl,
    ConnectionLimit,
}

#[derive(Provider)]
struct DatabaseProvider {
    #[dependency(tag = "Tag::DatabaseUrl")]
    url: String,
    #[dependency(tag = "Tag::ConnectionLimit")]
    connection_limit: u32,
}

#[async_trait]
impl Provider for DatabaseProvider {
    type Output = Database;

    /// Constructor a new database and supply it to the injector.
    async fn build(self) -> Option<Self::Output> {
        println!("Url: {:?}", self.url);
        println!("Connection Limit: {:?}", self.connection_limit);
        None
    }
}