diff options
Diffstat (limited to 'macros/src')
-rw-r--r-- | macros/src/factory/build_declare_interfaces.rs | 2 | ||||
-rw-r--r-- | macros/src/factory/declare_default_args.rs | 2 | ||||
-rw-r--r-- | macros/src/lib.rs | 39 |
3 files changed, 34 insertions, 9 deletions
diff --git a/macros/src/factory/build_declare_interfaces.rs b/macros/src/factory/build_declare_interfaces.rs index ac4ddd6..61e162f 100644 --- a/macros/src/factory/build_declare_interfaces.rs +++ b/macros/src/factory/build_declare_interfaces.rs @@ -14,7 +14,7 @@ pub fn build_declare_factory_interfaces( syrette::castable_factory::threadsafe::ThreadsafeCastableFactory< (std::sync::Arc<syrette::async_di_container::AsyncDIContainer>,), #factory_interface - > -> syrette::interfaces::factory::IFactory< + > -> syrette::interfaces::factory::IThreadsafeFactory< (std::sync::Arc<syrette::async_di_container::AsyncDIContainer>,), #factory_interface >, diff --git a/macros/src/factory/declare_default_args.rs b/macros/src/factory/declare_default_args.rs index 6450583..d19eba8 100644 --- a/macros/src/factory/declare_default_args.rs +++ b/macros/src/factory/declare_default_args.rs @@ -5,7 +5,7 @@ use syn::{Token, Type}; use crate::macro_flag::MacroFlag; use crate::util::iterator_ext::IteratorExt; -pub const FACTORY_MACRO_FLAGS: &[&str] = &["threadsafe"]; +pub const FACTORY_MACRO_FLAGS: &[&str] = &["threadsafe", "async"]; pub struct DeclareDefaultFactoryMacroArgs { diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 07ee7a5..172a113 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -6,16 +6,20 @@ use proc_macro::TokenStream; use quote::quote; -use syn::{parse, parse_macro_input, parse_str}; +use syn::{parse, parse_macro_input}; mod declare_interface_args; -mod factory; -mod fn_trait; mod injectable; mod libs; mod macro_flag; mod util; +#[cfg(feature = "factory")] +mod factory; + +#[cfg(feature = "factory")] +mod fn_trait; + use crate::declare_interface_args::DeclareInterfaceArgs; use crate::injectable::implementation::InjectableImpl; use crate::injectable::macro_args::InjectableMacroArgs; @@ -188,7 +192,7 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> TokenStream { use quote::ToTokens; - use syn::Type; + use syn::{parse_str, Type}; use crate::factory::build_declare_interfaces::build_declare_factory_interfaces; use crate::factory::macro_args::FactoryMacroArgs; @@ -266,6 +270,7 @@ pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> Toke /// /// # Flags /// - `threadsafe` - Mark as threadsafe. +/// - `async` - Mark as async. Infers the `threadsafe` flag. /// /// # Panics /// If the provided arguments are invalid. @@ -285,20 +290,40 @@ pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> Toke #[cfg(feature = "factory")] pub fn declare_default_factory(args_stream: TokenStream) -> TokenStream { + use syn::parse_str; + use crate::factory::build_declare_interfaces::build_declare_factory_interfaces; use crate::factory::declare_default_args::DeclareDefaultFactoryMacroArgs; use crate::fn_trait::FnTrait; let DeclareDefaultFactoryMacroArgs { interface, flags } = parse(args_stream).unwrap(); - let is_threadsafe = flags + let mut is_threadsafe = flags .iter() .find(|flag| flag.flag.to_string().as_str() == "threadsafe") .map_or(false, |flag| flag.is_on.value); + let is_async = flags + .iter() + .find(|flag| flag.flag.to_string().as_str() == "async") + .map_or(false, |flag| flag.is_on.value); + + if is_async { + is_threadsafe = true; + } + let mut factory_interface: FnTrait = parse( - quote! { - dyn Fn() -> syrette::ptr::TransientPtr<#interface> + if is_async { + quote! { + dyn Fn() -> syrette::future::BoxFuture< + 'static, + syrette::ptr::TransientPtr<#interface> + > + } + } else { + quote! { + dyn Fn() -> syrette::ptr::TransientPtr<#interface> + } } .into(), ) |