macro_rules! macrox {
($(
$(#[$attr:meta])*
macro $macro_name:ident
$(($($arg: ident: $ty: ty), *)
$body: block);
+)
*) => { ... };
($(
$(#[$attr:meta])*
macro $macro_name:ident
$(($(@$identifier: ident $arg: ident: $ty: ty), *)
$body: block);
+)
*) => { ... };
}Expand description
§Macrox
The main crate’s macro, it takes a custom-syntax macro declaration. (macro name(arg1: type1, arg2: type2 /* ... */) { /* Body */})
§Example
use typed_macros::macrox;
macrox! {
#[macro_export]
macro macro_name(arg: String) {
// Do something with arg.
}
}
fn main() {
// You can use the macro wherever you want.
macro_name!(String::from("Hi"));
}You can declare various macros inside macrox!, and they can have
attributes.
§Single-branched and Multi-branched macros
With this macro you can write both single-branched and multi-branched macros, and both are really easy!
§Single-branched macros
These are macros like the one in the example, with just one possible branch.
§Multi-branched macros
These are a little bit more complicated, they use identifiers both distinguishing what branch you’re trying to use.
An identifier is anything that starts with ‘@’, e. g. @a
Also, between the two branches there must be a ‘;’
§Example
use typed_macros::macrox;
macrox! {
#[macro_export]
macro my_macro(@a x: String) {
// Do something with x being a String
};
(@b x: u32) {
// Do something with x being a u32
}
}
fn main() {
my_macro!(@a String::from("hi!"));
my_macro!(@b 5_u32);
}