This is another one Rust quasi-quoting library like
quote that gives you mquote!
macro providing
several features aimed on better readability and usability.
Motivation
The only interpolations supported by quote!
macros are regular insertion #a
(and
you are not able to put an expression like my_struct.field
here) and repeating
insertion #(...)*
.
For me that's not enough. If you wanna conditionally put a piece of tokens, you
have to associate them with some variable and then interpolate it into quote!
expression:
let conditional_piece = if having_fun else ;
quote!
Don't you find it could be confusing? Especially if there're a lot of such things.
Even putting simple expression like my_struct.field
must be handled in this way.
Introduce templating mquote!
It supports:
- expression insertion
- if/else condition
- for iteration
- matching
- extending
So you're able to rewrite above code:
mquote!
This crate is not about syntax sugar only! In fact using mquote!
in complicated
cases gives a bit of performance increasing since it does not create a several
TokenStream
s and join them together, it handles everything within single
TokenStream
.
Crate contains mquote!
and mquote_spanned!
. Usage examples of the first one
are in following section. The second one allow you to set
span of producing tokens stream by this syntax: mquote_spanned!(span => ...)
.
More examples
Expression insertion
Turns given expression into tokens by using
ToTokens
.
If / elif / else
For
}
Matching
Extending
Sometimes you want mquote!
to consume an iterator of TokenTree
s
without cloning. It's possible with special syntax ^{iterable}
that accepts
any IntoIterator<Item=TokenTree>
.
Escaping #{}
or ^{}
If you want to put either #{abc}
or ^{abc}
as is, you should double braces: