From 0f2756536e8fc311119da2af5b4dcc33f41bec6e Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 4 Oct 2023 12:51:06 +0200 Subject: refactor!: remove factory & declare_default_factory macros BREAKING CHANGE: The factory and the declare_default_factory macros have been removed. They are no longer needed to use factories --- src/any_factory.rs | 13 +++ src/castable_factory/mod.rs | 88 +++++++++++++++++++ src/castable_factory/threadsafe.rs | 99 ++++++++++++++++++++++ src/di_container/asynchronous/binding/builder.rs | 27 +----- src/di_container/asynchronous/mod.rs | 89 ++++++-------------- src/di_container/blocking/binding/builder.rs | 18 +--- src/di_container/blocking/mod.rs | 30 +++---- src/lib.rs | 9 +- src/private/any_factory.rs | 11 --- src/private/castable_factory/mod.rs | 91 -------------------- src/private/castable_factory/threadsafe.rs | 103 ----------------------- src/private/factory.rs | 20 ----- src/private/mod.rs | 9 -- src/provider/async.rs | 31 ++++--- src/provider/blocking.rs | 20 +++-- 15 files changed, 284 insertions(+), 374 deletions(-) create mode 100644 src/any_factory.rs create mode 100644 src/castable_factory/mod.rs create mode 100644 src/castable_factory/threadsafe.rs delete mode 100644 src/private/any_factory.rs delete mode 100644 src/private/castable_factory/mod.rs delete mode 100644 src/private/castable_factory/threadsafe.rs delete mode 100644 src/private/factory.rs (limited to 'src') diff --git a/src/any_factory.rs b/src/any_factory.rs new file mode 100644 index 0000000..3aee98f --- /dev/null +++ b/src/any_factory.rs @@ -0,0 +1,13 @@ +//! Interface for any factory to ever exist. + +use std::any::Any; +use std::fmt::Debug; + +/// Interface for any factory to ever exist. +pub trait AnyFactory: Any + Debug +{ + fn as_any(&self) -> &dyn Any; +} + +/// Interface for any threadsafe factory to ever exist. +pub trait AnyThreadsafeFactory: AnyFactory + Send + Sync + Debug {} diff --git a/src/castable_factory/mod.rs b/src/castable_factory/mod.rs new file mode 100644 index 0000000..196dc14 --- /dev/null +++ b/src/castable_factory/mod.rs @@ -0,0 +1,88 @@ +use std::any::{type_name, Any}; +use std::fmt::Debug; + +use crate::any_factory::AnyFactory; +use crate::ptr::TransientPtr; + +#[cfg(feature = "async")] +pub mod threadsafe; + +pub struct CastableFactory +where + ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, +{ + func: &'static dyn Fn(&DIContainerT) -> TransientPtr, +} + +impl CastableFactory +where + ReturnInterface: 'static + ?Sized, +{ + pub fn new( + func: &'static dyn Fn(&DIContainerT) -> TransientPtr, + ) -> Self + { + Self { func } + } + + pub fn call(&self, di_container: &DIContainerT) -> TransientPtr + { + (self.func)(di_container) + } +} + +impl AnyFactory + for CastableFactory +where + ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, +{ + fn as_any(&self) -> &dyn Any + { + self + } +} + +impl Debug + for CastableFactory +where + ReturnInterface: 'static + ?Sized, +{ + #[cfg(not(tarpaulin_include))] + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + let ret = type_name::>(); + + formatter.write_fmt(format_args!( + "CastableFactory (&DIContainer) -> {ret} {{ ... }}" + )) + } +} + +#[cfg(test)] +mod tests +{ + use super::*; + use crate::di_container::blocking::MockDIContainer; + + #[derive(Debug, PartialEq, Eq)] + struct Bacon + { + heal_amount: u32, + } + + #[test] + fn can_call() + { + let castable_factory = CastableFactory::new(&|_: &MockDIContainer| { + TransientPtr::new(Bacon { heal_amount: 27 }) + }); + + let mock_di_container = MockDIContainer::new(); + + let output = castable_factory.call(&mock_di_container); + + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); + } +} diff --git a/src/castable_factory/threadsafe.rs b/src/castable_factory/threadsafe.rs new file mode 100644 index 0000000..5935d75 --- /dev/null +++ b/src/castable_factory/threadsafe.rs @@ -0,0 +1,99 @@ +use std::any::{type_name, Any}; +use std::fmt::Debug; + +use crate::any_factory::{AnyFactory, AnyThreadsafeFactory}; +use crate::ptr::TransientPtr; + +pub struct ThreadsafeCastableFactory +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ + func: &'static (dyn Fn(&DIContainerT) -> TransientPtr + Send + Sync), +} + +impl + ThreadsafeCastableFactory +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ + pub fn new( + func: &'static (dyn Fn(&DIContainerT) -> TransientPtr + + Send + + Sync), + ) -> Self + { + Self { func } + } + + pub fn call(&self, di_container: &DIContainerT) -> TransientPtr + { + (self.func)(di_container) + } +} + +impl AnyFactory + for ThreadsafeCastableFactory +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ + fn as_any(&self) -> &dyn Any + { + self + } +} + +impl AnyThreadsafeFactory + for ThreadsafeCastableFactory +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ +} + +impl Debug + for ThreadsafeCastableFactory +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ + #[cfg(not(tarpaulin_include))] + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + let ret = type_name::>(); + + formatter.write_fmt(format_args!( + "ThreadsafeCastableFactory (&AsyncDIContainer) -> {ret} {{ ... }}", + )) + } +} + +#[cfg(test)] +mod tests +{ + use super::*; + use crate::di_container::asynchronous::MockAsyncDIContainer; + + #[derive(Debug, PartialEq, Eq)] + struct Bacon + { + heal_amount: u32, + } + + #[test] + fn can_call() + { + let castable_factory = + ThreadsafeCastableFactory::new(&|_: &MockAsyncDIContainer| { + TransientPtr::new(Bacon { heal_amount: 27 }) + }); + + let mock_di_container = MockAsyncDIContainer::new(); + + let output = castable_factory.call(&mock_di_container); + + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); + } +} 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 + Send + Sync; /// # /// # #[tokio::main] @@ -175,7 +173,7 @@ where Interface: Fn + Send + Sync, FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn + 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 @@ -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 @@ -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::>() - .map_err(|err| match err { - CastError::NotArcCastable(_) => { - AsyncDIContainerError::InterfaceNotAsync( - type_name::(), - ) - } - CastError::CastFailed { - source: _, - from: _, - to: _, - } - | CastError::GetCasterFailed(_) => { - AsyncDIContainerError::CastFailed { - interface: type_name::(), - binding_kind: "factory", - } - } + .as_any() + .downcast_ref::>() + .ok_or_else(|| AsyncDIContainerError::CastFailed { + interface: type_name::(), + 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 = dyn IThreadsafeFactory< - dyn Fn<(), Output = TransientPtr> + Send + Sync, + type DefaultFactoryFn = ThreadsafeCastableFactory< + dyn Fn() -> TransientPtr + Send + Sync, AsyncDIContainer, >; - let default_factory = Self::cast_factory_binding::< - DefaultFactoryFn, - >(binding, "default factory")?; + let default_factory = binding + .as_any() + .downcast_ref::>() + .ok_or_else(|| AsyncDIContainerError::CastFailed { + interface: type_name::>(), + 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 = dyn IThreadsafeFactory< + type AsyncDefaultFactoryFn = ThreadsafeCastableFactory< dyn Fn<(), Output = BoxFuture<'static, TransientPtr>> + Send + Sync, AsyncDIContainer, >; - let async_default_factory = Self::cast_factory_binding::< - AsyncDefaultFactoryFn, - >( - binding, "async default factory" - )?; + let async_default_factory = binding + .as_any() + .downcast_ref::>() + .ok_or_else(|| AsyncDIContainerError::CastFailed { + interface: type_name::>(), + binding_kind: "async default factory", + })?; Ok(SomePtr::Transient(async_default_factory.call(self)().await)) } } } - #[cfg(feature = "factory")] - fn cast_factory_binding( - factory_binding: std::sync::Arc< - dyn crate::private::any_factory::AnyThreadsafeFactory, - >, - binding_kind: &'static str, - ) -> Result, AsyncDIContainerError> - { - factory_binding.cast::().map_err(|err| match err { - CastError::NotArcCastable(_) => { - AsyncDIContainerError::InterfaceNotAsync(type_name::()) - } - CastError::CastFailed { - source: _, - from: _, - to: _, - } - | CastError::GetCasterFailed(_) => AsyncDIContainerError::CastFailed { - interface: type_name::(), - binding_kind, - }, - }) - } - async fn get_binding_providable( &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) -> TransientPtr + 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) -> TransientPtr + 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; /// # - /// # #[factory] /// # type ICustomerIDFactory = dyn Fn(u32) -> TransientPtr; /// # /// # fn main() -> Result<(), Box> @@ -183,7 +181,7 @@ where Interface: Fn>, Func: Fn(&DIContainer) -> Box, { - 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>, >, { - 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; @@ -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::>() - .map_err(|_| DIContainerError::CastFailed { + .as_any() + .downcast_ref::>() + .ok_or_else(|| DIContainerError::CastFailed { interface: type_name::(), 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 = dyn IFactory< - dyn Fn<(), Output = TransientPtr>, - DIContainer, - >; + type DefaultFactoryFn = + CastableFactory TransientPtr, DIContainer>; let default_factory = factory_binding - .cast::>() - .map_err(|_| DIContainerError::CastFailed { + .as_any() + .downcast_ref::>() + .ok_or_else(|| DIContainerError::CastFailed { interface: type_name::(), 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) -> TransientPtr; 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) -> TransientPtr; let mut di_container = DIContainer::new(); diff --git a/src/lib.rs b/src/lib.rs index 7f4738d..d93acc8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,9 +102,6 @@ pub mod future; #[cfg_attr(doc_cfg, doc(cfg(feature = "async")))] pub use di_container::asynchronous::AsyncDIContainer; pub use di_container::blocking::DIContainer; -#[cfg(feature = "factory")] -#[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] -pub use syrette_macros::{declare_default_factory, factory}; pub use syrette_macros::{declare_interface, injectable, named}; #[doc(hidden)] @@ -113,6 +110,12 @@ pub mod private; mod provider; mod util; +#[cfg(feature = "factory")] +mod castable_factory; + +#[cfg(feature = "factory")] +mod any_factory; + #[cfg(test)] #[cfg(not(tarpaulin_include))] mod test_utils; diff --git a/src/private/any_factory.rs b/src/private/any_factory.rs deleted file mode 100644 index 64af57e..0000000 --- a/src/private/any_factory.rs +++ /dev/null @@ -1,11 +0,0 @@ -//! Interface for any factory to ever exist. - -use std::fmt::Debug; - -use crate::private::cast::{CastFrom, CastFromArc}; - -/// Interface for any factory to ever exist. -pub trait AnyFactory: CastFrom + Debug {} - -/// Interface for any threadsafe factory to ever exist. -pub trait AnyThreadsafeFactory: CastFromArc + Debug {} diff --git a/src/private/castable_factory/mod.rs b/src/private/castable_factory/mod.rs deleted file mode 100644 index 2ac5918..0000000 --- a/src/private/castable_factory/mod.rs +++ /dev/null @@ -1,91 +0,0 @@ -use std::any::type_name; -use std::fmt::Debug; - -use crate::private::any_factory::AnyFactory; -use crate::private::factory::IFactory; -use crate::ptr::TransientPtr; - -#[cfg(feature = "async")] -pub mod threadsafe; - -pub struct CastableFactory -where - ReturnInterface: 'static + ?Sized, - DIContainerT: 'static, -{ - func: &'static dyn Fn(&DIContainerT) -> TransientPtr, -} - -impl CastableFactory -where - ReturnInterface: 'static + ?Sized, -{ - pub fn new( - func: &'static dyn Fn(&DIContainerT) -> TransientPtr, - ) -> Self - { - Self { func } - } -} - -impl IFactory - for CastableFactory -where - ReturnInterface: 'static + ?Sized, -{ - fn call(&self, di_container: &DIContainerT) -> TransientPtr - { - (self.func)(di_container) - } -} - -impl AnyFactory - for CastableFactory -where - ReturnInterface: 'static + ?Sized, - DIContainerT: 'static, -{ -} - -impl Debug - for CastableFactory -where - ReturnInterface: 'static + ?Sized, -{ - #[cfg(not(tarpaulin_include))] - fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result - { - let ret = type_name::>(); - - formatter.write_fmt(format_args!( - "CastableFactory (&DIContainer) -> {ret} {{ ... }}" - )) - } -} - -#[cfg(test)] -mod tests -{ - use super::*; - use crate::di_container::blocking::MockDIContainer; - - #[derive(Debug, PartialEq, Eq)] - struct Bacon - { - heal_amount: u32, - } - - #[test] - fn can_call() - { - let castable_factory = CastableFactory::new(&|_: &MockDIContainer| { - TransientPtr::new(Bacon { heal_amount: 27 }) - }); - - let mock_di_container = MockDIContainer::new(); - - let output = castable_factory.call(&mock_di_container); - - assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); - } -} diff --git a/src/private/castable_factory/threadsafe.rs b/src/private/castable_factory/threadsafe.rs deleted file mode 100644 index 6e8da42..0000000 --- a/src/private/castable_factory/threadsafe.rs +++ /dev/null @@ -1,103 +0,0 @@ -use std::any::type_name; -use std::fmt::Debug; - -use crate::private::any_factory::{AnyFactory, AnyThreadsafeFactory}; -use crate::private::factory::IThreadsafeFactory; -use crate::ptr::TransientPtr; - -pub struct ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - func: &'static (dyn Fn(&DIContainerT) -> TransientPtr + Send + Sync), -} - -impl - ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - pub fn new( - func: &'static (dyn Fn(&DIContainerT) -> TransientPtr - + Send - + Sync), - ) -> Self - { - Self { func } - } -} - -impl IThreadsafeFactory - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - fn call(&self, di_container: &DIContainerT) -> TransientPtr - { - (self.func)(di_container) - } -} - -impl AnyFactory - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ -} - -impl AnyThreadsafeFactory - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ -} - -impl Debug - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - #[cfg(not(tarpaulin_include))] - fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result - { - let ret = type_name::>(); - - formatter.write_fmt(format_args!( - "ThreadsafeCastableFactory (&AsyncDIContainer) -> {ret} {{ ... }}", - )) - } -} - -#[cfg(test)] -mod tests -{ - use super::*; - use crate::di_container::asynchronous::MockAsyncDIContainer; - - #[derive(Debug, PartialEq, Eq)] - struct Bacon - { - heal_amount: u32, - } - - #[test] - fn can_call() - { - let castable_factory = - ThreadsafeCastableFactory::new(&|_: &MockAsyncDIContainer| { - TransientPtr::new(Bacon { heal_amount: 27 }) - }); - - let mock_di_container = MockAsyncDIContainer::new(); - - let output = castable_factory.call(&mock_di_container); - - assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); - } -} diff --git a/src/private/factory.rs b/src/private/factory.rs deleted file mode 100644 index 7191c2c..0000000 --- a/src/private/factory.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::private::cast::CastFrom; -use crate::ptr::TransientPtr; - -/// Interface for a factory. -pub trait IFactory: CastFrom -where - ReturnInterface: 'static + ?Sized, -{ - fn call(&self, di_container: &DIContainerT) -> TransientPtr; -} - -/// Interface for a threadsafe factory. -#[cfg(feature = "async")] -pub trait IThreadsafeFactory: - crate::private::cast::CastFromArc -where - ReturnInterface: 'static + ?Sized, -{ - fn call(&self, di_container: &DIContainerT) -> TransientPtr; -} diff --git a/src/private/mod.rs b/src/private/mod.rs index 8b20333..9b03ce8 100644 --- a/src/private/mod.rs +++ b/src/private/mod.rs @@ -4,12 +4,3 @@ pub mod cast; pub extern crate linkme; - -#[cfg(feature = "factory")] -pub mod any_factory; - -#[cfg(feature = "factory")] -pub mod factory; - -#[cfg(feature = "factory")] -pub mod castable_factory; diff --git a/src/provider/async.rs b/src/provider/async.rs index a3db57e..3875363 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -16,21 +16,15 @@ pub enum AsyncProvidable Singleton(ThreadsafeSingletonPtr>), #[cfg(feature = "factory")] Factory( - crate::ptr::ThreadsafeFactoryPtr< - dyn crate::private::any_factory::AnyThreadsafeFactory, - >, + crate::ptr::ThreadsafeFactoryPtr, ), #[cfg(feature = "factory")] DefaultFactory( - crate::ptr::ThreadsafeFactoryPtr< - dyn crate::private::any_factory::AnyThreadsafeFactory, - >, + crate::ptr::ThreadsafeFactoryPtr, ), #[cfg(feature = "factory")] AsyncDefaultFactory( - crate::ptr::ThreadsafeFactoryPtr< - dyn crate::private::any_factory::AnyThreadsafeFactory, - >, + crate::ptr::ThreadsafeFactoryPtr, ), } @@ -188,9 +182,8 @@ pub enum AsyncFactoryVariant #[cfg(feature = "factory")] pub struct AsyncFactoryProvider { - factory: crate::ptr::ThreadsafeFactoryPtr< - dyn crate::private::any_factory::AnyThreadsafeFactory, - >, + factory: + crate::ptr::ThreadsafeFactoryPtr, variant: AsyncFactoryVariant, } @@ -199,7 +192,7 @@ impl AsyncFactoryProvider { pub fn new( factory: crate::ptr::ThreadsafeFactoryPtr< - dyn crate::private::any_factory::AnyThreadsafeFactory, + dyn crate::any_factory::AnyThreadsafeFactory, >, variant: AsyncFactoryVariant, ) -> Self @@ -307,12 +300,22 @@ mod tests #[cfg(feature = "factory")] async fn async_factory_provider_works() { - use crate::private::any_factory::AnyThreadsafeFactory; + use std::any::Any; + + use crate::any_factory::{AnyFactory, AnyThreadsafeFactory}; use crate::ptr::ThreadsafeFactoryPtr; #[derive(Debug)] struct FooFactory; + impl AnyFactory for FooFactory + { + fn as_any(&self) -> &dyn Any + { + self + } + } + impl AnyThreadsafeFactory for FooFactory {} let factory_provider = AsyncFactoryProvider::new( diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index 5710a65..bde3be5 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -13,9 +13,9 @@ pub enum Providable Transient(TransientPtr>), Singleton(SingletonPtr>), #[cfg(feature = "factory")] - Factory(crate::ptr::FactoryPtr), + Factory(crate::ptr::FactoryPtr), #[cfg(feature = "factory")] - DefaultFactory(crate::ptr::FactoryPtr), + DefaultFactory(crate::ptr::FactoryPtr), } #[cfg_attr(test, mockall::automock)] @@ -108,7 +108,7 @@ where #[cfg(feature = "factory")] pub struct FactoryProvider { - factory: crate::ptr::FactoryPtr, + factory: crate::ptr::FactoryPtr, is_default_factory: bool, } @@ -116,7 +116,7 @@ pub struct FactoryProvider impl FactoryProvider { pub fn new( - factory: crate::ptr::FactoryPtr, + factory: crate::ptr::FactoryPtr, is_default_factory: bool, ) -> Self { @@ -196,13 +196,21 @@ mod tests #[cfg(feature = "factory")] fn factory_provider_works() { - use crate::private::any_factory::AnyFactory; + use std::any::Any; + + use crate::any_factory::AnyFactory; use crate::ptr::FactoryPtr; #[derive(Debug)] struct FooFactory; - impl AnyFactory for FooFactory {} + impl AnyFactory for FooFactory + { + fn as_any(&self) -> &dyn Any + { + self + } + } let factory_provider = FactoryProvider::new(FactoryPtr::new(FooFactory), false); let default_factory_provider = -- cgit v1.2.3-18-g5258