cgp_async_macro/lib.rs
1#![no_std]
2
3/*!
4 This library provides helper macros for using async functions in traits.
5*/
6
7extern crate alloc;
8extern crate proc_macro;
9
10use proc_macro::TokenStream;
11
12mod impl_async;
13
14/**
15 The `#[async_trait]` macro is used to desugar async functions in traits
16 to return `impl Future`.
17
18 This macro is required mainly to get around the current limitation of
19 [async functions in traits](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits/),
20 which would produce a lint warning for `async_fn_in_trait` if bare async
21 functions are defined in a trait.
22
23 ## Example
24
25 Given the following trait definition:
26
27 ```rust,ignore
28 #[async_trait]
29 pub trait CanRun {
30 async fn run(&self);
31 }
32 ```
33
34 The macro would desugar it to the following:
35
36 ```rust,ignore
37 pub trait CanRun {
38 fn run(&self) -> impl Future<Output = ()>;
39 }
40 ```
41*/
42#[proc_macro_attribute]
43pub fn async_trait(_attr: TokenStream, stream: TokenStream) -> TokenStream {
44 impl_async::impl_async(stream.into()).into()
45}