|
| 1 | +- Feature Name: Allow `lifetime` specifiers to be passed to macros |
| 2 | +- Start Date: 2016-04-22 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: (leave this empty) |
| 5 | + |
| 6 | +# Summary |
| 7 | +[summary]: #summary |
| 8 | + |
| 9 | +Add a `lifetime` specifier for `macro_rules!` patterns, that matches any valid |
| 10 | +lifetime. |
| 11 | + |
| 12 | +# Motivation |
| 13 | +[motivation]: #motivation |
| 14 | + |
| 15 | +Certain classes of macros are completely impossible without the ability to pass |
| 16 | +lifetimes. Specifically, anything that wants to implement a trait from inside of |
| 17 | +a macro is going to need to deal with lifetimes eventually. They're also |
| 18 | +commonly needed for any macros that need to deal with types in a more granular |
| 19 | +way than just `ty`. |
| 20 | + |
| 21 | +Since a lifetime is a single token, the only way to match against a lifetime is |
| 22 | +by capturing it as `tt`. Something like `'$lifetime:ident` would fail to |
| 23 | +compile. This is extremely limiting, as it becomes difficult to sanitize input, |
| 24 | +and `tt` is extremely difficult to use in a sequence without using awkward |
| 25 | +separators. |
| 26 | + |
| 27 | +# Detailed design |
| 28 | +[design]: #detailed-design |
| 29 | + |
| 30 | +This RFC proposes adding `lifetime` as an additional specifier to |
| 31 | +`macro_rules!` (alternatively: `life` or `lt`). As it is a single token, it is |
| 32 | +able to be followed by any other specifier. Since a lifetime acts very much |
| 33 | +like an identifier, and can appear in almost as many places, it can be handled |
| 34 | +almost identically. |
| 35 | + |
| 36 | +A preliminary implementation can be found at |
| 37 | +https://github.com/rust-lang/rust/pull/33135 |
| 38 | + |
| 39 | +# Drawbacks |
| 40 | +[drawbacks]: #drawbacks |
| 41 | + |
| 42 | +None |
| 43 | + |
| 44 | +# Alternatives |
| 45 | +[alternatives]: #alternatives |
| 46 | + |
| 47 | +A more general specifier, such as a "type parameter list", which would roughly |
| 48 | +map to `ast::Generics` would cover most of the cases that matching lifetimes |
| 49 | +individually would cover. |
| 50 | + |
| 51 | +# Unresolved questions |
| 52 | +[unresolved]: #unresolved-questions |
| 53 | + |
| 54 | +None |
0 commit comments