From d46181de1c19328ff8f3f6a12784cf14c53e9e71 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 15 Sep 2024 23:33:33 +0200 Subject: refactor!: rename to_*default_factory functions to to_*dynamic_value BREAKING CHANGE: The functions to_default_factory and to_async_default_factory have been renamed to to_dynamic_value and to_async_dynamic_value, respectively --- examples/async-factory/main.rs | 2 +- examples/async/bootstrap.rs | 2 +- examples/with-3rd-party/bootstrap.rs | 2 +- src/di_container/asynchronous.rs | 26 ++++++++------- src/di_container/asynchronous/binding/builder.rs | 40 ++++++++++++------------ src/di_container/blocking.rs | 6 ++-- src/di_container/blocking/binding/builder.rs | 20 ++++++------ 7 files changed, 50 insertions(+), 48 deletions(-) diff --git a/examples/async-factory/main.rs b/examples/async-factory/main.rs index 2b796cc..795e50c 100644 --- a/examples/async-factory/main.rs +++ b/examples/async-factory/main.rs @@ -84,7 +84,7 @@ async fn main() -> Result<()> di_container .bind::() - .to_async_default_factory(&|_| { + .to_async_dynamic_value(&|_| { Box::new(|| { Box::pin(async { // Do some time demanding thing... diff --git a/examples/async/bootstrap.rs b/examples/async/bootstrap.rs index 07e1bf8..5abeac7 100644 --- a/examples/async/bootstrap.rs +++ b/examples/async/bootstrap.rs @@ -20,7 +20,7 @@ pub async fn bootstrap() -> Result .in_singleton_scope() .await?; - di_container.bind::().to_default_factory(&|_| { + di_container.bind::().to_dynamic_value(&|_| { Box::new(|| { let cat: TransientPtr = TransientPtr::new(Cat::new()); diff --git a/examples/with-3rd-party/bootstrap.rs b/examples/with-3rd-party/bootstrap.rs index 26386f5..5d2f713 100644 --- a/examples/with-3rd-party/bootstrap.rs +++ b/examples/with-3rd-party/bootstrap.rs @@ -15,7 +15,7 @@ pub fn bootstrap() -> Result> di_container .bind::() - .to_default_factory(&|_| Box::new(|| TransientPtr::new(Shuriken::new())))?; + .to_dynamic_value(&|_| Box::new(|| TransientPtr::new(Shuriken::new())))?; Ok(di_container) } diff --git a/src/di_container/asynchronous.rs b/src/di_container/asynchronous.rs index c6308e6..a338c0a 100644 --- a/src/di_container/asynchronous.rs +++ b/src/di_container/asynchronous.rs @@ -371,20 +371,20 @@ impl AsyncDIContainer use crate::castable_function::threadsafe::ThreadsafeCastableFunction; use crate::ptr::TransientPtr; - type DefaultFactoryFn = ThreadsafeCastableFunction< + type Func = ThreadsafeCastableFunction< dyn Fn() -> TransientPtr + Send + Sync, AsyncDIContainer, >; - let default_factory = func_bound + let dynamic_val_func = func_bound .as_any() - .downcast_ref::>() + .downcast_ref::>() .ok_or_else(|| AsyncDIContainerError::CastFailed { - interface: type_name::>(), - binding_kind: "default factory", + interface: type_name::>(), + binding_kind: "dynamic value func", })?; - Ok(SomePtr::Transient(default_factory.call(self)())) + Ok(SomePtr::Transient(dynamic_val_func.call(self)())) } #[cfg(feature = "factory")] AsyncProvidable::Function( @@ -395,22 +395,24 @@ impl AsyncDIContainer use crate::future::BoxFuture; use crate::ptr::TransientPtr; - type AsyncDefaultFactoryFn = ThreadsafeCastableFunction< + type Func = ThreadsafeCastableFunction< dyn Fn<(), Output = BoxFuture<'static, TransientPtr>> + Send + Sync, AsyncDIContainer, >; - let async_default_factory = func_bound + let async_dynamic_value_func = func_bound .as_any() - .downcast_ref::>() + .downcast_ref::>() .ok_or_else(|| AsyncDIContainerError::CastFailed { - interface: type_name::>(), - binding_kind: "async default factory", + interface: type_name::>(), + binding_kind: "async dynamic value function", })?; - Ok(SomePtr::Transient(async_default_factory.call(self)().await)) + Ok(SomePtr::Transient( + async_dynamic_value_func.call(self)().await, + )) } } } diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index 833517b..6f1281d 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -300,8 +300,8 @@ where Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } - /// Creates a binding of type `Interface` to a factory that takes no arguments - /// inside of the associated [`AsyncDIContainer`]. + /// Creates a binding of type `Interface` to a value resolved using the given + /// function. /// /// # Errors /// Will return Err if the associated [`AsyncDIContainer`] already have a binding @@ -329,7 +329,7 @@ where /// # { /// # let mut di_container = AsyncDIContainer::new(); /// # - /// di_container.bind::().to_default_factory(&|_| { + /// di_container.bind::().to_dynamic_value(&|_| { /// Box::new(|| { /// let bar = TransientPtr::new(Bar { /// num: 42, @@ -345,16 +345,16 @@ where /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub fn to_default_factory( + pub fn to_dynamic_value( self, - factory_func: &'static FactoryFunc, + func: &'static Func, ) -> Result< AsyncBindingWhenConfigurator<'di_container, Interface>, AsyncBindingBuilderError, > where Return: 'static + ?Sized, - FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<(), crate::ptr::TransientPtr> + Func: Fn(&AsyncDIContainer) -> BoxFn<(), crate::ptr::TransientPtr> + Send + Sync, { @@ -373,12 +373,12 @@ where ))); } - let factory_impl = ThreadsafeCastableFunction::new(factory_func); + let castable_func = ThreadsafeCastableFunction::new(func); self.di_container.set_binding::( BindingOptions::new(), Box::new(crate::provider::r#async::AsyncFunctionProvider::new( - Arc::new(factory_impl), + Arc::new(castable_func), ProvidableFunctionKind::Instant, )), ); @@ -386,8 +386,8 @@ where Ok(AsyncBindingWhenConfigurator::new(self.di_container)) } - /// Creates a binding of factory type `Interface` to a async factory inside of the - /// associated [`AsyncDIContainer`]. + /// Creates a binding of type `Interface` to a value resolved using the given + /// async function. /// /// # Errors /// Will return Err if the associated [`AsyncDIContainer`] already have a binding @@ -418,7 +418,7 @@ where /// # /// di_container /// .bind::() - /// .to_async_default_factory(&|_| { + /// .to_async_dynamic_value(&|_| { /// Box::new(|| { /// Box::pin(async { /// let bar = TransientPtr::new(Bar { @@ -438,16 +438,16 @@ where /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub fn to_async_default_factory( + pub fn to_async_dynamic_value( self, - factory_func: &'static FactoryFunc, + func: &'static Func, ) -> Result< AsyncBindingWhenConfigurator<'di_container, Interface>, AsyncBindingBuilderError, > where Return: 'static + ?Sized, - FactoryFunc: Fn(&AsyncDIContainer) -> BoxFn<(), crate::future::BoxFuture<'static, Return>> + Func: Fn(&AsyncDIContainer) -> BoxFn<(), crate::future::BoxFuture<'static, Return>> + Send + Sync, { @@ -466,12 +466,12 @@ where ))); } - let factory_impl = ThreadsafeCastableFunction::new(factory_func); + let castable_func = ThreadsafeCastableFunction::new(func); self.di_container.set_binding::( BindingOptions::new(), Box::new(crate::provider::r#async::AsyncFunctionProvider::new( - Arc::new(factory_impl), + Arc::new(castable_func), ProvidableFunctionKind::AsyncInstant, )), ); @@ -608,7 +608,7 @@ mod tests #[tokio::test] #[cfg(feature = "factory")] - async fn can_bind_to_default_factory() + async fn can_bind_to_dynamic_value() { use crate::ptr::TransientPtr; @@ -633,7 +633,7 @@ mod tests ); binding_builder - .to_default_factory(&|_| { + .to_dynamic_value(&|_| { Box::new(|| { let user_manager: TransientPtr = TransientPtr::new(subjects_async::UserManager::new()); @@ -646,7 +646,7 @@ mod tests #[tokio::test] #[cfg(feature = "factory")] - async fn can_bind_to_async_default_factory() + async fn can_bind_to_async_dynamic_value() { use crate::ptr::TransientPtr; use crate::test_utils::async_closure; @@ -672,7 +672,7 @@ mod tests ); binding_builder - .to_async_default_factory(&|_| { + .to_async_dynamic_value(&|_| { async_closure!(|| { let user_manager: TransientPtr = TransientPtr::new(subjects_async::UserManager::new()); diff --git a/src/di_container/blocking.rs b/src/di_container/blocking.rs index fa3523b..3d79ae7 100644 --- a/src/di_container/blocking.rs +++ b/src/di_container/blocking.rs @@ -311,15 +311,15 @@ impl DIContainer type Func = CastableFunction TransientPtr, DIContainer>; - let default_factory = func_bound + let dynamic_val_func = func_bound .as_any() .downcast_ref::>() .ok_or_else(|| DIContainerError::CastFailed { interface: type_name::(), - binding_kind: "default factory", + binding_kind: "dynamic value function", })?; - Ok(SomePtr::Transient(default_factory.call(self)())) + Ok(SomePtr::Transient(dynamic_val_func.call(self)())) } } } diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 345fb02..558db6e 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -208,8 +208,8 @@ where Ok(BindingWhenConfigurator::new(self.di_container)) } - /// Creates a binding of type `Interface` to a factory that takes no arguments - /// inside of the associated [`DIContainer`]. + /// Creates a binding of type `Interface` to a value resolved using the given + /// function. /// /// # Errors /// Will return Err if the associated [`DIContainer`] already have a binding for @@ -247,7 +247,7 @@ where /// # { /// # let mut di_container = DIContainer::new(); /// # - /// di_container.bind::().to_default_factory(&|_| { + /// di_container.bind::().to_dynamic_value(&|_| { /// Box::new(|| { /// let buffer = TransientPtr::new(Buffer::::new()); /// @@ -260,13 +260,13 @@ where /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub fn to_default_factory( + pub fn to_dynamic_value( self, - factory_func: &'static FactoryFunc, + func: &'static Func, ) -> Result, BindingBuilderError> where Return: 'static + ?Sized, - FactoryFunc: Fn( + Func: Fn( &DIContainer, ) -> crate::ptr::TransientPtr< dyn Fn<(), Output = crate::ptr::TransientPtr>, @@ -286,12 +286,12 @@ where >())); } - let factory_impl = CastableFunction::new(factory_func); + let castable_func = CastableFunction::new(func); self.di_container.set_binding::( BindingOptions::new(), Box::new(crate::provider::blocking::FunctionProvider::new( - Rc::new(factory_impl), + Rc::new(castable_func), ProvidableFunctionKind::Instant, )), ); @@ -377,7 +377,7 @@ mod tests #[test] #[cfg(feature = "factory")] - fn can_bind_to_default_factory() + fn can_bind_to_dynamic_value() { use crate::ptr::TransientPtr; @@ -401,7 +401,7 @@ mod tests ); binding_builder - .to_default_factory(&|_| { + .to_dynamic_value(&|_| { Box::new(move || { let user_manager: TransientPtr = TransientPtr::new(subjects::UserManager::new()); -- cgit v1.2.3-18-g5258