diff options
Diffstat (limited to 'src/di_container')
-rw-r--r-- | src/di_container/asynchronous/binding/builder.rs | 27 | ||||
-rw-r--r-- | src/di_container/asynchronous/mod.rs | 89 | ||||
-rw-r--r-- | src/di_container/blocking/binding/builder.rs | 18 | ||||
-rw-r--r-- | src/di_container/blocking/mod.rs | 30 |
4 files changed, 47 insertions, 117 deletions
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index 5e77c24..f42e6a1 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -127,7 +127,6 @@ where /// ``` /// # use std::error::Error; /// # - /// # use syrette::{factory}; /// # use syrette::AsyncDIContainer; /// # use syrette::ptr::TransientPtr; /// # @@ -141,7 +140,6 @@ where /// # /// # impl Foo for Bar {} /// # - /// # #[factory(threadsafe = true)] /// # type FooFactory = dyn Fn(i32, String) -> TransientPtr<dyn Foo> + Send + Sync; /// # /// # #[tokio::main] @@ -175,7 +173,7 @@ where Interface: Fn<Args, Output = Return> + Send + Sync, FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<Args, Return> + Send + Sync, { - use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -213,7 +211,6 @@ where /// # use std::error::Error; /// # use std::time::Duration; /// # - /// # use syrette::{factory}; /// # use syrette::AsyncDIContainer; /// # use syrette::ptr::TransientPtr; /// # use syrette::future::BoxFuture; @@ -228,7 +225,6 @@ where /// # /// # impl Foo for Bar {} /// # - /// # #[factory] /// # type FooFactory = dyn Fn(i32, String) -> BoxFuture< /// # 'static, /// # TransientPtr<dyn Foo> @@ -274,7 +270,7 @@ where + Send + Sync, { - use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -358,7 +354,7 @@ where + Send + Sync, { - use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -449,7 +445,7 @@ where + Send + Sync, { - use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -516,11 +512,8 @@ mod tests #[cfg(feature = "factory")] async fn can_bind_to_factory() { - use crate as syrette; - use crate::factory; use crate::ptr::TransientPtr; - #[factory(threadsafe = true)] type IUserManagerFactory = dyn Fn( String, i32, @@ -567,10 +560,8 @@ mod tests use crate::future::BoxFuture; use crate::ptr::TransientPtr; use crate::test_utils::async_closure; - use crate::{self as syrette, factory}; #[rustfmt::skip] - #[factory] type IUserManagerFactory = dyn Fn(String) -> BoxFuture< 'static, TransientPtr<dyn subjects_async::IUserManager> @@ -611,13 +602,8 @@ mod tests #[cfg(feature = "factory")] async fn can_bind_to_default_factory() { - use syrette_macros::declare_default_factory; - - use crate as syrette; use crate::ptr::TransientPtr; - declare_default_factory!(dyn subjects_async::IUserManager); - let mut di_container_mock = MockAsyncDIContainer::new(); di_container_mock @@ -654,13 +640,8 @@ mod tests #[cfg(feature = "factory")] async fn can_bind_to_async_default_factory() { - use syrette_macros::declare_default_factory; - use crate::ptr::TransientPtr; use crate::test_utils::async_closure; - use crate::{self as syrette}; - - declare_default_factory!(dyn subjects_async::IUserManager, async = true); let mut di_container_mock = MockAsyncDIContainer::new(); diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 403ea18..3e29ef6 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -348,95 +348,64 @@ impl AsyncDIContainer } #[cfg(feature = "factory")] AsyncProvidable::Factory(factory_binding) => { - use crate::private::factory::IThreadsafeFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; let factory = factory_binding - .cast::<dyn IThreadsafeFactory<Interface, Self>>() - .map_err(|err| match err { - CastError::NotArcCastable(_) => { - AsyncDIContainerError::InterfaceNotAsync( - type_name::<Interface>(), - ) - } - CastError::CastFailed { - source: _, - from: _, - to: _, - } - | CastError::GetCasterFailed(_) => { - AsyncDIContainerError::CastFailed { - interface: type_name::<Interface>(), - binding_kind: "factory", - } - } + .as_any() + .downcast_ref::<ThreadsafeCastableFactory<Interface, Self>>() + .ok_or_else(|| AsyncDIContainerError::CastFailed { + interface: type_name::<Interface>(), + binding_kind: "factory", })?; Ok(SomePtr::ThreadsafeFactory(factory.call(self).into())) } #[cfg(feature = "factory")] AsyncProvidable::DefaultFactory(binding) => { - use crate::private::factory::IThreadsafeFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::ptr::TransientPtr; - type DefaultFactoryFn<Interface> = dyn IThreadsafeFactory< - dyn Fn<(), Output = TransientPtr<Interface>> + Send + Sync, + type DefaultFactoryFn<Interface> = ThreadsafeCastableFactory< + dyn Fn() -> TransientPtr<Interface> + Send + Sync, AsyncDIContainer, >; - let default_factory = Self::cast_factory_binding::< - DefaultFactoryFn<Interface>, - >(binding, "default factory")?; + let default_factory = binding + .as_any() + .downcast_ref::<DefaultFactoryFn<Interface>>() + .ok_or_else(|| AsyncDIContainerError::CastFailed { + interface: type_name::<DefaultFactoryFn<Interface>>(), + binding_kind: "default factory", + })?; Ok(SomePtr::Transient(default_factory.call(self)())) } #[cfg(feature = "factory")] AsyncProvidable::AsyncDefaultFactory(binding) => { + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; use crate::future::BoxFuture; - use crate::private::factory::IThreadsafeFactory; use crate::ptr::TransientPtr; - type AsyncDefaultFactoryFn<Interface> = dyn IThreadsafeFactory< + type AsyncDefaultFactoryFn<Interface> = ThreadsafeCastableFactory< dyn Fn<(), Output = BoxFuture<'static, TransientPtr<Interface>>> + Send + Sync, AsyncDIContainer, >; - let async_default_factory = Self::cast_factory_binding::< - AsyncDefaultFactoryFn<Interface>, - >( - binding, "async default factory" - )?; + let async_default_factory = binding + .as_any() + .downcast_ref::<AsyncDefaultFactoryFn<Interface>>() + .ok_or_else(|| AsyncDIContainerError::CastFailed { + interface: type_name::<AsyncDefaultFactoryFn<Interface>>(), + binding_kind: "async default factory", + })?; Ok(SomePtr::Transient(async_default_factory.call(self)().await)) } } } - #[cfg(feature = "factory")] - fn cast_factory_binding<Type: 'static + ?Sized>( - factory_binding: std::sync::Arc< - dyn crate::private::any_factory::AnyThreadsafeFactory, - >, - binding_kind: &'static str, - ) -> Result<std::sync::Arc<Type>, AsyncDIContainerError> - { - factory_binding.cast::<Type>().map_err(|err| match err { - CastError::NotArcCastable(_) => { - AsyncDIContainerError::InterfaceNotAsync(type_name::<Type>()) - } - CastError::CastFailed { - source: _, - from: _, - to: _, - } - | CastError::GetCasterFailed(_) => AsyncDIContainerError::CastFailed { - interface: type_name::<Type>(), - binding_kind, - }, - }) - } - async fn get_binding_providable<Interface>( &self, binding_options: BindingOptions<'static>, @@ -683,10 +652,8 @@ mod tests } } - use crate as syrette; - use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; - #[crate::factory(threadsafe = true)] type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager> + Send + Sync; @@ -767,10 +734,8 @@ mod tests } } - use crate as syrette; - use crate::private::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; - #[crate::factory(threadsafe = true)] type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager> + Send + Sync; diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index f322809..9f7f6f9 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -119,7 +119,7 @@ where /// ``` /// # use std::error::Error; /// # - /// # use syrette::{DIContainer, factory}; + /// # use syrette::DIContainer; /// # use syrette::ptr::TransientPtr; /// # /// # trait ICustomerID {} @@ -140,10 +140,8 @@ where /// # /// # impl ICustomer for Customer {} /// # - /// # #[factory] /// # type ICustomerFactory = dyn Fn(String, u32) -> TransientPtr<dyn ICustomer>; /// # - /// # #[factory] /// # type ICustomerIDFactory = dyn Fn(u32) -> TransientPtr<dyn ICustomerID>; /// # /// # fn main() -> Result<(), Box<dyn Error>> @@ -183,7 +181,7 @@ where Interface: Fn<Args, Output = crate::ptr::TransientPtr<Return>>, Func: Fn(&DIContainer) -> Box<Interface>, { - use crate::private::castable_factory::CastableFactory; + use crate::castable_factory::CastableFactory; if self .di_container @@ -218,7 +216,7 @@ where /// ``` /// # use std::error::Error; /// # - /// # use syrette::{DIContainer, factory}; + /// # use syrette::DIContainer; /// # use syrette::ptr::TransientPtr; /// # /// # trait IBuffer {} @@ -271,7 +269,7 @@ where dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>, >, { - use crate::private::castable_factory::CastableFactory; + use crate::castable_factory::CastableFactory; if self .di_container @@ -335,11 +333,8 @@ mod tests #[cfg(feature = "factory")] fn can_bind_to_factory() { - use crate as syrette; - use crate::factory; use crate::ptr::TransientPtr; - #[factory] type IUserManagerFactory = dyn Fn(i32, String) -> TransientPtr<dyn subjects::IUserManager>; @@ -378,13 +373,8 @@ mod tests #[cfg(feature = "factory")] fn can_bind_to_default_factory() { - use syrette_macros::declare_default_factory; - - use crate as syrette; use crate::ptr::TransientPtr; - declare_default_factory!(dyn subjects::IUserManager); - let mut mock_di_container = MockDIContainer::new(); mock_di_container diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index 65e697e..d9efe94 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -285,11 +285,12 @@ impl DIContainer )), #[cfg(feature = "factory")] Providable::Factory(factory_binding) => { - use crate::private::factory::IFactory; + use crate::castable_factory::CastableFactory; let factory = factory_binding - .cast::<dyn IFactory<Interface, Self>>() - .map_err(|_| DIContainerError::CastFailed { + .as_any() + .downcast_ref::<CastableFactory<Interface, Self>>() + .ok_or_else(|| DIContainerError::CastFailed { interface: type_name::<Interface>(), binding_kind: "factory", })?; @@ -298,17 +299,16 @@ impl DIContainer } #[cfg(feature = "factory")] Providable::DefaultFactory(factory_binding) => { - use crate::private::factory::IFactory; + use crate::castable_factory::CastableFactory; use crate::ptr::TransientPtr; - type DefaultFactoryFn<Interface> = dyn IFactory< - dyn Fn<(), Output = TransientPtr<Interface>>, - DIContainer, - >; + type DefaultFactoryFn<Interface> = + CastableFactory<dyn Fn() -> TransientPtr<Interface>, DIContainer>; let default_factory = factory_binding - .cast::<DefaultFactoryFn<Interface>>() - .map_err(|_| DIContainerError::CastFailed { + .as_any() + .downcast_ref::<DefaultFactoryFn<Interface>>() + .ok_or_else(|| DIContainerError::CastFailed { interface: type_name::<Interface>(), binding_kind: "default factory", })?; @@ -517,7 +517,7 @@ mod tests #[cfg(feature = "factory")] fn can_get_factory() { - use crate::private::castable_factory::CastableFactory; + use crate::castable_factory::CastableFactory; use crate::ptr::FactoryPtr; trait IUserManager @@ -556,9 +556,6 @@ mod tests } } - use crate as syrette; - - #[crate::factory] type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager>; let mut di_container = DIContainer::new(); @@ -595,7 +592,7 @@ mod tests #[cfg(feature = "factory")] fn can_get_factory_named() { - use crate::private::castable_factory::CastableFactory; + use crate::castable_factory::CastableFactory; use crate::ptr::FactoryPtr; trait IUserManager @@ -634,9 +631,6 @@ mod tests } } - use crate as syrette; - - #[crate::factory] type IUserManagerFactory = dyn Fn(Vec<i128>) -> TransientPtr<dyn IUserManager>; let mut di_container = DIContainer::new(); |