#identifier #macro #invocation #ident #concatenate #mashup

deprecated macro mashup-impl

Concatenate identifiers in a macro invocation

15 releases

Uses old Rust 2015

0.1.14+deprecated Dec 19, 2022
0.1.13+deprecated May 23, 2021
0.1.12 Jul 26, 2020
0.1.9 Oct 5, 2018
0.1.5 Jul 15, 2018

#9 in #invocation

Download history 3369/week @ 2024-07-22 3943/week @ 2024-07-29 3009/week @ 2024-08-05 3159/week @ 2024-08-12 2886/week @ 2024-08-19 2583/week @ 2024-08-26 1321/week @ 2024-09-02 700/week @ 2024-09-09 431/week @ 2024-09-16 534/week @ 2024-09-23 372/week @ 2024-09-30 203/week @ 2024-10-07 464/week @ 2024-10-14 370/week @ 2024-10-21 289/week @ 2024-10-28 297/week @ 2024-11-04

1,434 downloads per month
Used in 30 crates (via mashup)

MIT/Apache

17KB
238 lines

Mashup: a working stable concat_idents

github crates.io docs.rs build status


Note: If you are targeting Rust 1.31+, please use the paste crate instead which has an easier interface. Only consider using this one if you care about supporting compilers between 1.15 and 1.31.

The nightly-only concat_idents! macro in the Rust standard library is notoriously underpowered in that its concatenated identifiers can only refer to existing items, they can never be used to define something new.

This crate provides a more flexible spin on concatenating idents.

[dependencies]
mashup = "0.1"

Mashup works with any Rust compiler version 1.15+.

So tell me about concatenating idents

This crate provides a mashup! macro-generating macro. You provide mashup a mapping of arbitrary key tokens to idents that you want concatenated together, and mashup defines for you a substitution macro that substitutes your key tokens with the single concatenated ident corresponding to each one.

#[macro_use]
extern crate mashup;

// Use mashup to generate a substitution macro called m. The substitution macro
// will replace all occurrences of the key token "method" in its input with the
// single concatenated identifier abc.
mashup! {
    m["method"] = a b c;
}

struct Struct;

m! {
    impl Struct {
        fn "method"() {}
    }
}

fn main() {
    // Our struct now has an abc method.
    Struct::abc();
}

Glossary

  • Substitution macro: A macro produced by the mashup! macro. The input to mashup! provides a name for one or more substitution macros to be defined. The substitution macro in the short example above is called m!.

  • Key tokens: Arbitrary tokens that are to be replaced by a single concatenated ident anywhere in the input of a substitution macro. The token "method" is used as a key token above.

  • Ident pieces: Idents that are concatenated together to form a single concatenated ident in the final macro-expanded code. The a b c are ident pieces that come together to form the abc method name.

More elaborate example

This example demonstrates some trickier uses of mashup.

  • You may need to bundle a mashup! invocation inside of a more convenient user-facing macro of your own.

  • The mashup! invocation may define multiple substitutions, including via the $(...)* repetition available in macro_rules.

  • Key tokens may consist of more than one token.

  • Substitution macros work equally well in item position and expression position.

#[macro_use]
extern crate mashup;

const ROCKET_A: &str = "/a";
const ROCKET_B: &str = "/b";

macro_rules! routes {
    ($($route:ident),*) => {{
        mashup! {
            $(
                m["rocket-codegen-route" $route] = ROCKET_ $route;
            )*
        }

        m! {
            vec![$("rocket-codegen-route" $route),*]
        }
    }}
}

fn main() {
    let routes = routes!(A, B);
    assert_eq!(routes, vec!["/a", "/b"]);
}

Attributes

Attributes for the substitution macro, including doc comments, may be provided inside of the mashup invocation.

mashup! {
    /// Needs better documentation.
    #[macro_export]
    m1["w"] = W w;
    m1["x"] = X x;

    #[macro_export]
    #[doc(hidden)]
    m2["y"] = Y y;
    m2["z"] = Z z;
}

Limitations

  • The mashup! macro may be invoked at most once within a lexical scope. To provide a way around this, you may use a single mashup invocation to define more than one substitution macro by using as many different substitution macro names within one invocation as you want (#5).

  • As a consequence of hygiene, a concatenated identifier may not be used to refer to a captured local variable (#6).


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~230KB