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
| pub struct DeriveInput { | |
| pub attrs: Vec<Attribute>, | |
| pub vis: Visibility, | |
| pub ident: Ident, | |
| pub generics: Generics, | |
| pub data: Data, | |
| } |
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 proc_macro::TokenStream; | |
| use quote::quote; | |
| use syn::{parse_macro_input, ItemFn, DeriveInput}; | |
| use syn::{Data, Fields, Type, Expr}; | |
| #[proc_macro_derive(CustomDerive)] | |
| pub fn add_derive_proc_macro(input: TokenStream) -> TokenStream { | |
| let input = parse_macro_input!(input as DeriveInput); | |
| let name = input.ident; |
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
| [package] | |
| name = "proc-macro-testing" | |
| version = "0.1.0" | |
| edition = "2024" | |
| [dependencies] | |
| my_proc_macros_lib = { path = "my_proc_macros_lib" } | |
| tokio = { version = "*", features = ["full"]} | |
| reqwest = {version = "0.12.12", features = ["json"]} | |
| serde = {version = "1.0.217", features = ["derive"]} |
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::*; |
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
| [dependencies] | |
| my_proc_macros_lib = { path = "my_proc_macros_lib" } |
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
| [package] | |
| name = "my_proc_macros_lib" | |
| version = "0.1.0" | |
| edition = "2021" | |
| [lib] | |
| name = "my_proc_macros_lib" | |
| path = "src/lib.rs" | |
| proc-macro = 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
| cargo new --lib my_proc_macros_lib |
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] | |
| pub fn my_fn_like_proc_macro(input: TokenStream) -> TokenStream { | |
| // Parse the function from the input | |
| let input_fn = parse_macro_input!(input as ItemFn); | |
| // Visualize or transform (here, just printing function name) | |
| // This can be seen with `cargo expand` | |
| let fn_name = &input_fn.sig.ident; | |
| println!("Function-like macro called on function: {}", fn_name); |
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
| ItemFn { | |
| attrs: [], | |
| vis: Visibility::Public( | |
| Pub, | |
| ), | |
| sig: Signature { | |
| constness: None, | |
| asyncness: None, | |
| unsafety: None, | |
| abi: None, |
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
| Ident { sym: pub, span: #0 bytes(117..120) } | |
| Ident { sym: fn, span: #0 bytes(121..123) } | |
| Ident { sym: foo, span: #0 bytes(124..127) } | |
| Group (delim = Parenthesis) | |
| Punct { char: '-', spacing: Joint, span: #0 bytes(130..131) } | |
| Punct { char: '>', spacing: Alone, span: #0 bytes(131..132) } | |
| Ident { sym: i32, span: #0 bytes(133..136) } | |
| Group (delim = Brace) | |
| Literal { kind: Integer, symbol: "100", suffix: None, span: #0 bytes(147..150) } | |
| Punct { char: '+', spacing: Alone, span: #0 bytes(151..152) } |