1

I need to generate setters and getters for my FFI code.

Is there a way I can append the variable name to the setter and getter function names in a macro? I tried stringify!($var + set) but it generated a bunch of errors.

Is hiding it in a module for each variable the only way?

macro_rules! gen {
    ($var:ident: $type:ident) => {
        pub fn set(new_val: $type) {
            unsafe { $var = new_val; }
        }
        pub fn get() -> $type {
            unsafe { $var }
        }
    }
}
gen!(myvar1: u32);
gen!(myvar2: i32);

For myvar1 the setter and getter would be myvar1_set and myvar1_get respectively.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
KDN
  • 485
  • 2
  • 7
  • 18
  • I leave this here, in case someone would not find on rust forum. With [paste](https://docs.rs/paste/1.0.3/paste/index.html) crate it's possible now. – fanyul Nov 16 '20 at 14:01

0 Answers0