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.txt
Created June 28, 2025 21:17
Example of creating a Gist using Python
Entering compute
Return value from compute: 20
Exiting compute (elapsed: 369µs)
@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::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);
}
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
// 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! {
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
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;
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
#[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;
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.txt
Created June 28, 2025 21:17
Example of creating a Gist using Python
Called hello_macro from CustomDerive macro.
Debug: Person { name: "", age: 0 }
@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::CustomDerive;
#[derive(CustomDerive)]
struct Person {
    name: String,
    age: u32,
}
pub fn test_custom_derive() {
    let _p = Person::default();
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
impl ::core::default::Default for #name {
fn default() -> Self {
Self {
#(#default_fields),*
}
}
}
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
let default_fields = fields.iter().map(|f| {
let fname = &f.ident;
quote! { #fname: Default::default() }
});
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
Data::Struct(data_struct) => match &data_struct.fields {
Fields::Named(fields_named) => &fields_named.named,
_ => panic!("CustomDerive only supports named struct fields."),
},