dtolnay / paste

Macros for all your token pasting needs
Apache License 2.0
983 stars 53 forks source link

Add $var:unquote modifier #88

Closed nikis05 closed 1 year ago

nikis05 commented 1 year ago

I'd like to propose a new modifier for variable conversion named unquote. When applied to a string literal variable, it should remove the quotes, producing tokens.

Example:

fn foo() {}

macro_rules! bar {
    ($literal:literal) => {
        paste!([<$literal:unquote>]());
    };
}

bar!("foo"); // - expands to foo().

Use case:

I have a macro_rules derive macro, implemented using https://crates.io/crates/macro_rules_attribute. User input is expected to contain an attribute, #[foo(some_attr = "path::to::func")]. Since it is impossible to specify paths in Rust meta, a common pattern is to ask users to provide a string literal instead, the proc macro then parses it as expected token type. One example of a crate that uses this pattern is serde: https://serde.rs/container-attrs.html#default--path.

Since the purpose of paste is to enable macro_rules to do what normally only proc macros can do, I think this would be a nice addition.

dtolnay commented 1 year ago

I think [\<$literal>] already works for this.