Skip to content

Wesl macro #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ members = [
"crates/wesl-test",
"crates/wesl-web",
"crates/wesl-cli",
"crates/tokrepr",
"crates/tokrepr-derive",
"examples/random_wgsl",
"examples/wesl-consumer",
]
Expand All @@ -24,6 +26,8 @@ rust-version = "1.86.0"
wesl = { path = "crates/wesl", version = "0.1.2" }
wesl-macros = { path = "crates/wesl-macros", version = "0.1.2" }
wgsl-parse = { path = "crates/wgsl-parse", version = "0.1.2" }
tokrepr = { path = "crates/tokrepr", version = "0.0.1" }
tokrepr-derive = { path = "crates/tokrepr-derive", version = "0.0.1" }

[workspace.lints.rust]

Expand Down
21 changes: 21 additions & 0 deletions crates/tokrepr-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "tokrepr-derive"
description = "Derive macro for tokrepr"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version = "0.0.1"
rust-version.workspace = true

[dependencies]
itertools = "0.14.0"
proc-macro2 = "1.0.86"
quote = "1.0.37"
syn = "2.0.77"

[lib]
proc-macro = true

[lints]
workspace = true
103 changes: 103 additions & 0 deletions crates/tokrepr-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//! Derive macro for tokrepr.
//!
//!

use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Data, DeriveInput, Fields, parse_macro_input};

#[proc_macro_derive(TokRepr)]
pub fn derive_tokrepr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
tokrepr_impl(input).into()
}

pub(crate) fn tokrepr_impl(input: DeriveInput) -> TokenStream {
let name = &input.ident;
let tokrepr_path = quote! { tokrepr };
let self_path = quote! {};

let body = match &input.data {
Data::Struct(data) => {
let fields = match &data.fields {
Fields::Named(fields) => {
let fields = fields.named.iter().map(|f| &f.ident).collect_vec();
quote! {
#(let #fields = #tokrepr_path::TokRepr::tok_repr(&self.#fields);)*

#tokrepr_path::quote::quote! {
#self_path #name {
#(#fields: # #fields,)*
}
}
}
}
Fields::Unnamed(f) => {
let fields = (0..f.unnamed.len())
.map(|n| format_ident!("f{n}"))
.collect_vec();

quote! {
#(let #fields = #tokrepr_path::TokRepr::tok_repr(#fields);)*

#tokrepr_path::quote::quote!{
#self_path #name(#(# #fields,)*)
}
}
}
Fields::Unit => {
quote! {
#tokrepr_path::quote::quote! {
#self_path #name
}
}
}
};
fields
}
Data::Enum(data) => {
let fields = data.variants.iter().map(|v| {
let variant = &v.ident;
match &v.fields {
Fields::Named(_) => unimplemented!(),
Fields::Unnamed(f) => {
let fields = (0..f.unnamed.len())
.map(|n| format_ident!("f{n}"))
.collect_vec();

quote! {
Self::#variant(#(#fields),*) => {
#(let #fields = #tokrepr_path::TokRepr::tok_repr(#fields);)*

#tokrepr_path::quote::quote!{
#self_path #name::#variant(#(# #fields,)*)
}
}
}
},
Fields::Unit => {
quote! {
Self::#variant => #tokrepr_path::quote::quote! { #self_path #name::#variant }
}
},
}
});

quote! {
match self {
#(#fields,)*
}
}
}
Data::Union(_) => unimplemented!("tokrepr derive is not implemented for unions"),
};

quote! {
impl #tokrepr_path::TokRepr for #name {
fn tok_repr(&self) -> #tokrepr_path::proc_macro2::TokenStream {
#body
}
}
}
}
22 changes: 22 additions & 0 deletions crates/tokrepr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "tokrepr"
description = "Turn Rust instances into token representations"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version = "0.0.1"
rust-version.workspace = true

[dependencies]
proc-macro2 = "1.0.86"
quote = "1.0.37"
tokrepr-derive = { workspace = true, optional = true }

[features]
default = ["derive"]
derive = ["dep:tokrepr-derive"]
rc = []

[lints]
workspace = true
Loading
Loading