mockalloc_macros/
lib.rs

1#![deny(missing_docs)]
2//! mockalloc-macros
3//!
4//! Provides the `#[mockalloc::test]` procedural macro for the `mockalloc`
5//! crate.
6
7use proc_macro::TokenStream;
8use quote::quote;
9
10/// Replacement for the `#[test]` macro to automatically detect allocation
11/// bugs from within the test.
12///
13/// Equivalent to wrapping the test body in
14/// `mockalloc::assert_allocs(|| { ... });`.
15#[proc_macro_attribute]
16pub fn test(_args: TokenStream, item: TokenStream) -> TokenStream {
17    let input = syn::parse_macro_input!(item as syn::ItemFn);
18    let sig = &input.sig;
19    let body = &input.block;
20    let attrs = &input.attrs;
21    let vis = input.vis;
22
23    let result = quote! {
24        #[::core::prelude::v1::test]
25        #(#attrs)*
26        #vis #sig {
27            ::mockalloc::assert_allocs(move || {
28                #body
29            });
30        }
31    };
32
33    result.into()
34}