Mykhailo Maidan

Crafting Custom Frameworks with Rust: A Deep Dive into Procedural Macros

What if your Rust macros could share context across your entire crate? Learn how a pre-compilation metadata pass unlocks powerful, boilerplate-free APIs.

Crafting Custom Frameworks with Rust: A Deep Dive into Procedural Macros
#1about 7 minutes

Exploring the power and pitfalls of Rust macros

Rust's metaprogramming capabilities allow for reducing boilerplate code, but macros can be complex to write, debug, and test.

#2about 6 minutes

Simplifying event handlers with custom attribute macros

An event-driven framework's API can be significantly simplified by using attribute macros to define event handlers and their corresponding events.

#3about 7 minutes

Overcoming unordered macro expansion with build-time metadata

To solve the problem of unordered macro expansion, a build script (`build.rs`) can analyze the source code and generate a metadata file before compilation.

#4about 8 minutes

Parsing crate source code with the `syn` crate

The `syn` and `syn-inline-mod` crates enable parsing an entire Rust crate into a single Abstract Syntax Tree (AST), which can then be analyzed using the visitor pattern.

#5about 5 minutes

Loading metadata and modifying the AST in macros

Procedural macros can load the pre-generated metadata from disk and use the `VisitMut` trait from `syn` to modify the AST, such as renaming functions to simulate overloading.

#6about 8 minutes

Evaluating the trade-offs of a metadata-driven macro framework

While this metadata-driven approach offers powerful compile-time checks and extensibility for tooling, it increases build times and implementation complexity.

#7about 12 minutes

Q&A on testing, nightly Rust, and framework design

The discussion covers practical considerations like the necessity of nightly Rust for certain features, strategies for testing complex macros, and potential runtime optimizations.

Related jobs
Jobs that call for the skills explored in this talk.

test

Milly
Vienna, Austria

Intermediate

test

Milly
Vienna, Austria

Intermediate

Featured Partners

Related Articles

View all articles
DC
Daniel Cranney
Building AI Solutions with Rust and Docker
In recent years, artificial intelligence has surged in popularity in the world of development. While Python remains a popular choice in the realm of AI, Rust - often known as Rust Lang - is quickly emerging as a formidable alternative.Rust programmin...
Building AI Solutions with Rust and Docker
CH
Chris Heilmann
WeAreDevelopers LIVE days are changing - get ready to take part
Starting with this week's Web Dev Day edition of WeAreDevelopers LIVE Days, we changed the the way we run these online conferences. The main differences are:Shorter talks (half an hour tops)More interaction in Q&AA tips and tricks "Did you know" sect...
WeAreDevelopers LIVE days are changing - get ready to take part

From learning to earning

Jobs that call for the skills explored in this talk.