tlborm | The Little Book of Rust Macros | Learning library
kandi X-RAY | tlborm Summary
kandi X-RAY | tlborm Summary
The Little Book of Rust Macros(Updated)
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 tlborm
tlborm Key Features
tlborm Examples and Code Snippets
Community Discussions
Trending Discussions on tlborm
QUESTION
I'm trying to write a Rust macro that allows me to make use of the field names and types of a struct declaration, but I still need to emit the struct.
I've got it working with optional attributes, visibility of the struct (thanks to The Little Book of Rust Macros), but can't figure out how to deal with the optional presence of pub
in the individual fields.
So far I've got:
...ANSWER
Answered 2017-Feb-02 at 14:02You can't. At least, not with a single, non-recursive rule. This is because Rust doesn't have a macro matcher for visibility.
The parse-macros
crate contains a parse_struct!
macro that shows the work necessary to completely parse a struct
definition. Short version: you need to parse each field individually, with one rule for each of "has pub
" and "doesn't have pub
".
I'd also just note that there's another case your macro doesn't yet account for: attributes on the fields, which is needed for doc-comments on them to work.
Quite soon, macros 1.1 should be stabilised, which might provide an easier approach (assuming you can express your macro as a derivation, and don't care about older versions of Rust).
QUESTION
I'm trying to understand how Rust macro captures work and am looking at the nom parser library.
Location nom/src/bytes.rs declares macro tag!
which captures with ($i:expr, $tag: expr)
.
However throughout the places where tag!
is used it is used with only one argument, i.e. tag!("+")
.
Example: nom-lua52/op.rs. I've read tutorials about macros however this construct seems to be special.
It seems like the input $i
is somehow implicit?
ANSWER
Answered 2018-Jul-16 at 18:24The trick is that the tag!()
macro call is inside the value!
macro, which will match against tag!
and pass it an additional parameter. This is described in the file implementing tag!
.
QUESTION
I cannot understand where lazy-static's @TAIL
and @MAKE
have been defined and their particular use cases.
If I've understood internal rules correctly, the primary usage of @as_expr
in the example is to hide as_expr!
(or in general, previously defined macros) from being exported i.e. it's a way of altering the global macro namespace. Following that, then @TAIL
or @MAKE
should already be a macro while I cannot find them in the lazy_static source.
ANSWER
Answered 2018-Oct-15 at 02:29You linked to the definitions. @TAIL
is right there three lines down on 137, @MAKE
is on 162.
@name
is not special in any way whatsoever. There is absolutely no special behaviour. It's just a sequence of tokens that cannot show up in "normal" code, and is thus unlikely to be accidentally matched to other rules. @as_expr
does not hide an as_expr!
macro, it's used instead of defining a publicly visible as_expr!
macro.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install tlborm
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