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
use my_proc_macros_lib::comp;
pub fn test_list_compreh() {
let vec_of_vecs = vec![vec![1, 2, 3], vec![4, 5, 6]];
let result = comp![x for vec in vec_of_vecs for x in vec if x > 1 && x < 6].collect::<Vec<_>>();
println!("{:?}", result);
assert_eq!(result, [2, 3, 4, 5]);
}
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
let vec_of_vecs = vec![vec![1, 2, 3], vec![4, 5, 6]];
let result = comp![x for vec in vec_of_vecs for x in vec if x > 1].collect::<Vec<_>>();
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
impl ToTokens for Comprehension {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let all_for_if_clauses =
std::iter::once(&self.for_if_clause).chain(&self.additional_for_if_clauses);
let mut innermost_to_outermost = all_for_if_clauses.rev();
let mut output = {
// innermost is a special case--here we do the mapping
let innermost = innermost_to_outermost
.next()
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
struct Condition(Expr);
impl Parse for Condition {
fn parse(input: ParseStream) -> syn::Result<Self> {
_ = input.parse::<Token![if]>()?;
input.parse().map(Self)
}
}
impl ToTokens for Condition {
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
struct ForIfClause {
pattern: Pat,
sequence: Expr,
conditions: Vec<Condition>,
}
impl Parse for ForIfClause {
fn parse(input: ParseStream) -> syn::Result<Self> {
_ = input.parse::<Token![for]>()?;
let pattern = Pat::parse_single(input)?;
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
pub struct Comprehension {
pub mapping: Mapping,
pub for_if_clause: ForIfClause,
pub additional_for_if_clauses: Vec<ForIfClause>,
}
impl Parse for Comprehension {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self {
mapping: input.parse()?,
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.rs
Created June 28, 2025 21:17
Example of creating a Gist using Python
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
parse_macro_input, Expr, Pat, Token,
};
pub struct Comprehension {
pub mapping: Mapping,
pub for_if_clause: ForIfClause,
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.py
Created June 28, 2025 21:17
Example of creating a Gist using Python
xs = [-1, 2, 2, 3, 4]
out = [x * 2 for x in xs if x > 0]
# out = [-1, 4, 4, 6, 8]
@freedomtowin
freedomtowin / init-deep-dive_rust-proc-macro.txt
Created June 28, 2025 21:17
Example of creating a Gist using Python
create_custom_function_and_print!(hello: "Hello, world!");
@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)