Compile-Time Reflection in Rust. A New Way of Making Derive Macros

Macros are a powerful tool for generating code that would be difficult or tedious to write by hand. Rust is a programming language that supports  a macro system called procedural macros. The programmer can define a procedural macro by using standard Rust code on a stream of tokens and thus does not have to rely on a domain-specific language for writing macros.

Usually, when someone wants to write procedural macros, they would use the libraries syn and quote. syn is a library for parsing token streams to data structures, and quote reads data structures and produce token streams. Both libraries are useful because of the flexibility they offer. However, they don’t solve some of the hard problems with macros in Rust that arise from dealing with lifetimes and generics.  

This thesis introduces a new library called reflect that is designed to deal with some of the hard problems with procedural macros.




Tags: macros, Rust, derive macros, reflection, compile-time reflection, compiler, macro programming, programming languages By Asbjørn Gaarde
Published July 27, 2020 9:53 AM - Last modified July 27, 2020 2:07 PM