diff options
Diffstat (limited to 'src/di_container')
7 files changed, 50 insertions, 39 deletions
diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index 8ba5be3..b2d2b55 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -21,6 +21,7 @@ pub type BoxFn<Args, Return> = Box<(dyn Fn<Args, Output = Return> + Send + Sync)  /// Binding builder for type `Interface` inside a [`IAsyncDIContainer`].  ///  /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer +#[must_use = "No binding will be created if you don't use the binding builder"]  pub struct AsyncBindingBuilder<Interface, DIContainerType, DependencyHistoryType>  where      Interface: 'static + ?Sized + Send + Sync, @@ -96,7 +97,7 @@ where      ///      /// [`IAsyncDIContainer`]: crate::di_container::asynchronous::IAsyncDIContainer      pub async fn to<Implementation>( -        &self, +        self,      ) -> Result<          AsyncBindingScopeConfigurator<              Interface, @@ -121,7 +122,7 @@ where              self.dependency_history_factory,          ); -        binding_scope_configurator.in_transient_scope().await; +        binding_scope_configurator.set_in_transient_scope().await;          Ok(binding_scope_configurator)      } @@ -178,7 +179,7 @@ where      #[cfg(feature = "factory")]      #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]      pub async fn to_factory<Args, Return, FactoryFunc>( -        &self, +        self,          factory_func: &'static FactoryFunc,      ) -> Result<          AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, @@ -271,7 +272,7 @@ where      #[cfg(feature = "factory")]      #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]      pub async fn to_async_factory<Args, Return, FactoryFunc>( -        &self, +        self,          factory_func: &'static FactoryFunc,      ) -> Result<          AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, @@ -364,7 +365,7 @@ where      #[cfg(feature = "factory")]      #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]      pub async fn to_default_factory<Return, FactoryFunc>( -        &self, +        self,          factory_func: &'static FactoryFunc,      ) -> Result<          AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, @@ -458,7 +459,7 @@ where      #[cfg(feature = "factory")]      #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]      pub async fn to_async_default_factory<Return, FactoryFunc>( -        &self, +        self,          factory_func: &'static FactoryFunc,      ) -> Result<          AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs index b5923ec..2271a16 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -63,19 +63,10 @@ where      ///      /// This is the default.      pub async fn in_transient_scope( -        &self, +        self,      ) -> AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>      { -        self.di_container -            .set_binding::<Interface>( -                None, -                Box::new(AsyncTransientTypeProvider::< -                    Implementation, -                    DIContainerType, -                    DependencyHistoryType, -                >::new()), -            ) -            .await; +        self.set_in_transient_scope().await;          AsyncBindingWhenConfigurator::new(self.di_container.clone())      } @@ -85,7 +76,7 @@ where      /// # Errors      /// Will return Err if resolving the implementation fails.      pub async fn in_singleton_scope( -        &self, +        self,      ) -> Result<          AsyncBindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,          AsyncBindingScopeConfiguratorError, @@ -110,6 +101,20 @@ where          Ok(AsyncBindingWhenConfigurator::new(self.di_container.clone()))      } + +    pub(crate) async fn set_in_transient_scope(&self) +    { +        self.di_container +            .set_binding::<Interface>( +                None, +                Box::new(AsyncTransientTypeProvider::< +                    Implementation, +                    DIContainerType, +                    DependencyHistoryType, +                >::new()), +            ) +            .await; +    }  }  #[cfg(test)] diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs index 4d56347..fb14bd4 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -45,7 +45,7 @@ where      /// # Errors      /// Will return Err if no binding for the interface already exists.      pub async fn when_named( -        &self, +        self,          name: &'static str,      ) -> Result<(), AsyncBindingWhenConfiguratorError>      { diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 65fa40f..27151d7 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -16,6 +16,7 @@ use crate::interfaces::injectable::Injectable;  /// Binding builder for type `Interface` inside a [`IDIContainer`].  ///  /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer +#[must_use = "No binding will be created if you don't use the binding builder"]  pub struct BindingBuilder<Interface, DIContainerType, DependencyHistoryType>  where      Interface: 'static + ?Sized, @@ -90,7 +91,7 @@ where      ///      /// [`IDIContainer`]: crate::di_container::blocking::IDIContainer      pub fn to<Implementation>( -        &self, +        self,      ) -> Result<          BindingScopeConfigurator<              Interface, @@ -117,7 +118,7 @@ where              self.dependency_history_factory,          ); -        binding_scope_configurator.in_transient_scope(); +        binding_scope_configurator.set_in_transient_scope();          Ok(binding_scope_configurator)      } @@ -191,7 +192,7 @@ where      #[cfg(feature = "factory")]      #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]      pub fn to_factory<Args, Return, Func>( -        &self, +        self,          factory_func: &'static Func,      ) -> Result<          BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, @@ -221,7 +222,7 @@ where              )),          ); -        Ok(BindingWhenConfigurator::new(self.di_container.clone())) +        Ok(BindingWhenConfigurator::new(self.di_container))      }      /// Creates a binding of type `Interface` to a factory that takes no arguments @@ -280,7 +281,7 @@ where      #[cfg(feature = "factory")]      #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]      pub fn to_default_factory<Return, FactoryFunc>( -        &self, +        self,          factory_func: &'static FactoryFunc,      ) -> Result<          BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>, @@ -313,7 +314,7 @@ where              )),          ); -        Ok(BindingWhenConfigurator::new(self.di_container.clone())) +        Ok(BindingWhenConfigurator::new(self.di_container))      }  } diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index 6c6c32d..ede0f6b 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -64,19 +64,12 @@ where      /// This is the default.      #[allow(clippy::must_use_candidate)]      pub fn in_transient_scope( -        &self, +        self,      ) -> BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>      { -        self.di_container.set_binding::<Interface>( -            None, -            Box::new(TransientTypeProvider::< -                Implementation, -                DIContainerType, -                DependencyHistoryType, -            >::new()), -        ); +        self.set_in_transient_scope(); -        BindingWhenConfigurator::new(self.di_container.clone()) +        BindingWhenConfigurator::new(self.di_container)      }      /// Configures the binding to be in a singleton scope. @@ -84,7 +77,7 @@ where      /// # Errors      /// Will return Err if resolving the implementation fails.      pub fn in_singleton_scope( -        &self, +        self,      ) -> Result<          BindingWhenConfigurator<Interface, DIContainerType, DependencyHistoryType>,          BindingScopeConfiguratorError, @@ -101,7 +94,19 @@ where          self.di_container              .set_binding::<Interface>(None, Box::new(SingletonProvider::new(singleton))); -        Ok(BindingWhenConfigurator::new(self.di_container.clone())) +        Ok(BindingWhenConfigurator::new(self.di_container)) +    } + +    pub(crate) fn set_in_transient_scope(&self) +    { +        self.di_container.set_binding::<Interface>( +            None, +            Box::new(TransientTypeProvider::< +                Implementation, +                DIContainerType, +                DependencyHistoryType, +            >::new()), +        );      }  } diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs index f93806b..656d81d 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -45,7 +45,7 @@ where      /// # Errors      /// Will return Err if no binding for the interface already exists.      pub fn when_named( -        &self, +        self,          name: &'static str,      ) -> Result<(), BindingWhenConfiguratorError>      { diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index de7ef67..cd7065c 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -137,7 +137,6 @@ impl DIContainer  impl IDIContainer<DependencyHistory> for DIContainer  { -    #[must_use]      fn bind<Interface>(          self: &mut Rc<Self>,      ) -> BindingBuilder<Interface, Self, DependencyHistory>  | 
