Using Macro to call function

#1

Hi all, I am starting to use macros,. I am trying to reduce syntax of a function call, here is my following code

struct SPH {
    x: Vec<f32>,
    y: Vec<f32>,
    h: Vec<f32>,
}

impl SPH {
    fn new() -> Self {
        SPH {
            x: vec![0.; 10],
            y: vec![0.; 10],
            h: vec![0.; 10],
        }
    }
}

// pair(d_x, d_y, d_h, s_x, s_y, s_h);
fn pair(d_x: &[f32], d_y: &[f32], d_h: &mut [f32], s_x: &[f32], s_y: &[f32], s_h: &[f32]) {
    for i in 0..d_h.len() {
    for j in 0..s_h.len(){
        
        d_h[i] += s_x[j] * s_y[j] * d_x[i] * d_y[i];
    }
    }
}
macro_rules! macro_pair{
    ($func_name:ident, $dest:ty, $source:ty) => {
        $func_name(& dest.x, &dest.y, &mut dest.h, &source.x, &source.y, &source.h);
    }
}

fn main() {
    let mut sph_d = SPH::new();
    let sph_s = SPH::new();
    // pair(&sph_d.x, &sph_d.y, &mut sph_d.h, &sph_s.x, &sph_s.y, &sph_s.h);
    macro_pair!(pair, sph_d, sph_s);

}

I get the following error

   Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `dest` in this scope
  --> src/main.rs:28:22
   |
28 |         $func_name(& dest.x, &dest.y, &mut dest.h, &source.x, &source.y, &source.h);
   |                      ^^^^ not found in this scope
...
36 |     macro_pair!(pair, sph_d, sph_s);
   |     -------------------------------- in this macro invocation

error[E0425]: cannot find value `dest` in this scope
  --> src/main.rs:28:31
   |
28 |         $func_name(& dest.x, &dest.y, &mut dest.h, &source.x, &source.y, &source.h);
   |                               ^^^^ not found in this scope
...
36 |     macro_pair!(pair, sph_d, sph_s);
   |     -------------------------------- in this macro invocation

error[E0425]: cannot find value `dest` in this scope
  --> src/main.rs:28:44
   |
28 |         $func_name(& dest.x, &dest.y, &mut dest.h, &source.x, &source.y, &source.h);
   |                                            ^^^^ not found in this scope
...
36 |     macro_pair!(pair, sph_d, sph_s);
   |     -------------------------------- in this macro invocation

error[E0425]: cannot find value `source` in this scope
  --> src/main.rs:28:53
   |
28 |         $func_name(& dest.x, &dest.y, &mut dest.h, &source.x, &source.y, &source.h);
   |                                                     ^^^^^^ not found in this scope
...
36 |     macro_pair!(pair, sph_d, sph_s);
   |     -------------------------------- in this macro invocation

error[E0425]: cannot find value `source` in this scope
  --> src/main.rs:28:64
   |
28 |         $func_name(& dest.x, &dest.y, &mut dest.h, &source.x, &source.y, &source.h);
   |                                                                ^^^^^^ not found in this scope
...
36 |     macro_pair!(pair, sph_d, sph_s);
   |     -------------------------------- in this macro invocation

error[E0425]: cannot find value `source` in this scope
  --> src/main.rs:28:75
   |
28 |         $func_name(& dest.x, &dest.y, &mut dest.h, &source.x, &source.y, &source.h);
   |                                                                           ^^^^^^ not found in this scope
...
36 |     macro_pair!(pair, sph_d, sph_s);
   |     -------------------------------- in this macro invocation

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0425`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Playground like is here

0 Likes

#2

Two things:

  1. You need to use $name to access the macro variable
  2. Your $dest and $source need to be identifiers (or expressions), not types.

Fixed playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bf8000a80cdca2af93fcd803ff9a4934

1 Like

#3

Thank you. Actually when I googled for the struct type, it was written that it is of ty type.

0 Likes

#4

ty is a type name, but value of struct is just a value (so expr, or ident if you want to force, that expression is given via variable).

0 Likes