dtolnay / quote

Rust quasi-quoting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

async doesn't seem to work?

kurtbuilds opened this issue · comments

This snippit:

fn codegen() -> TokenStream {
    quote! {
        use tokio;

        pub async fn main() {
            println!("Hello, world!");
        }
    }
}

fails with

error: can't qualify macro invocation with `pub`
 --> stdin:1:13
  |
1 | use tokio ; pub async fn main () { println ! ("Hello, world!") ; }
  |             ^^^
  |
  = help: try adjusting the macro to put `pub` inside the invocation

error: expected one of `!` or `::`, found `fn`
 --> stdin:1:23
  |
1 | use tokio ; pub async fn main () { println ! ("Hello, world!") ; }
  |                      -^^ unexpected token
  |                      |
  |                      expected one of `!` or `::` here

Are async functions not supported? Are there recommended workarounds?

This code

fn codegen() -> TokenStream {
    quote! {
        use tokio;

        pub fn main() -> impl Future<Output = Result<(), anyhow::Error>> {
            async {
                println!("Hello, world!");
                Ok(())
            }
        }
    }
}

also fails, with a different error:

error: expected one of `,` or `}`, found `!`
 --> stdin:1:105
  |
1 | use tokio ; pub fn main () -> impl Future < Output = Result < () , anyhow :: Error >> { async { println ! ("Hello, world!") ; Ok (()) } }
  |                                                                                                        -^ unexpected token
  |                                                                                                        |
  |                                                                                                        expected one of `,` or `}` here