Skip to content

Commit

Permalink
simple derive with async
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandros committed Oct 22, 2024
1 parent d11f883 commit aeeec86
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
30 changes: 24 additions & 6 deletions ferrunix-macros/src/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn ::std::future::Future<Output = ()> + Send>,
> }
quote! {
pub(crate) fn register<'reg>(
registry: &'reg ::ferrunix::Registry,
) -> ::std::pin::Pin<
::std::boxed::Box<dyn ::std::future::Future<Output = ()> + 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")]
Expand All @@ -60,6 +68,14 @@ fn box_if_required(tokens: &proc_macro2::TokenStream) -> proc_macro2::TokenStrea
}
}

fn await_if_needed() -> Option<proc_macro2::TokenStream> {
(cfg!(feature = "tokio")).then(|| {
quote! {
.await
}
})
}

fn registration(
input: &DeriveInput,
attrs: &DeriveAttrInput,
Expand Down Expand Up @@ -103,11 +119,13 @@ fn registration_empty(
dependency_type: &syn::Ident,
) -> syn::Result<proc_macro2::TokenStream> {
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)
Expand Down
40 changes: 20 additions & 20 deletions ferrunix/tests/it/derive_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::future::Future<Output = ()> + 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<dyn std::future::Future<Output = ()> + Send + 'reg>,
// >
// where
// Self: Sync + 'static,
// {
// Box::pin(async move {
// registry
// .transient(|| Box::pin(async move { Self {} }))
// .await;
// })
// }
// }

ferrunix::autoregister!(RegistrationFunc::new(Empty::register));

Expand Down

0 comments on commit aeeec86

Please sign in to comment.