diff options
author | HampusM <hampus@hampusmat.com> | 2022-09-18 16:39:27 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-09-18 16:39:27 +0200 |
commit | 0b914f415cb04c45d8655cae3828af264887d203 (patch) | |
tree | b7b0cac02d7ea1dcc2fcb02ba98ab642d0f6e82f /macros | |
parent | 8d15cf4888be1261a0e6ec39088dc8104d6f0197 (diff) |
feat: add factory macro async flag
Diffstat (limited to 'macros')
-rw-r--r-- | macros/src/factory_macro_args.rs | 2 | ||||
-rw-r--r-- | macros/src/lib.rs | 25 |
2 files changed, 23 insertions, 4 deletions
diff --git a/macros/src/factory_macro_args.rs b/macros/src/factory_macro_args.rs index 0cf1d66..dd80c1c 100644 --- a/macros/src/factory_macro_args.rs +++ b/macros/src/factory_macro_args.rs @@ -5,7 +5,7 @@ use syn::Token; 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 FactoryMacroArgs { diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 27577c7..2715f3d 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -161,6 +161,8 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt /// /// # Flags /// - `threadsafe` - Mark as threadsafe. +/// - `async` - Mark as async. Infers the `threadsafe` flag. The return type is +/// automatically put inside of a pinned boxed future. /// /// # Panics /// If the attributed item is not a type alias. @@ -197,11 +199,20 @@ pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> Toke let FactoryMacroArgs { 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 factory_type_alias::FactoryTypeAlias { mut type_alias, mut factory_interface, @@ -212,8 +223,16 @@ pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> Toke let output = factory_interface.output.clone(); factory_interface.output = parse( - quote! { - syrette::ptr::TransientPtr<#output> + if is_async { + quote! { + std::pin::Pin<Box< + dyn std::future::Future<Output = syrette::ptr::TransientPtr<#output>> + >> + } + } else { + quote! { + syrette::ptr::TransientPtr<#output> + } } .into(), ) |