Skip to content

Instantly share code, notes, and snippets.

View freedomtowin's full-sized avatar

Rohan Kotwani freedomtowin

View GitHub Profile
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
pub struct DeriveInput {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub ident: Ident,
pub generics: Generics,
pub data: Data,
}
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
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;
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.toml
Created June 28, 2025 21:17
Example of creating a Gist using Python
[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"]}
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
use my_proc_macros_lib::*;
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.toml
Created June 28, 2025 21:17
Example of creating a Gist using Python
[dependencies]
my_proc_macros_lib = { path = "my_proc_macros_lib" }
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.toml
Created June 28, 2025 21:17
Example of creating a Gist using Python
[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
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.sh
Created June 28, 2025 21:17
Example of creating a Gist using Python
cargo new --lib my_proc_macros_lib
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
#[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);
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.txt
Created June 28, 2025 21:17
Example of creating a Gist using Python
ItemFn {
attrs: [],
vis: Visibility::Public(
Pub,
),
sig: Signature {
constness: None,
asyncness: None,
unsafety: None,
abi: None,
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.txt
Created June 28, 2025 21:17
Example of creating a Gist using Python
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) }