Crates.io | tokrepr |
lib.rs | tokrepr |
version | 0.1.1 |
created_at | 2025-07-27 19:03:40.894657+00 |
updated_at | 2025-08-31 15:28:46.983899+00 |
description | Turn Rust instances into token representations |
homepage | |
repository | https://github.com/wgsl-tooling-wg/wesl-rs |
max_upload_size | |
id | 1770321 |
size | 18,230 |
Turn an instance into a Rust [TokenStream
] that represents the instance.
This crate provides the [TokRepr
] trait which turns an instance into Rust code that
when called, produce an expression that evaluates to the same instance. This can be
useful for code generation in procedural macros.
This trait is primarily meant to assist with the creation of procedural macros. It can
be useful if the macro needs to output an instance of a complex data structure.
Without if, writing the token stream manually, or with the quote!
trait can be a
burden.
The trait is implemented for Rust primitives and most data types from the standard
library. It uses proc_macro2
and quote
.
For user-defined types, the trait can be implemented manually or automatically with the derive macro. See the macro documentation for configuration options.
This trait generates code. Like all procedural macros, the generated code is
unhygienic. Implementers of TokRepr
need to pay attention to the scope of
identifiers. The derive macro assumes that the struct or enum is in scope at the macro
call site.
The uneval
crate provides a similar feature
but leverages serde
for serialization, which has the advantage of being widely
implemented for data structure types. It does not provide a way to customize the
codegen of a type.