diff options
| author | HampusM <hampus@hampusmat.com> | 2023-08-04 22:03:30 +0200 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2023-08-04 22:03:30 +0200 | 
| commit | b4718494e3a1759286caca1dd34c01db6c2f1214 (patch) | |
| tree | 736bde3a1647d7c26c7cc27ce3caf4de2fa665d8 | |
| parent | b4ddc1e626fbd11d784b442d246ddc5f54c35b51 (diff) | |
refactor!: remove SomeThreadsafePtr & move variants to SomePtr
BREAKING CHANGE: SomeThreadsafePtr has been removed and it's variants have been moved to SomePtr
| -rw-r--r-- | macros/src/injectable/implementation.rs | 12 | ||||
| -rw-r--r-- | src/di_container/asynchronous/mod.rs | 44 | ||||
| -rw-r--r-- | src/errors/ptr.rs | 3 | ||||
| -rw-r--r-- | src/ptr.rs | 37 | ||||
| -rw-r--r-- | src/test_utils.rs | 8 | 
5 files changed, 46 insertions, 58 deletions
diff --git a/macros/src/injectable/implementation.rs b/macros/src/injectable/implementation.rs index 0ea623c..575b5c4 100644 --- a/macros/src/injectable/implementation.rs +++ b/macros/src/injectable/implementation.rs @@ -223,6 +223,10 @@ impl<Dep: IDependency> InjectableImpl<Dep>          let self_type = &self.self_type;          let constructor = &self.constructor_method.sig.ident; +        let dependency_idents = (0..get_dep_method_calls.len()) +            .map(|index| format_ident!("dependency_{index}")) +            .collect::<Vec<_>>(); +          quote! {              #maybe_doc_hidden              impl #generics syrette::interfaces::async_injectable::AsyncInjectable< @@ -256,8 +260,14 @@ impl<Dep: IDependency> InjectableImpl<Dep>                          #maybe_prevent_circular_deps +                        // Dependencies can't be passed directly to the constructor +                        // because the Rust compiler becomes sad about SomePtr having +                        // a variant with a Rc inside of it and .await being called even +                        // when the Rc variant isn't even being created +                        #(let #dependency_idents = #get_dep_method_calls;)* +                          Ok(syrette::ptr::TransientPtr::new(Self::#constructor( -                            #(#get_dep_method_calls),* +                            #(#dependency_idents),*                          )))                      })                  } 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<Self>, -    ) -> BoxFuture<'b, Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>> +    ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>>      where          Interface: 'static + 'b + ?Sized + Send + Sync,          'a: 'b, @@ -112,7 +112,7 @@ where      fn get_named<'a, 'b, Interface>(          self: &'a Arc<Self>,          name: &'static str, -    ) -> BoxFuture<'b, Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>> +    ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>>      where          Interface: 'static + 'b + ?Sized + Send + Sync,          'a: 'b, @@ -123,7 +123,7 @@ where          self: &Arc<Self>,          dependency_history: DependencyHistoryType,          name: Option<&'static str>, -    ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError> +    ) -> Result<SomePtr<Interface>, AsyncDIContainerError>      where          Interface: 'static + ?Sized + Send + Sync;  } @@ -161,7 +161,7 @@ impl IAsyncDIContainer<DependencyHistory> for AsyncDIContainer      fn get<'a, 'b, Interface>(          self: &'a Arc<Self>, -    ) -> BoxFuture<'b, Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>> +    ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>>      where          Interface: 'static + 'b + ?Sized + Send + Sync,          'a: 'b, @@ -176,7 +176,7 @@ impl IAsyncDIContainer<DependencyHistory> for AsyncDIContainer      fn get_named<'a, 'b, Interface>(          self: &'a Arc<Self>,          name: &'static str, -    ) -> BoxFuture<'b, Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>> +    ) -> BoxFuture<'b, Result<SomePtr<Interface>, AsyncDIContainerError>>      where          Interface: 'static + 'b + ?Sized + Send + Sync,          'a: 'b, @@ -192,7 +192,7 @@ impl IAsyncDIContainer<DependencyHistory> for AsyncDIContainer          self: &Arc<Self>,          dependency_history: DependencyHistory,          name: Option<&'static str>, -    ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError> +    ) -> Result<SomePtr<Interface>, AsyncDIContainerError>      where          Interface: 'static + ?Sized + Send + Sync,      { @@ -243,23 +243,21 @@ impl AsyncDIContainer      async fn handle_binding_providable<Interface>(          self: &Arc<Self>,          binding_providable: AsyncProvidable<Self, DependencyHistory>, -    ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError> +    ) -> Result<SomePtr<Interface>, AsyncDIContainerError>      where          Interface: 'static + ?Sized + Send + Sync,      {          match binding_providable { -            AsyncProvidable::Transient(transient_binding) => { -                Ok(SomeThreadsafePtr::Transient( -                    transient_binding.cast::<Interface>().map_err(|_| { -                        AsyncDIContainerError::CastFailed { -                            interface: type_name::<Interface>(), -                            binding_kind: "transient", -                        } -                    })?, -                )) -            } +            AsyncProvidable::Transient(transient_binding) => Ok(SomePtr::Transient( +                transient_binding.cast::<Interface>().map_err(|_| { +                    AsyncDIContainerError::CastFailed { +                        interface: type_name::<Interface>(), +                        binding_kind: "transient", +                    } +                })?, +            )),              AsyncProvidable::Singleton(singleton_binding) => { -                Ok(SomeThreadsafePtr::ThreadsafeSingleton( +                Ok(SomePtr::ThreadsafeSingleton(                      singleton_binding                          .cast::<Interface>()                          .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  { @@ -70,6 +70,14 @@ where      #[cfg(feature = "factory")]      #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]      Factory(FactoryPtr<Interface>), + +    /// A smart pointer to a interface in the singleton scope. +    ThreadsafeSingleton(ThreadsafeSingletonPtr<Interface>), + +    /// A smart pointer to a factory. +    #[cfg(feature = "factory")] +    #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] +    ThreadsafeFactory(ThreadsafeFactoryPtr<Interface>),  }  impl<Interface> SomePtr<Interface> @@ -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<Interface> -where -    Interface: 'static + ?Sized, -{ -    /// A smart pointer to a interface in the transient scope. -    Transient(TransientPtr<Interface>), -    /// A smart pointer to a interface in the singleton scope. -    ThreadsafeSingleton(ThreadsafeSingletonPtr<Interface>), - -    /// A smart pointer to a factory. -    #[cfg(feature = "factory")] -    #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))] -    ThreadsafeFactory(ThreadsafeFactoryPtr<Interface>), -} - -impl<Interface> SomeThreadsafePtr<Interface> -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<DependencyHistoryType> @@ -397,14 +397,14 @@ pub mod mocks                  async fn get<Interface>(                      self: &Arc<Self>, -                ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError> +                ) -> Result<SomePtr<Interface>, AsyncDIContainerError>                  where                      Interface: 'static + ?Sized + Send + Sync;                  async fn get_named<Interface>(                      self: &Arc<Self>,                      name: &'static str, -                ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError> +                ) -> Result<SomePtr<Interface>, AsyncDIContainerError>                  where                      Interface: 'static + ?Sized + Send + Sync; @@ -413,7 +413,7 @@ pub mod mocks                      self: &Arc<Self>,                      dependency_history: DependencyHistoryType,                      name: Option<&'static str>, -                ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError> +                ) -> Result<SomePtr<Interface>, AsyncDIContainerError>                  where                      Interface: 'static + ?Sized + Send + Sync;              }  | 
