Place-macro
Macros you wish you had while you were writing your non-proc macro.
This library privides some macros that make writing regural non-proc macros much simpler, readable and with less dirty tricks.
The main macro of this library is place. It is able to expand the macros in
this library in reverse expansion order.
Macros
place: expands the following macros in reverse order, see below
ignore: expands to nothingidentity: expands to what is given, it bypasses the reverse order in theplacemacrodollar: expands to dollar sign$string: concats the contents into single string, see the docidentifier: concats the contents into sintle identifier in the same way as stringhead: expands to the first tokentail: expands to all but the first tokenstart: expands to all but the last tokenlast: expands to the last tokenreverse: expands to the tokens in reverse orderstringify: expands to string of the inputreplace_newline: replaces all newlines and folowing whitespace in literal with the given literalstr_replace: replace in string literal
The macro place
Expands the other macros inside in reverse order. The macros inside that will
be expanded are used with a different sintax: instead of calling a macro as
string!("hello" "there") you call it as __string__("hello" "there"). One
exception is the macro dollar that is called without the parenthesis:
__dollar__ instead of dollar!().
For some of the macros there are also shorter names:
__identity__-__id____string__-__str____dollar__-__s____identifier__-__ident____stringify__-__strfy____replace_newline__-__repnl____str_replace__-__repstr__
Example
The following passes:
use place;
let res = place!;
assert_eq!;
Why is this useful?
- You can generate identifiers in your macros:
use place;
my_cool_macro!
/// Expands to:
- You can generate strings as macro parameters in your macros:
use place;
my_cool_macro!
/// Expands to:
- Or you can even generate macros in your macros
use place;
my_cooler_macro! ;
my_cool_macro!
/// now you have the same function as in the previous example
The last example was a little less readable, but you can see that you can do a lot with this macro.
Links
- Author: BonnyAD9
- GitHub repository: BonnyAD/raplay
- Package: crates.io
- Documentation: docs.rs
- My Website: bonnyad9.github.io