简体   繁体   中英

Is it possible to declare variables procedurally using Rust macros?

Basically, there are two parts to this question:

  1. Can you pass an unknown identifier to a macro in Rust ?

  2. Can you combine strings to generate new variable names in a Rust macro?

For example, something like:

macro_rules! expand(
  ($x:ident) => (
    let mut x_$x = 0;
  )
)

Calling expand!(hi) obvious fails because hi is an unknown identifier; but can you somehow do this?

ie. The equivalent in C of something like:

#include <stdio.h>
#define FN(Name, base) \
  int x1_##Name = 0 + base; \
  int x2_##Name = 2 + base; \
  int x3_##Name = 4 + base; \
  int x4_##Name = 8 + base; \
  int x5_##Name = 16 + base;

int main() {
  FN(hello, 10)
  printf("%d %d %d %d %d\n", x1_hello, x2_hello, x3_hello, x4_hello, x5_hello);
  return 0;
}

Why you say, what a terrible idea. Why would you ever want to do that?

I'm glad you asked!

Consider this rust block:

{
   let marker = 0;
   let borrowed = borrow_with_block_lifetime(data, &marker); 
   unsafe {
      perform_ffi_call(borrowed);
   }
}

You now have a borrowed value with an explicitly bounded lifetime (marker) that isn't using a structure lifetime, but that we can guarantee exists for the entire scope of the ffi call; at the same time we don't run into obscure errors where a * is de-referenced unsafely inside an unsafe block and so the compiler doesn't catch it as an error, despite the error being made inside a safe block .

(see also Why are all my pointers pointing to the same place with to_c_str() in rust? )

The use a macro that can declare temporary variables for this purpose would considerably ease the troubles I have fighting with the compiler. That's why I want to do this.

Yes however this is only available as a nightly-only experimental API which may be removed.

You can pass arbitrary identifier into a macro and yes, you can concatenate identifiers into a new identifier using concat_idents!() macro:

#![feature(concat_idents)]

macro_rules! test {
    ($x:ident) => ({
        let z = concat_idents!(hello_, $x);
        z();
    })
}

fn hello_world() {  }

fn main() {
    test!(world);
}

However, as far as I know, because concat_idents!() itself is a macro, you can't use this concatenated identifier everywhere you could use plain identifier, only in certain places like in example above, and this, in my opinion, is a HUGE drawback. Just yesterday I tried to write a macro which could remove a lot of boilerplate in my code, but eventually I was not able to do it because macros do not support arbitrary placement of concatenated identifiers.

BTW, if I understand your idea correctly, you don't really need concatenating identifiers to obtain unique names. Rust macros, contrary to the C ones, are hygienic . This means that all names of local variables introduced inside a macro won't leak to the scope where this macro is called. For example, you could assume that this code would work:

macro_rules! test {
    ($body:expr) => ({ let x = 10; $body })
}

fn main() {
    let y = test!(x + 10);
    println!("{}", y);
}

That is, we create a variable x and put an expression after its declaration. It is then natural to think that x in test!(x + 10) refers to that variable declared by the macro, and everything should be fine, but in fact this code won't compile:

main3.rs:8:19: 8:20 error: unresolved name `x`.
main3.rs:8     let y = test!(x + 10);
                             ^
main3.rs:3:1: 5:2 note: in expansion of test!
main3.rs:8:13: 8:27 note: expansion site
error: aborting due to previous error

So if all you need is uniqueness of locals, then you can safely do nothing and use any names you want, they will be unique automatically. This is explained in macro tutorial, though I find the example there somewhat confusing.

In cases where concat_idents doesn't work (which is most cases I'd like to use it) changing the problem from concatenated identifiers to using namespaces does work.

That is, instead of the non-working code:

macro_rules! test {
    ($x:ident) => ({
        struct concat_idents!(hello_, $x) {}
        enum contact_idents!(hello_, $x) {}
    })
}

The user can name the namespace, and then have preset names as shown below:

macro_rules! test {
    ($x:ident) => ({
        mod $x {
            struct HelloStruct {}
            enum HelloEnum {}
        }
    })
}

Now you have a name based on the macro's argument. This technique is only helpful in specific cases.

There is also https://github.com/dtolnay/paste , which works well in casese where concat_idents is underpowered or in cases where you can't target the nightly compiler.

macro_rules! foo_macro {
    ( $( $name:ident ),+ ) => {
        paste::item! {
            #[test]
            fn [<test_ $name>]() {
                assert! false
            }
        }
    };
}

You can collect your identifiers into a struct if you don't want to use nightly and external crates and your identifiers are types.

use std::fmt::Debug;

fn print_f<T: Debug>(v: &T){
    println!("{:?}", v);
}

macro_rules! print_all {
    ($($name:ident),+) => {
        struct Values{
            $($name: $name),+
        }
        let values = Values{
            $(
                $name: $name::default()
            ),+
        };
        $(
            print_f(&values.$name);
        )+
    };
}

fn main(){
    print_all!(String, i32, usize);
}

This code prints

""
0
0

If you fear that Value will conflict with some type name, you can use some long UUID as part of the name:

struct Values_110cf51d7a694c808e6fe79bf1485d5b{
   $($name:$name),+
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM