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
| Entering compute | |
| Return value from compute: 20 | |
| Exiting compute (elapsed: 369µs) |
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
| use my_proc_macros_lib::log_entry_and_exit; | |
| #[log_entry_and_exit(time, ret)] | |
| fn compute(x: i32) -> i32 { | |
| x * 2 | |
| } | |
| pub fn test_attribute() { | |
| let val = compute(10); | |
| } |
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
| // Parse the annotated function | |
| let input_fn: ItemFn = parse_macro_input!(item as ItemFn); | |
| let vis = &input_fn.vis; | |
| let sig = &input_fn.sig; | |
| let body = &input_fn.block; | |
| let ident = &input_fn.sig.ident; | |
| let print_ret = if log_ret { | |
| quote! { |
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
| let mut log_time = false; | |
| let mut log_ret = false; | |
| // Create parser | |
| let arg_parser = syn::meta::parser(|meta| { | |
| if meta.path.is_ident("time") { | |
| log_time = true; | |
| Ok(()) | |
| } else if meta.path.is_ident("ret") { | |
| log_ret = true; |
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 log_entry_and_exit(attr: TokenStream, item: TokenStream) -> TokenStream { | |
| // Attribute flags | |
| let mut log_time = false; | |
| let mut log_ret = false; | |
| // Create parser | |
| let arg_parser = syn::meta::parser(|meta| { | |
| if meta.path.is_ident("time") { | |
| log_time = true; |
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
| Called hello_macro from CustomDerive macro. | |
| Debug: Person { name: "", age: 0 } |
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
| use my_proc_macros_lib::CustomDerive; | |
| #[derive(CustomDerive)] | |
| struct Person { | |
| name: String, | |
| age: u32, | |
| } | |
| pub fn test_custom_derive() { | |
| let _p = Person::default(); |
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
| impl ::core::default::Default for #name { | |
| fn default() -> Self { | |
| Self { | |
| #(#default_fields),* | |
| } | |
| } | |
| } |
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
| let default_fields = fields.iter().map(|f| { | |
| let fname = &f.ident; | |
| quote! { #fname: Default::default() } | |
| }); |
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
| Data::Struct(data_struct) => match &data_struct.fields { | |
| Fields::Named(fields_named) => &fields_named.named, | |
| _ => panic!("CustomDerive only supports named struct fields."), | |
| }, |