diff options
author | HampusM <hampus@hampusmat.com> | 2024-09-15 23:33:33 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-09-15 23:33:33 +0200 |
commit | d46181de1c19328ff8f3f6a12784cf14c53e9e71 (patch) | |
tree | 863bdd4e90ea2d9be66c3164222611cff2aca04a | |
parent | a34f7c03779aaf90f34b5ff59587daf1db42de8d (diff) |
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
-rw-r--r-- | examples/async-factory/main.rs | 2 | ||||
-rw-r--r-- | examples/async/bootstrap.rs | 2 | ||||
-rw-r--r-- | examples/with-3rd-party/bootstrap.rs | 2 | ||||
-rw-r--r-- | src/di_container/asynchronous.rs | 26 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/builder.rs | 40 | ||||
-rw-r--r-- | src/di_container/blocking.rs | 6 | ||||
-rw-r--r-- | 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::<dyn IPerson>() - .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<AsyncDIContainer, anyhow::Error> .in_singleton_scope() .await?; - di_container.bind::<dyn ICat>().to_default_factory(&|_| { + di_container.bind::<dyn ICat>().to_dynamic_value(&|_| { Box::new(|| { let cat: TransientPtr<dyn ICat> = 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<DIContainer, Box<dyn Error>> di_container .bind::<Shuriken>() - .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<Interface> = ThreadsafeCastableFunction< + type Func<Interface> = ThreadsafeCastableFunction< dyn Fn() -> TransientPtr<Interface> + Send + Sync, AsyncDIContainer, >; - let default_factory = func_bound + let dynamic_val_func = func_bound .as_any() - .downcast_ref::<DefaultFactoryFn<Interface>>() + .downcast_ref::<Func<Interface>>() .ok_or_else(|| AsyncDIContainerError::CastFailed { - interface: type_name::<DefaultFactoryFn<Interface>>(), - binding_kind: "default factory", + interface: type_name::<Func<Interface>>(), + 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<Interface> = ThreadsafeCastableFunction< + type Func<Interface> = ThreadsafeCastableFunction< dyn Fn<(), Output = BoxFuture<'static, TransientPtr<Interface>>> + Send + Sync, AsyncDIContainer, >; - let async_default_factory = func_bound + let async_dynamic_value_func = func_bound .as_any() - .downcast_ref::<AsyncDefaultFactoryFn<Interface>>() + .downcast_ref::<Func<Interface>>() .ok_or_else(|| AsyncDIContainerError::CastFailed { - interface: type_name::<AsyncDefaultFactoryFn<Interface>>(), - binding_kind: "async default factory", + interface: type_name::<Func<Interface>>(), + 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::<dyn Foo>().to_default_factory(&|_| { + /// di_container.bind::<dyn Foo>().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<Return, FactoryFunc>( + pub fn to_dynamic_value<Return, Func>( 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<Return>> + Func: Fn(&AsyncDIContainer) -> BoxFn<(), crate::ptr::TransientPtr<Return>> + 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::<Interface>( 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::<dyn Foo>() - /// .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<Return, FactoryFunc>( + pub fn to_async_dynamic_value<Return, Func>( 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::<Interface>( 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<dyn subjects_async::IUserManager> = 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<dyn subjects_async::IUserManager> = 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<Interface> = CastableFunction<dyn Fn() -> TransientPtr<Interface>, DIContainer>; - let default_factory = func_bound + let dynamic_val_func = func_bound .as_any() .downcast_ref::<Func<Interface>>() .ok_or_else(|| DIContainerError::CastFailed { interface: type_name::<Interface>(), - 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::<dyn IBuffer>().to_default_factory(&|_| { + /// di_container.bind::<dyn IBuffer>().to_dynamic_value(&|_| { /// Box::new(|| { /// let buffer = TransientPtr::new(Buffer::<BUFFER_SIZE>::new()); /// @@ -260,13 +260,13 @@ where /// ``` #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - pub fn to_default_factory<Return, FactoryFunc>( + pub fn to_dynamic_value<Return, Func>( self, - factory_func: &'static FactoryFunc, + func: &'static Func, ) -> Result<BindingWhenConfigurator<'di_container, Interface>, BindingBuilderError> where Return: 'static + ?Sized, - FactoryFunc: Fn( + Func: Fn( &DIContainer, ) -> crate::ptr::TransientPtr< dyn Fn<(), Output = crate::ptr::TransientPtr<Return>>, @@ -286,12 +286,12 @@ where >())); } - let factory_impl = CastableFunction::new(factory_func); + let castable_func = CastableFunction::new(func); self.di_container.set_binding::<Interface>( 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<dyn subjects::IUserManager> = TransientPtr::new(subjects::UserManager::new()); |