use crate::*; use spin::Once; use std::any::Any; use std::mem::MaybeUninit; type Builder = ServiceDescriptorBuilder; #[inline(always)] fn no_op(_services: &ServiceProvider) -> Ref { Ref::new(MaybeUninit::>::uninit()) } /// Initializes a new singleton [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). #[inline] pub fn singleton() -> ServiceDescriptorBuilder { Builder::new(ServiceLifetime::Singleton, Type::of::()) } /// Initializes a new keyed singleton [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). #[inline] pub fn singleton_with_key() -> ServiceDescriptorBuilder where TSvc: Any + ?Sized, { Builder::keyed::(ServiceLifetime::Singleton, Type::of::()) } /// Initializes a new singleton [`ServiceDescriptor`](crate::ServiceDescriptor). /// /// # Arguments /// /// * `factory` - The factory method used to create the service #[inline] pub fn singleton_factory(factory: F) -> ServiceDescriptor where F: Fn(&ServiceProvider) -> Ref + 'static, { Builder::::new(ServiceLifetime::Singleton, Type::factory_of::()).from(factory) } /// Initializes a new keyed singleton [`ServiceDescriptor`](crate::ServiceDescriptor). /// /// # Arguments /// /// * `factory` - The factory method used to create the service #[inline] pub fn singleton_with_key_factory(factory: F) -> ServiceDescriptor where F: Fn(&ServiceProvider) -> Ref + 'static, { Builder::::keyed::(ServiceLifetime::Singleton, Type::factory_of::()) .from(factory) } /// Initializes a new singleton [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). /// /// # Remarks /// /// This function maps a concrete type to itself rather than a trait. #[inline] pub fn singleton_as_self() -> ServiceDescriptorBuilder { Builder::new(ServiceLifetime::Singleton, Type::of::()) } /// Initializes a new scoped [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). #[inline] pub fn scoped() -> ServiceDescriptorBuilder { Builder::new(ServiceLifetime::Scoped, Type::of::()) } /// Initializes a new scoped keyed [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). #[inline] pub fn scoped_with_key() -> ServiceDescriptorBuilder where TSvc: Any + ?Sized, { Builder::keyed::(ServiceLifetime::Scoped, Type::of::()) } /// Initializes a new scoped [`ServiceDescriptor`](crate::ServiceDescriptor). /// /// # Arguments /// /// * `factory` - The factory method used to create the service #[inline] pub fn scoped_factory(factory: F) -> ServiceDescriptor where T: Any + ?Sized, F: Fn(&ServiceProvider) -> Ref + 'static, { Builder::::new(ServiceLifetime::Scoped, Type::factory_of::()).from(factory) } /// Initializes a new keyed scoped [`ServiceDescriptor`](crate::ServiceDescriptor). /// /// # Arguments /// /// * `factory` - The factory method used to create the service #[inline] pub fn scoped_with_key_factory(factory: F) -> ServiceDescriptor where TSvc: Any + ?Sized, F: Fn(&ServiceProvider) -> Ref + 'static, { Builder::::keyed::(ServiceLifetime::Scoped, Type::factory_of::()) .from(factory) } /// Initializes a new transient [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). #[inline] pub fn transient() -> ServiceDescriptorBuilder { Builder::new(ServiceLifetime::Transient, Type::of::()) } /// Initializes a new keyed transient [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). #[inline] pub fn transient_with_key() -> ServiceDescriptorBuilder { Builder::keyed::(ServiceLifetime::Transient, Type::of::()) } /// Initializes a new transient [`ServiceDescriptor`](crate::ServiceDescriptor). /// /// # Arguments /// /// * `factory` - The factory method used to create the service #[inline] pub fn transient_factory(factory: F) -> ServiceDescriptor where T: Any + ?Sized, F: Fn(&ServiceProvider) -> Ref + 'static, { Builder::::new(ServiceLifetime::Transient, Type::factory_of::()).from(factory) } /// Initializes a new keyed transient [`ServiceDescriptor`](crate::ServiceDescriptor). /// /// # Arguments /// /// * `factory` - The factory method used to create the service #[inline] pub fn transient_with_key_factory(factory: F) -> ServiceDescriptor where F: Fn(&ServiceProvider) -> Ref + 'static, { Builder::::keyed::(ServiceLifetime::Transient, Type::factory_of::()) .from(factory) } /// Initializes a new transient [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). /// /// # Remarks /// /// This function maps a concrete type to itself rather than a trait. #[inline] pub fn transient_as_self() -> ServiceDescriptorBuilder { Builder::new(ServiceLifetime::Transient, Type::of::()) } /// Initializes a new transient keyed [`ServiceDescriptorBuilder`](crate::ServiceDescriptorBuilder). /// /// # Remarks /// /// This function maps a concrete type to itself rather than a trait. #[inline] pub fn transient_with_key_as_self() -> ServiceDescriptorBuilder { Builder::keyed::(ServiceLifetime::Transient, Type::of::()) } /// Creates a new singleton [`ServiceDescriptor`](crate::ServiceDescriptor) for an existing service instance. /// /// # Arguments /// /// * `instance` - The existing service instance /// /// # Remarks /// /// This function maps an existing instance to a trait. #[inline] pub fn existing(instance: Box) -> ServiceDescriptor { ServiceDescriptor::new( ServiceLifetime::Singleton, Type::of::(), Type::of::(), Vec::with_capacity(0), Once::initialized(Ref::new(Ref::::from(instance))), Ref::new(no_op), ) } /// Creates a new singleton [`ServiceDescriptor`](crate::ServiceDescriptor) for an existing service instance. /// /// # Arguments /// /// * `instance` - The existing service instance /// /// # Remarks /// /// This function maps an existing instance to itself rather than a trait. #[inline] pub fn existing_as_self(instance: T) -> ServiceDescriptor { ServiceDescriptor::new( ServiceLifetime::Singleton, Type::of::(), Type::of::(), Vec::with_capacity(0), Once::initialized(Ref::new(Ref::from(instance))), Ref::new(no_op), ) } /// Creates a new singleton [`ServiceDescriptor`](crate::ServiceDescriptor) for an existing service instance with a key. /// /// # Arguments /// /// * `instance` - The existing service instance /// /// # Remarks /// /// This function maps an existing instance to a trait. #[inline] pub fn existing_with_key( instance: Box, ) -> ServiceDescriptor { ServiceDescriptor::new( ServiceLifetime::Singleton, Type::keyed::(), Type::of::(), Vec::with_capacity(0), Once::initialized(Ref::new(Ref::::from(instance))), Ref::new(no_op), ) } /// Creates a new singleton [`ServiceDescriptor`](crate::ServiceDescriptor) for an existing service instance with a key. /// /// # Arguments /// /// * `instance` - The existing service instance /// /// # Remarks /// /// This function maps an existing instance to itself rather than a trait. #[inline] pub fn existing_with_key_as_self(instance: TSvc) -> ServiceDescriptor { ServiceDescriptor::new( ServiceLifetime::Singleton, Type::keyed::(), Type::of::(), Vec::with_capacity(0), Once::initialized(Ref::new(Ref::from(instance))), Ref::new(no_op), ) } /// Creates a new [`ServiceDependency`](crate::ServiceDependency) with a cardinality of exactly one (1:1). #[inline] pub fn exactly_one() -> ServiceDependency { ServiceDependency::new(Type::of::(), ServiceCardinality::ExactlyOne) } /// Creates a new keyed [`ServiceDependency`](crate::ServiceDependency) with a cardinality of exactly one (1:1). #[inline] pub fn exactly_one_with_key() -> ServiceDependency { ServiceDependency::new(Type::keyed::(), ServiceCardinality::ExactlyOne) } /// Creates a new [`ServiceDependency`](crate::ServiceDependency) with a cardinality of zero or one (0:1). #[inline] pub fn zero_or_one() -> ServiceDependency { ServiceDependency::new(Type::of::(), ServiceCardinality::ZeroOrOne) } /// Creates a new keyed [`ServiceDependency`](crate::ServiceDependency) with a cardinality of zero or one (0:1). #[inline] pub fn zero_or_one_with_key() -> ServiceDependency { ServiceDependency::new(Type::keyed::(), ServiceCardinality::ZeroOrOne) } /// Creates a new [`ServiceDependency`](crate::ServiceDependency) with a cardinality of zero or more (0:*). #[inline] pub fn zero_or_more() -> ServiceDependency { ServiceDependency::new(Type::of::(), ServiceCardinality::ZeroOrMore) } /// Creates a new keyed [`ServiceDependency`](crate::ServiceDependency) with a cardinality of zero or more (0:*). #[inline] pub fn zero_or_more_with_key() -> ServiceDependency { ServiceDependency::new(Type::keyed::(), ServiceCardinality::ZeroOrMore) }