Anyway macro_rules to check if tt is sync or async?

Anyway macro_rules to check if tt is sync or async?
Why not 2 rules? Because the macro can take n number of async & sync functions as parameter.


macro_rules! gen {
    ($($func:tt),*) => {
        if $func is async { ... expand to async function $func ... }
        else { ... expand to sync function $func ... }

async fn async_fn () {...}
fn sync_fn () {...}

gen!(async_fn, sync_fn, ...);

It's not possible. The sync/async distinction is type-level information. Macros however are a purely syntactic construct. They don't (and can't) have access to type information.

Trying to generate sync/async code seems shady, while we are at it. Are you sure you aren't looking for generics instead of a macro? At the type level, an async function is just a function that returns impl Future. If you want to generate a function which has the same return type as another function, you'll need generics.

1 Like

I think better go with all async.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.