From 8d15cf4888be1261a0e6ec39088dc8104d6f0197 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 18 Sep 2022 14:42:23 +0200 Subject: refactor: add put factory return types in TransientPtr automatically --- examples/async/interfaces/food.rs | 3 +-- examples/factory/interfaces/user.rs | 4 +--- macros/src/lib.rs | 25 ++++++++++++++++++++----- src/async_di_container.rs | 4 ++-- src/di_container.rs | 4 ++-- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/examples/async/interfaces/food.rs b/examples/async/interfaces/food.rs index b310a3d..ae5eaef 100644 --- a/examples/async/interfaces/food.rs +++ b/examples/async/interfaces/food.rs @@ -1,5 +1,4 @@ use syrette::factory; -use syrette::ptr::TransientPtr; pub trait IFood { @@ -7,4 +6,4 @@ pub trait IFood } #[factory(threadsafe = true)] -pub type IFoodFactory = dyn Fn() -> TransientPtr; +pub type IFoodFactory = dyn Fn() -> dyn IFood; diff --git a/examples/factory/interfaces/user.rs b/examples/factory/interfaces/user.rs index aafd0cb..75fbc87 100644 --- a/examples/factory/interfaces/user.rs +++ b/examples/factory/interfaces/user.rs @@ -1,5 +1,4 @@ use syrette::factory; -use syrette::ptr::TransientPtr; pub trait IUser { @@ -9,5 +8,4 @@ pub trait IUser } #[factory] -pub type IUserFactory = - dyn Fn(&'static str, &'static str, &'static str) -> TransientPtr; +pub type IUserFactory = dyn Fn(&'static str, &'static str, &'static str) -> dyn IUser; diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 3f75340..27577c7 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -152,6 +152,8 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt /// Makes a type alias usable as a factory interface. /// +/// The return type is automatically put inside of a [`TransientPtr`]. +/// /// *This macro is only available if Syrette is built with the "factory" feature.* /// /// # Arguments @@ -166,8 +168,6 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt /// # Examples /// ``` /// # use syrette::factory; -/// # use syrette::interfaces::factory::IFactory; -/// # use syrette::ptr::TransientPtr; /// # /// # trait IConfigurator {} /// # @@ -184,12 +184,15 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt /// # impl IConfigurator for Configurator {} /// # /// #[factory] -/// type IConfiguratorFactory = dyn Fn(Vec) -> TransientPtr; +/// type IConfiguratorFactory = dyn Fn(Vec) -> dyn IConfigurator; /// ``` #[proc_macro_attribute] #[cfg(feature = "factory")] pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> TokenStream { + use quote::ToTokens; + use syn::Type; + use crate::factory_macro_args::FactoryMacroArgs; let FactoryMacroArgs { flags } = parse(args_stream).unwrap(); @@ -200,12 +203,24 @@ pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> Toke .map_or(false, |flag| flag.is_on.value); let factory_type_alias::FactoryTypeAlias { - type_alias, - factory_interface, + mut type_alias, + mut factory_interface, arg_types: _, return_type: _, } = parse(type_alias_stream).unwrap(); + let output = factory_interface.output.clone(); + + factory_interface.output = parse( + quote! { + syrette::ptr::TransientPtr<#output> + } + .into(), + ) + .unwrap(); + + type_alias.ty = Box::new(Type::Verbatim(factory_interface.to_token_stream())); + let decl_interfaces = if is_threadsafe { quote! { syrette::declare_interface!( diff --git a/src/async_di_container.rs b/src/async_di_container.rs index 6f87f1b..7dcca57 100644 --- a/src/async_di_container.rs +++ b/src/async_di_container.rs @@ -1134,7 +1134,7 @@ mod tests use crate as syrette; #[crate::factory(threadsafe = true)] - type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr; + type IUserManagerFactory = dyn Fn(Vec) -> dyn IUserManager; mock! { Provider {} @@ -1244,7 +1244,7 @@ mod tests use crate as syrette; #[crate::factory(threadsafe = true)] - type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr; + type IUserManagerFactory = dyn Fn(Vec) -> dyn IUserManager; mock! { Provider {} diff --git a/src/di_container.rs b/src/di_container.rs index ac75f81..3adee09 100644 --- a/src/di_container.rs +++ b/src/di_container.rs @@ -982,7 +982,7 @@ mod tests use crate as syrette; #[crate::factory] - type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr; + type IUserManagerFactory = dyn Fn(Vec) -> dyn IUserManager; mock! { Provider {} @@ -1078,7 +1078,7 @@ mod tests use crate as syrette; #[crate::factory] - type IUserManagerFactory = dyn Fn(Vec) -> TransientPtr; + type IUserManagerFactory = dyn Fn(Vec) -> dyn IUserManager; mock! { Provider {} -- cgit v1.2.3-18-g5258