linux/rust
Gary Guo 823d4737d4 rust: macros: add paste! proc macro
This macro provides a flexible way to concatenated identifiers together
and it allows the resulting identifier to be used to declare new items,
which `concat_idents!` does not allow. It also allows identifiers to be
transformed before concatenated.

The `concat_idents!` example

    let x_1 = 42;
    let x_2 = concat_idents!(x, _1);
    assert!(x_1 == x_2);

can be written with `paste!` macro like this:

    let x_1 = 42;
    let x_2 = paste!([<x _1>]);
    assert!(x_1 == x_2);

However `paste!` macro is more flexible because it can be used to create
a new variable:

    let x_1 = 42;
    paste!(let [<x _2>] = [<x _1>];);
    assert!(x_1 == x_2);

While this is not possible with `concat_idents!`.

This macro is similar to the `paste!` crate [1], but this is a fresh
implementation to avoid vendoring large amount of code directly. Also, I
have augmented it to provide a way to specify span of the resulting
token, allowing precise control.

For example, this code is broken because the variable is declared inside
the macro, so Rust macro hygiene rules prevents access from the outside:

    macro_rules! m {
        ($id: ident) => {
            // The resulting token has hygiene of the macro.
            paste!(let [<$id>] = 1;)
        }
    }

    m!(a);
    let _ = a;

In this version of `paste!` macro I added a `span` modifier to allow
this:

    macro_rules! m {
        ($id: ident) => {
            // The resulting token has hygiene of `$id`.
            paste!(let [<$id:span>] = 1;)
        }
    }

    m!(a);
    let _ = a;

Link: http://docs.rs/paste/ [1]
Signed-off-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Link: https://lore.kernel.org/r/20230628171108.1150742-1-gary@garyguo.net
[ Added SPDX license identifier as discussed in the list and fixed typo. ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2023-08-10 22:28:02 +02:00
..
alloc rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
bindings rust: allocator: Prevent mis-aligned allocation 2023-08-04 17:10:31 +02:00
kernel rust: types: make Opaque be !Unpin 2023-08-10 01:18:34 +02:00
macros rust: macros: add paste! proc macro 2023-08-10 22:28:02 +02:00
uapi rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
.gitignore rust: uapi: Add UAPI crate 2023-04-22 01:46:45 +02:00
bindgen_parameters rust: bindgen: Add alt_instr as opaque type 2023-03-02 22:44:15 +01:00
build_error.rs rust: add build_error crate 2022-12-04 01:59:16 +01:00
compiler_builtins.rs rust: compiler_builtins: make stubs non-global 2023-01-16 21:04:34 +01:00
exports.c rust: add build_error crate 2022-12-04 01:59:16 +01:00
helpers.c rust: helpers: sort includes alphabetically in rust/helpers.c 2023-08-07 11:33:33 +02:00
Makefile scripts: make rust-analyzer for out-of-tree modules 2023-08-07 11:33:34 +02:00