use std::{any::Any, marker::PhantomData, sync::Arc};
use crate::{DiError, resolve};
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
}
}
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
}
}