proc-macro-workshop | write Rust procedural macros [ Rust Latam conference | Wrapper library
kandi X-RAY | proc-macro-workshop Summary
kandi X-RAY | proc-macro-workshop Summary
Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of proc-macro-workshop
proc-macro-workshop Key Features
proc-macro-workshop Examples and Code Snippets
Community Discussions
Trending Discussions on proc-macro-workshop
QUESTION
I am trying to write procedural macros that will accept a Rust enum like
...ANSWER
Answered 2021-Feb-21 at 13:39Here is a Playground repro: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=6fb228ef5b3f3a100ba410d7c3e9714c
So, indeed what you mention is true: the expanded code could be copy-pasted and it would work. When this happens (having behavior from macro expansion and "manual copy-pasted expansion" differ), there are two possibilities:
macro_rules!
metavariablesWhen emitting code using
macro_rules!
special captures, some of these captures are wrapped with special invisible parenthesis that already tell the parser how the thing inside should be parsed, which make it illegal to use in other places (for instance, one may capture a$Trait:ty
, and then doingimpl $Trait for ...
will fail (it will parse$Trait
as a type, thus leading to it being interpreted as a trait object (old syntax)); see also https://github.com/danielhenrymantilla/rust-defile for other examples.This is not your case, but it's good to keep in mind (e.g. my initial hunch was that when doing
$tt::$rhs
if$tt
was a:path
-like capture, then that could fail).macro hygiene/transparency and
Span
sConsider, for instance:
QUESTION
I'm trying to implement the builder pattern from the proc macro workshop I'm creating a proc macro which parses a struct, extracts its name
, field_names
and field_types
. It should reproduce the struct itself and also create a builder struct with the same field_names
but with optional types.
My problem is that field_name
and field_type
are iterators that I would have to use twice in order to create two structs out of one.
This is my source tree
...ANSWER
Answered 2021-Jan-06 at 21:28Iterators can only be used zero or one time, not multiple; that's standard Rust and doesn't involve the quote!
macro:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install proc-macro-workshop
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page