Skip to content

Using #[async_recursion] inside a declarative macro #29

@HectorMRC

Description

@HectorMRC

I am trying to implement a declarative macro that, given a type, implements a recursive asynchronous function that takes that type as parameter. However, it seems unfeasible when the given type is a reference, since the type may not live as long as the 'async_recursion lifetime.

A minimal example of the declarative macros is something as following:

macro_rules! help_please {
    ($param:ty) => {
        #[async_recursion]
        async fn recursive_fn<F>(param: $param, f: &F)
        where
            F: Fn($param) + Sync + Send,
        {
            f(param);
        }
    };
}

And then, if I use it like in the example down below, it does not compile and tells me to add explicit lifetime 'async_recursion to the type of 'param': &'async_recursion usize. What cannot be done since the lifetime `async_recursion does not exist at that level.

macros_async::help_please!(&usize);

I am aware that my approach may be weird or absolutely wrong; however, I would like to know if there is any way to tell async_recursion which is the lifetime that should be taken as `async_recursion. Can this situation be resolved somehow? Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions