diff --git a/ferrunix-macros/src/inject.rs b/ferrunix-macros/src/inject.rs index ca4a529..679fbe3 100644 --- a/ferrunix-macros/src/inject.rs +++ b/ferrunix-macros/src/inject.rs @@ -41,15 +41,23 @@ fn register_func_sig() -> proc_macro2::TokenStream { quote! { pub(crate) fn register(registry: &::ferrunix::Registry) } #[cfg(feature = "tokio")] - quote! { pub(crate) fn register(registry: &::ferrunix::Registry) -> ::std::pin::Pin< - ::std::boxed::Box + Send>, - > } + quote! { + pub(crate) fn register<'reg>( + registry: &'reg ::ferrunix::Registry, + ) -> ::std::pin::Pin< + ::std::boxed::Box + Send + 'reg>, + > + where + Self: Sync + 'static, + } } -fn box_if_required(tokens: &proc_macro2::TokenStream) -> proc_macro2::TokenStream { +fn box_if_required( + tokens: &proc_macro2::TokenStream, +) -> proc_macro2::TokenStream { #[cfg(not(feature = "tokio"))] { - tokens.clone() + quote! { { #tokens } } } #[cfg(feature = "tokio")] @@ -60,6 +68,14 @@ fn box_if_required(tokens: &proc_macro2::TokenStream) -> proc_macro2::TokenStrea } } +fn await_if_needed() -> Option { + (cfg!(feature = "tokio")).then(|| { + quote! { + .await + } + }) +} + fn registration( input: &DeriveInput, attrs: &DeriveAttrInput, @@ -103,11 +119,13 @@ fn registration_empty( dependency_type: &syn::Ident, ) -> syn::Result { let ctor = get_ctor_for(registered_ty, quote!(Self {}))?; + let ctor = box_if_required(&ctor); + let ifawait = await_if_needed(); let tokens = quote! { registry.#dependency_type::<#registered_ty>(|| { #ctor - }); + })#ifawait; }; Ok(tokens) diff --git a/ferrunix/tests/it/derive_async.rs b/ferrunix/tests/it/derive_async.rs index 9ddde6b..ef40633 100644 --- a/ferrunix/tests/it/derive_async.rs +++ b/ferrunix/tests/it/derive_async.rs @@ -2,28 +2,28 @@ use ferrunix::{Inject, RegistrationFunc, Registry}; -// #[derive(Inject)] -// #[provides(transient)] +#[derive(Inject)] +#[provides(transient)] struct Empty {} -#[automatically_derived] -impl<'reg> Empty { - #[allow(clippy::use_self)] - pub(crate) fn register( - registry: &'reg ::ferrunix::Registry, - ) -> std::pin::Pin< - std::boxed::Box + Send + 'reg>, - > - where - Self: Sync + 'reg, - { - Box::pin(async move { - registry - .transient(|| Box::pin(async move { Self {} })) - .await; - }) - } -} +// #[automatically_derived] +// impl Empty { +// #[allow(clippy::use_self)] +// pub(crate) fn register<'reg>( +// registry: &'reg ::ferrunix::Registry, +// ) -> std::pin::Pin< +// std::boxed::Box + Send + 'reg>, +// > +// where +// Self: Sync + 'static, +// { +// Box::pin(async move { +// registry +// .transient(|| Box::pin(async move { Self {} })) +// .await; +// }) +// } +// } ferrunix::autoregister!(RegistrationFunc::new(Empty::register));