use super::{ServiceDescriptor, ServiceLifetime};
use crate::{Ref, ServiceDependency, ServiceProvider, Type};
use std::any::Any;
use std::marker::PhantomData;
use std::sync::OnceLock;
pub struct ServiceDescriptorBuilder<TSvc: ?Sized, TImpl> {
lifetime: ServiceLifetime,
service_type: Type,
implementation_type: Type,
dependencies: Vec<ServiceDependency>,
_marker_svc: PhantomData<TSvc>,
_marker_impl: PhantomData<TImpl>,
}
impl<TSvc: ?Sized, TImpl> ServiceDescriptorBuilder<TSvc, TImpl> {
pub fn depends_on(mut self, dependency: ServiceDependency) -> Self {
if !self.dependencies.contains(&dependency) {
self.dependencies.push(dependency);
}
self
}
pub fn new(lifetime: ServiceLifetime, implementation_type: Type) -> Self {
Self {
lifetime,
service_type: Type::of::<TSvc>(),
implementation_type,
dependencies: Vec::new(),
_marker_svc: PhantomData,
_marker_impl: PhantomData,
}
}
pub fn keyed<TKey>(lifetime: ServiceLifetime, implementation_type: Type) -> Self {
Self {
lifetime,
service_type: Type::keyed::<TKey, TSvc>(),
implementation_type,
dependencies: Vec::new(),
_marker_svc: PhantomData,
_marker_impl: PhantomData,
}
}
}
macro_rules! from {
(($($traits:tt)+), ($($bounds:tt)+)) => {
impl<TSvc: ?Sized + $($traits)+, TImpl> ServiceDescriptorBuilder<TSvc, TImpl> {
pub fn from(mut self, factory: impl (Fn(&ServiceProvider) -> Ref<TSvc>) + $($bounds)+) -> ServiceDescriptor {
ServiceDescriptor::new(
self.lifetime,
self.service_type,
self.implementation_type,
if self.dependencies.is_empty() {
Vec::new()
} else {
self.dependencies.shrink_to_fit();
self.dependencies
},
OnceLock::new(),
Ref::new(move |sp| Ref::new(factory(sp))),
)
}
}
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "async")] {
from!((Any + Send + Sync), (Send + Sync + 'static));
} else {
from!((Any), ('static));
}
}