From b4718494e3a1759286caca1dd34c01db6c2f1214 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 4 Aug 2023 22:03:30 +0200 Subject: refactor!: remove SomeThreadsafePtr & move variants to SomePtr BREAKING CHANGE: SomeThreadsafePtr has been removed and it's variants have been moved to SomePtr --- src/di_container/asynchronous/mod.rs | 44 ++++++++++++++++-------------------- src/errors/ptr.rs | 3 +-- src/ptr.rs | 37 ++++++++---------------------- src/test_utils.rs | 8 +++---- 4 files changed, 35 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 19e28a8..b603e88 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -66,7 +66,7 @@ use crate::private::cast::arc::CastArc; use crate::private::cast::boxed::CastBox; use crate::private::cast::error::CastError; use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; -use crate::ptr::{SomeThreadsafePtr, TransientPtr}; +use crate::ptr::{SomePtr, TransientPtr}; pub mod binding; pub mod prelude; @@ -96,7 +96,7 @@ where /// - Casting the binding for `Interface` fails fn get<'a, 'b, Interface>( self: &'a Arc, - ) -> BoxFuture<'b, Result, AsyncDIContainerError>> + ) -> BoxFuture<'b, Result, AsyncDIContainerError>> where Interface: 'static + 'b + ?Sized + Send + Sync, 'a: 'b, @@ -112,7 +112,7 @@ where fn get_named<'a, 'b, Interface>( self: &'a Arc, name: &'static str, - ) -> BoxFuture<'b, Result, AsyncDIContainerError>> + ) -> BoxFuture<'b, Result, AsyncDIContainerError>> where Interface: 'static + 'b + ?Sized + Send + Sync, 'a: 'b, @@ -123,7 +123,7 @@ where self: &Arc, dependency_history: DependencyHistoryType, name: Option<&'static str>, - ) -> Result, AsyncDIContainerError> + ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync; } @@ -161,7 +161,7 @@ impl IAsyncDIContainer for AsyncDIContainer fn get<'a, 'b, Interface>( self: &'a Arc, - ) -> BoxFuture<'b, Result, AsyncDIContainerError>> + ) -> BoxFuture<'b, Result, AsyncDIContainerError>> where Interface: 'static + 'b + ?Sized + Send + Sync, 'a: 'b, @@ -176,7 +176,7 @@ impl IAsyncDIContainer for AsyncDIContainer fn get_named<'a, 'b, Interface>( self: &'a Arc, name: &'static str, - ) -> BoxFuture<'b, Result, AsyncDIContainerError>> + ) -> BoxFuture<'b, Result, AsyncDIContainerError>> where Interface: 'static + 'b + ?Sized + Send + Sync, 'a: 'b, @@ -192,7 +192,7 @@ impl IAsyncDIContainer for AsyncDIContainer self: &Arc, dependency_history: DependencyHistory, name: Option<&'static str>, - ) -> Result, AsyncDIContainerError> + ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync, { @@ -243,23 +243,21 @@ impl AsyncDIContainer async fn handle_binding_providable( self: &Arc, binding_providable: AsyncProvidable, - ) -> Result, AsyncDIContainerError> + ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync, { match binding_providable { - AsyncProvidable::Transient(transient_binding) => { - Ok(SomeThreadsafePtr::Transient( - transient_binding.cast::().map_err(|_| { - AsyncDIContainerError::CastFailed { - interface: type_name::(), - binding_kind: "transient", - } - })?, - )) - } + AsyncProvidable::Transient(transient_binding) => Ok(SomePtr::Transient( + transient_binding.cast::().map_err(|_| { + AsyncDIContainerError::CastFailed { + interface: type_name::(), + binding_kind: "transient", + } + })?, + )), AsyncProvidable::Singleton(singleton_binding) => { - Ok(SomeThreadsafePtr::ThreadsafeSingleton( + Ok(SomePtr::ThreadsafeSingleton( singleton_binding .cast::() .map_err(|err| match err { @@ -308,9 +306,7 @@ impl AsyncDIContainer } })?; - Ok(SomeThreadsafePtr::ThreadsafeFactory( - factory(self.clone()).into(), - )) + Ok(SomePtr::ThreadsafeFactory(factory(self.clone()).into())) } #[cfg(feature = "factory")] AsyncProvidable::DefaultFactory(binding) => { @@ -323,7 +319,7 @@ impl AsyncDIContainer >, >(binding, "default factory")?; - Ok(SomeThreadsafePtr::Transient(default_factory(self.clone())())) + Ok(SomePtr::Transient(default_factory(self.clone())())) } #[cfg(feature = "factory")] AsyncProvidable::AsyncDefaultFactory(binding) => { @@ -340,7 +336,7 @@ impl AsyncDIContainer binding, "async default factory" )?; - Ok(SomeThreadsafePtr::Transient( + Ok(SomePtr::Transient( async_default_factory(self.clone())().await, )) } diff --git a/src/errors/ptr.rs b/src/errors/ptr.rs index 1db10c7..e0c3d05 100644 --- a/src/errors/ptr.rs +++ b/src/errors/ptr.rs @@ -1,9 +1,8 @@ //! Smart pointer alias errors. -/// Error type for [`SomePtr`] and [`SomeThreadsafePtr`]. +/// Error type for [`SomePtr`]. /// /// [`SomePtr`]: crate::ptr::SomePtr -/// [`SomeThreadsafePtr`]: crate::ptr::SomeThreadsafePtr #[derive(thiserror::Error, Debug)] pub enum SomePtrError { diff --git a/src/ptr.rs b/src/ptr.rs index bcaa566..1facc52 100644 --- a/src/ptr.rs +++ b/src/ptr.rs @@ -70,6 +70,14 @@ where #[cfg(feature = "factory")] #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] Factory(FactoryPtr), + + /// A smart pointer to a interface in the singleton scope. + ThreadsafeSingleton(ThreadsafeSingletonPtr), + + /// A smart pointer to a factory. + #[cfg(feature = "factory")] + #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] + ThreadsafeFactory(ThreadsafeFactoryPtr), } impl SomePtr @@ -87,36 +95,11 @@ where cfg(feature = "factory"), cfg_attr(doc_cfg, doc(cfg(feature = "factory"))) ); -} - -/// Some threadsafe smart pointer. -#[derive(strum_macros::IntoStaticStr)] -pub enum SomeThreadsafePtr -where - Interface: 'static + ?Sized, -{ - /// A smart pointer to a interface in the transient scope. - Transient(TransientPtr), - /// A smart pointer to a interface in the singleton scope. - ThreadsafeSingleton(ThreadsafeSingletonPtr), - - /// A smart pointer to a factory. - #[cfg(feature = "factory")] - #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] - ThreadsafeFactory(ThreadsafeFactoryPtr), -} - -impl SomeThreadsafePtr -where - Interface: 'static + ?Sized, -{ - create_as_variant_fn!(SomeThreadsafePtr, Transient, SomePtrError); - - create_as_variant_fn!(SomeThreadsafePtr, ThreadsafeSingleton, SomePtrError); + create_as_variant_fn!(SomePtr, ThreadsafeSingleton, SomePtrError); create_as_variant_fn!( - SomeThreadsafePtr, + SomePtr, ThreadsafeFactory, SomePtrError, cfg(feature = "factory"), diff --git a/src/test_utils.rs b/src/test_utils.rs index 6b351cc..78ad63b 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -375,7 +375,7 @@ pub mod mocks use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncDIContainerError; use crate::provider::r#async::IAsyncProvider; - use crate::ptr::SomeThreadsafePtr; + use crate::ptr::SomePtr; mock! { pub AsyncDIContainer @@ -397,14 +397,14 @@ pub mod mocks async fn get( self: &Arc, - ) -> Result, AsyncDIContainerError> + ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync; async fn get_named( self: &Arc, name: &'static str, - ) -> Result, AsyncDIContainerError> + ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync; @@ -413,7 +413,7 @@ pub mod mocks self: &Arc, dependency_history: DependencyHistoryType, name: Option<&'static str>, - ) -> Result, AsyncDIContainerError> + ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync; } -- cgit v1.2.3-18-g5258