Last active
August 19, 2024 15:13
-
-
Save aalhitennf/4f601c27d94e9355dba8cf999ecc952b to your computer and use it in GitHub Desktop.
proc_macro main wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[proc_macro_attribute] | |
pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream { | |
let input = parse_macro_input!(item as ItemFn); | |
let fn_name = &input.sig.ident; // Function name | |
if fn_name != "main" { | |
panic!("[macro name] can be derived only on main function!"); | |
} | |
let fn_output = &input.sig.output; // Function output | |
let scope = input.block; | |
quote! { | |
fn #fn_name() #fn_output { | |
#scope | |
} | |
} | |
.into() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment