Skip to main content

reth_concurrency_macros/
lib.rs

1//! Procedural macros for `reth-concurrency`.
2
3use proc_macro::TokenStream;
4use quote::quote;
5use syn::{parse_macro_input, spanned::Spanned, Error, ItemFn};
6
7/// Marks a test that runs inside `reth_concurrency::sync::Runtime::run`.
8///
9/// Expands to a `#[test]` whose body moves into a `Runtime::run` closure, so the test runs
10/// inline by default and under simulation with both the `fuzz` feature and `--cfg dst`. Other
11/// attributes, such as `#[should_panic]`, are kept. Inside `proptest!`, it replaces `#[test]` and
12/// runs every case, including input generation, in one runtime.
13///
14/// Async tests require the `async-test` feature. They run on current-thread Tokio natively.
15/// In simulation their future and blocking workers run under Shuttle. See
16/// `reth_concurrency::sync::Runtime::run_async` for supported wakeups.
17#[proc_macro_attribute]
18pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
19    if !args.is_empty() {
20        let args = proc_macro2::TokenStream::from(args);
21        return Error::new(args.span(), "`reth_concurrency::test` takes no arguments")
22            .to_compile_error()
23            .into();
24    }
25
26    let ItemFn { attrs, vis, mut sig, block } = parse_macro_input!(item as ItemFn);
27    if !sig.inputs.is_empty() {
28        return Error::new(
29            sig.inputs.span(),
30            "`reth_concurrency::test` functions take no arguments",
31        )
32        .to_compile_error()
33        .into();
34    }
35
36    let body = if sig.asyncness.take().is_some() {
37        quote!(::reth_concurrency::sync::Runtime::new().run_async(move || async move #block))
38    } else {
39        quote!(::reth_concurrency::sync::Runtime::new().run(move || #block))
40    };
41
42    quote! {
43        #[::core::prelude::v1::test]
44        #(#attrs)*
45        #vis #sig {
46            #body
47        }
48    }
49    .into()
50}