auto-di 0.2.0

Ergonomic async-aware automatic dependency injection for Rust
Documentation
use std::{any::Any, marker::PhantomData, sync::Arc};

use crate::{DiError, resolve};

/// Resolves a dependency each time [`Provider::get`] is called.
pub struct Provider<T> {
    _marker: PhantomData<fn() -> T>,
}

impl<T> Clone for Provider<T> {
    fn clone(&self) -> Self {
        Self::default()
    }
}

impl<T> Default for Provider<T> {
    fn default() -> Self {
        Self {
            _marker: PhantomData,
        }
    }
}

impl<T> Provider<T>
where
    T: Any + Send + Sync,
{
    pub async fn get(&self) -> Result<Arc<T>, DiError> {
        resolve::<T>().await
    }
}

/// Resolves a dependency on first access and caches it locally.
pub struct Lazy<T> {
    cell: tokio::sync::OnceCell<Arc<T>>,
}

impl<T> Default for Lazy<T> {
    fn default() -> Self {
        Self {
            cell: tokio::sync::OnceCell::new(),
        }
    }
}

impl<T> Lazy<T>
where
    T: Any + Send + Sync,
{
    pub async fn get(&self) -> Result<&Arc<T>, DiError> {
        self.cell
            .get_or_try_init(|| async { resolve::<T>().await })
            .await
    }
}