6 releases

0.2.1 Jul 19, 2023
0.2.0 Jul 19, 2023
0.1.2 Jul 4, 2020
0.1.1 Jul 4, 2020
0.1.0 Jul 4, 2020

#1104 in Rust patterns

Download history 210/week @ 2024-07-20 273/week @ 2024-07-27 339/week @ 2024-08-03 138/week @ 2024-08-10 1084/week @ 2024-08-17 1748/week @ 2024-08-24 2559/week @ 2024-08-31 1714/week @ 2024-09-07 2054/week @ 2024-09-14 1659/week @ 2024-09-21 1089/week @ 2024-09-28 1158/week @ 2024-10-05 1009/week @ 2024-10-12 1203/week @ 2024-10-19 1258/week @ 2024-10-26 1806/week @ 2024-11-02

5,452 downloads per month
Used in 7 crates (2 directly)

Zlib OR MIT OR Apache-2.0

11KB

::defile

Helper proc-macro to "ungroup" a captured metavariable (thus potentially breaking their hygiene, hence the name).

Repository Latest version Documentation MSRV unsafe forbidden License CI no_std

This is useful when using helper macro_rules macros, that need to parse using some special rule (e.g. :expr, :path, :pat), but that later want to further inspect the captured variable.

This is not something a macro_rules! can do on its own, since such so-called metavariables are seen as an opaque single token (:tt) (the sequence of tokens captured in the metavariable have been grouped (≈ parenthesized) but using invisible parenthesis.

Example

macro_rules! check_expr {
    (
        42
    ) => ({
        println!("Got `42`!");
    });

    (
        $($tt:tt)*
    ) => ({
        println!("Did not get `42`. Instead, got the following tokens:\n[");
        $(
            println!("    `{}`,", stringify!($tt));
        )*
        println!("]");
    });
}

macro_rules! check_all_exprs {(
    $(
        $expr:expr // use :expr to be able to use `,` as a delimiter
    ),* $(,)?
) => (
    fn main () {
        $(
            println!("vvvvvvvvvvvvv");
            check_expr!($expr);
            println!("^^^^^^^^^^^^^\n");
        )*
    }
)}

check_all_exprs!(42, 1 + 1);

outputs:

vvvvvvvvvvvvv
Did not get `42`. Instead, got the following tokens:
[
    `42`,
]
^^^^^^^^^^^^^

vvvvvvvvvvvvv
Did not get `42`. Instead, got the following tokens:
[
    `1 + 1`,
]
^^^^^^^^^^^^^
  • That is:

    • The token 42 does not match 42!

    • That being said, the expression 1 + 1 is viewed as a single indivisible token too.

      Indeed, that's kind of the point of this behavior: if we do 2 * $expr where $expr captures 1 + 1 we expect the result to be 2 * (1 + 1) instead of 2 * 1 + 1!

But by doing:

macro_rules! check_all_exprs {(
    $(
        $expr:expr // use :expr to be able to use `,` as a delimiter
    ),* $(,)?
) => (::defile::defile! { // 👈
    fn main () {
        $(
            println!("vvvvvvvvvvvvv");
            check_expr!(@$expr);
//                      👆
            println!("^^^^^^^^^^^^^\n");
        )*
    }
})}

we do get:

vvvvvvvvvvvvv
Got `42`!
^^^^^^^^^^^^^

vvvvvvvvvvvvv
Did not get `42`. Instead, got the following tokens:
[
    `1`,
    `+`,
    `1`,
]
^^^^^^^^^^^^^
  • 42 has matched the literal 42, but be aware that this has also resulted in 1 + 1 getting split. So, if you were to defile expressions such as 2 * @$expr, you may not obtain the expected result! Use with caution.

Caveats

Currently (1.45.0), there are several bugs regarding the interaction between macro_rules! macros and procedural macros, that may lead to defile! and any other helper procedural macro to split groups that are not @-prefixed.

Hopefully those bugs are solved, making the actual implementation of defile! meaningful.

Dependencies