From 20d37eb93060e51970d3791c6c173e07ef5ad489 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 11 Jul 2024 21:00:09 +0200 Subject: refactor: rename castable factory to castable function --- src/castable_factory/mod.rs | 93 -------------------- src/castable_factory/threadsafe.rs | 102 ---------------------- src/castable_function/mod.rs | 93 ++++++++++++++++++++ src/castable_function/threadsafe.rs | 105 +++++++++++++++++++++++ src/di_container/asynchronous/binding/builder.rs | 16 ++-- src/di_container/asynchronous/mod.rs | 20 ++--- src/di_container/blocking/binding/builder.rs | 8 +- src/di_container/blocking/mod.rs | 16 ++-- src/lib.rs | 2 +- src/provider/async.rs | 18 ++-- src/provider/blocking.rs | 14 +-- 11 files changed, 246 insertions(+), 241 deletions(-) delete mode 100644 src/castable_factory/mod.rs delete mode 100644 src/castable_factory/threadsafe.rs create mode 100644 src/castable_function/mod.rs create mode 100644 src/castable_function/threadsafe.rs diff --git a/src/castable_factory/mod.rs b/src/castable_factory/mod.rs deleted file mode 100644 index 0cb2127..0000000 --- a/src/castable_factory/mod.rs +++ /dev/null @@ -1,93 +0,0 @@ -use std::any::{type_name, Any}; -use std::fmt::Debug; - -use crate::ptr::TransientPtr; - -#[cfg(feature = "async")] -pub mod threadsafe; - -/// Interface for any castable factory. -pub trait AnyCastableFactory: Any + Debug -{ - fn as_any(&self) -> &dyn Any; -} - -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 AnyCastableFactory - 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 deleted file mode 100644 index 8b1e66d..0000000 --- a/src/castable_factory/threadsafe.rs +++ /dev/null @@ -1,102 +0,0 @@ -use std::any::{type_name, Any}; -use std::fmt::Debug; - -use crate::castable_factory::AnyCastableFactory; -use crate::ptr::TransientPtr; - -/// Interface for any threadsafe castable factory. -pub trait AnyThreadsafeCastableFactory: AnyCastableFactory + Send + Sync + Debug {} - -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 AnyCastableFactory - for ThreadsafeCastableFactory -where - DIContainerT: 'static, - ReturnInterface: 'static + ?Sized, -{ - fn as_any(&self) -> &dyn Any - { - self - } -} - -impl AnyThreadsafeCastableFactory - 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/castable_function/mod.rs b/src/castable_function/mod.rs new file mode 100644 index 0000000..4c2f0db --- /dev/null +++ b/src/castable_function/mod.rs @@ -0,0 +1,93 @@ +use std::any::{type_name, Any}; +use std::fmt::Debug; + +use crate::ptr::TransientPtr; + +#[cfg(feature = "async")] +pub mod threadsafe; + +/// Interface for any castable function. +pub trait AnyCastableFunction: Any + Debug +{ + fn as_any(&self) -> &dyn Any; +} + +pub struct CastableFunction +where + ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, +{ + func: &'static dyn Fn(&DIContainerT) -> TransientPtr, +} + +impl CastableFunction +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 AnyCastableFunction + for CastableFunction +where + ReturnInterface: 'static + ?Sized, + DIContainerT: 'static, +{ + fn as_any(&self) -> &dyn Any + { + self + } +} + +impl Debug + for CastableFunction +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!( + "CastableFunction (&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_func = CastableFunction::new(&|_: &MockDIContainer| { + TransientPtr::new(Bacon { heal_amount: 27 }) + }); + + let mock_di_container = MockDIContainer::new(); + + let output = castable_func.call(&mock_di_container); + + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); + } +} diff --git a/src/castable_function/threadsafe.rs b/src/castable_function/threadsafe.rs new file mode 100644 index 0000000..7543396 --- /dev/null +++ b/src/castable_function/threadsafe.rs @@ -0,0 +1,105 @@ +use std::any::{type_name, Any}; +use std::fmt::Debug; + +use crate::castable_function::AnyCastableFunction; +use crate::ptr::TransientPtr; + +/// Interface for any threadsafe castable function. +pub trait AnyThreadsafeCastableFunction: + AnyCastableFunction + Send + Sync + Debug +{ +} + +pub struct ThreadsafeCastableFunction +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ + func: &'static (dyn Fn(&DIContainerT) -> TransientPtr + Send + Sync), +} + +impl + ThreadsafeCastableFunction +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 AnyCastableFunction + for ThreadsafeCastableFunction +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ + fn as_any(&self) -> &dyn Any + { + self + } +} + +impl AnyThreadsafeCastableFunction + for ThreadsafeCastableFunction +where + DIContainerT: 'static, + ReturnInterface: 'static + ?Sized, +{ +} + +impl Debug + for ThreadsafeCastableFunction +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!( + "ThreadsafeCastableFunction(&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_function = + ThreadsafeCastableFunction::new(&|_: &MockAsyncDIContainer| { + TransientPtr::new(Bacon { heal_amount: 27 }) + }); + + let mock_di_container = MockAsyncDIContainer::new(); + + let output = castable_function.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 f42e6a1..8465c9a 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -173,7 +173,7 @@ where Interface: Fn + Send + Sync, FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn + Send + Sync, { - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -186,7 +186,7 @@ where ))); } - let factory_impl = ThreadsafeCastableFactory::new(factory_func); + let factory_impl = ThreadsafeCastableFunction::new(factory_func); self.di_container.set_binding::( BindingOptions::new(), @@ -270,7 +270,7 @@ where + Send + Sync, { - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -283,7 +283,7 @@ where ))); } - let factory_impl = ThreadsafeCastableFactory::new(factory_func); + let factory_impl = ThreadsafeCastableFunction::new(factory_func); self.di_container.set_binding::( BindingOptions::new(), @@ -354,7 +354,7 @@ where + Send + Sync, { - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -367,7 +367,7 @@ where ))); } - let factory_impl = ThreadsafeCastableFactory::new(factory_func); + let factory_impl = ThreadsafeCastableFunction::new(factory_func); self.di_container.set_binding::( BindingOptions::new(), @@ -445,7 +445,7 @@ where + Send + Sync, { - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; use crate::provider::r#async::AsyncFactoryVariant; if self @@ -458,7 +458,7 @@ where ))); } - let factory_impl = ThreadsafeCastableFactory::new(factory_func); + let factory_impl = ThreadsafeCastableFunction::new(factory_func); self.di_container.set_binding::( BindingOptions::new(), diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 3e29ef6..c993b8b 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -348,11 +348,11 @@ impl AsyncDIContainer } #[cfg(feature = "factory")] AsyncProvidable::Factory(factory_binding) => { - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; let factory = factory_binding .as_any() - .downcast_ref::>() + .downcast_ref::>() .ok_or_else(|| AsyncDIContainerError::CastFailed { interface: type_name::(), binding_kind: "factory", @@ -362,10 +362,10 @@ impl AsyncDIContainer } #[cfg(feature = "factory")] AsyncProvidable::DefaultFactory(binding) => { - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; use crate::ptr::TransientPtr; - type DefaultFactoryFn = ThreadsafeCastableFactory< + type DefaultFactoryFn = ThreadsafeCastableFunction< dyn Fn() -> TransientPtr + Send + Sync, AsyncDIContainer, >; @@ -382,11 +382,11 @@ impl AsyncDIContainer } #[cfg(feature = "factory")] AsyncProvidable::AsyncDefaultFactory(binding) => { - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; use crate::future::BoxFuture; use crate::ptr::TransientPtr; - type AsyncDefaultFactoryFn = ThreadsafeCastableFactory< + type AsyncDefaultFactoryFn = ThreadsafeCastableFunction< dyn Fn<(), Output = BoxFuture<'static, TransientPtr>> + Send + Sync, @@ -652,7 +652,7 @@ mod tests } } - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr + Send + Sync; @@ -674,7 +674,7 @@ mod tests inner_mock_provider.expect_provide().returning(|_, _| { Ok(AsyncProvidable::Factory( crate::ptr::ThreadsafeFactoryPtr::new( - ThreadsafeCastableFactory::new(factory_func), + ThreadsafeCastableFunction::new(factory_func), ), )) }); @@ -734,7 +734,7 @@ mod tests } } - use crate::castable_factory::threadsafe::ThreadsafeCastableFactory; + use crate::castable_function::threadsafe::ThreadsafeCastableFunction; type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr + Send + Sync; @@ -756,7 +756,7 @@ mod tests inner_mock_provider.expect_provide().returning(|_, _| { Ok(AsyncProvidable::Factory( crate::ptr::ThreadsafeFactoryPtr::new( - ThreadsafeCastableFactory::new(factory_func), + ThreadsafeCastableFunction::new(factory_func), ), )) }); diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 9f7f6f9..ead1a54 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -181,7 +181,7 @@ where Interface: Fn>, Func: Fn(&DIContainer) -> Box, { - use crate::castable_factory::CastableFactory; + use crate::castable_function::CastableFunction; if self .di_container @@ -192,7 +192,7 @@ where >())); } - let factory_impl = CastableFactory::new(factory_func); + let factory_impl = CastableFunction::new(factory_func); self.di_container.set_binding::( BindingOptions::new(), @@ -269,7 +269,7 @@ where dyn Fn<(), Output = crate::ptr::TransientPtr>, >, { - use crate::castable_factory::CastableFactory; + use crate::castable_function::CastableFunction; if self .di_container @@ -280,7 +280,7 @@ where >())); } - let factory_impl = CastableFactory::new(factory_func); + let factory_impl = CastableFunction::new(factory_func); self.di_container.set_binding::( BindingOptions::new(), diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index d9efe94..d8b0d59 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -285,11 +285,11 @@ impl DIContainer )), #[cfg(feature = "factory")] Providable::Factory(factory_binding) => { - use crate::castable_factory::CastableFactory; + use crate::castable_function::CastableFunction; let factory = factory_binding .as_any() - .downcast_ref::>() + .downcast_ref::>() .ok_or_else(|| DIContainerError::CastFailed { interface: type_name::(), binding_kind: "factory", @@ -299,11 +299,11 @@ impl DIContainer } #[cfg(feature = "factory")] Providable::DefaultFactory(factory_binding) => { - use crate::castable_factory::CastableFactory; + use crate::castable_function::CastableFunction; use crate::ptr::TransientPtr; type DefaultFactoryFn = - CastableFactory TransientPtr, DIContainer>; + CastableFunction TransientPtr, DIContainer>; let default_factory = factory_binding .as_any() @@ -517,7 +517,7 @@ mod tests #[cfg(feature = "factory")] fn can_get_factory() { - use crate::castable_factory::CastableFactory; + use crate::castable_function::CastableFunction; use crate::ptr::FactoryPtr; trait IUserManager @@ -572,7 +572,7 @@ mod tests let mut mock_provider = MockIProvider::new(); mock_provider.expect_provide().returning_st(|_, _| { - Ok(Providable::Factory(FactoryPtr::new(CastableFactory::new( + Ok(Providable::Factory(FactoryPtr::new(CastableFunction::new( factory_func, )))) }); @@ -592,7 +592,7 @@ mod tests #[cfg(feature = "factory")] fn can_get_factory_named() { - use crate::castable_factory::CastableFactory; + use crate::castable_function::CastableFunction; use crate::ptr::FactoryPtr; trait IUserManager @@ -647,7 +647,7 @@ mod tests let mut mock_provider = MockIProvider::new(); mock_provider.expect_provide().returning_st(|_, _| { - Ok(Providable::Factory(FactoryPtr::new(CastableFactory::new( + Ok(Providable::Factory(FactoryPtr::new(CastableFunction::new( factory_func, )))) }); diff --git a/src/lib.rs b/src/lib.rs index 5544faa..d061be6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,7 +111,7 @@ mod provider; mod util; #[cfg(feature = "factory")] -mod castable_factory; +mod castable_function; #[cfg(test)] #[cfg(not(tarpaulin_include))] diff --git a/src/provider/async.rs b/src/provider/async.rs index 6ccb082..68eed87 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -17,19 +17,19 @@ pub enum AsyncProvidable #[cfg(feature = "factory")] Factory( crate::ptr::ThreadsafeFactoryPtr< - dyn crate::castable_factory::threadsafe::AnyThreadsafeCastableFactory, + dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, ), #[cfg(feature = "factory")] DefaultFactory( crate::ptr::ThreadsafeFactoryPtr< - dyn crate::castable_factory::threadsafe::AnyThreadsafeCastableFactory, + dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, ), #[cfg(feature = "factory")] AsyncDefaultFactory( crate::ptr::ThreadsafeFactoryPtr< - dyn crate::castable_factory::threadsafe::AnyThreadsafeCastableFactory, + dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, ), } @@ -189,7 +189,7 @@ pub enum AsyncFactoryVariant pub struct AsyncFactoryProvider { factory: crate::ptr::ThreadsafeFactoryPtr< - dyn crate::castable_factory::threadsafe::AnyThreadsafeCastableFactory, + dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, variant: AsyncFactoryVariant, } @@ -199,7 +199,7 @@ impl AsyncFactoryProvider { pub fn new( factory: crate::ptr::ThreadsafeFactoryPtr< - dyn crate::castable_factory::threadsafe::AnyThreadsafeCastableFactory, + dyn crate::castable_function::threadsafe::AnyThreadsafeCastableFunction, >, variant: AsyncFactoryVariant, ) -> Self @@ -309,14 +309,14 @@ mod tests { use std::any::Any; - use crate::castable_factory::threadsafe::AnyThreadsafeCastableFactory; - use crate::castable_factory::AnyCastableFactory; + use crate::castable_function::threadsafe::AnyThreadsafeCastableFunction; + use crate::castable_function::AnyCastableFunction; use crate::ptr::ThreadsafeFactoryPtr; #[derive(Debug)] struct FooFactory; - impl AnyCastableFactory for FooFactory + impl AnyCastableFunction for FooFactory { fn as_any(&self) -> &dyn Any { @@ -324,7 +324,7 @@ mod tests } } - impl AnyThreadsafeCastableFactory for FooFactory {} + impl AnyThreadsafeCastableFunction for FooFactory {} let factory_provider = AsyncFactoryProvider::new( ThreadsafeFactoryPtr::new(FooFactory), diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index 65e315d..6475dc7 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -13,10 +13,10 @@ 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, + crate::ptr::FactoryPtr, ), } @@ -110,7 +110,7 @@ where #[cfg(feature = "factory")] pub struct FactoryProvider { - factory: crate::ptr::FactoryPtr, + factory: crate::ptr::FactoryPtr, is_default_factory: bool, } @@ -118,7 +118,9 @@ pub struct FactoryProvider impl FactoryProvider { pub fn new( - factory: crate::ptr::FactoryPtr, + factory: crate::ptr::FactoryPtr< + dyn crate::castable_function::AnyCastableFunction, + >, is_default_factory: bool, ) -> Self { @@ -200,13 +202,13 @@ mod tests { use std::any::Any; - use crate::castable_factory::AnyCastableFactory; + use crate::castable_function::AnyCastableFunction; use crate::ptr::FactoryPtr; #[derive(Debug)] struct FooFactory; - impl AnyCastableFactory for FooFactory + impl AnyCastableFunction for FooFactory { fn as_any(&self) -> &dyn Any { -- cgit v1.2.3-18-g5258