1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use proc_macro::TokenStream;
use syn::{parse_macro_input, ItemFn};
use quote::{quote, ToTokens};

/// Automatically start async runtime or call `loom::model` if required:
///
/// ```no_run
/// #[concurrency_toolkit::test]
/// fn test() {
///     // ...
/// }
/// ```
///
/// However, unlike `maybe_async::maybe_async`, this proc macro requires the function
/// to not be declared as `async` due to implementation detail
/// (`syn` doesn't provides an easy way to parse `async function), but it still can
/// remove `async`-related keywords just like `maybe_async::maybe_async`.
#[proc_macro_attribute]
pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {
    // Construct a representation of Rust code as a syntax tree
    // that we can manipulate
    let input = parse_macro_input!(item as ItemFn);

    let vis = input.vis;
    let sig = input.sig;
    let mut block = input.block.to_token_stream();

    #[cfg(feature = "permutation_testing")]
    {
        block = quote! {
            {
                concurrency_toolkit::loom::model(
                    || # block
                );
            }
        };
    }

    let expanded = quote! {
        #[concurrency_toolkit::maybe_async::test(
            feature = "is_sync",
            async(feature = "async_tokio", concurrency_toolkit::tokio::test),
        )]
        # vis async # sig # block
    };

    // Hand the output tokens back to the compiler
    TokenStream::from(expanded)
}