A lucky ant
A lucky ant

Reputation: 109

How do I use macro_rules to define a struct with optional #[cfg]?

I'm writing a Rust program that loads some functions dynamically from a C DLL. I write a helper struct to load them:

// some function pointer definitions
// type func1_ptr = unsafe extern "C" func1() -> ();
// .....

pub struct FuncWrapper {
    pub func1: func1_type,
}

impl FuncWrapper {
    pub fn new() -> Self {
        Self {
            func1: unsafe { mem::transmute(load_function(lib, "func1")) },
        }
    }
}

Since there are 100+ functions to load, I write a macro to do the dirty work:

use paste::paste;

macro_rules! define_load_functions {
  ($v:vis $name:ident { $($func_name:ident),* $(,)?}) => {
  paste! {
    $v struct $name {
    $(
      pub $func_name: [<$func_name _ptr>],
    )*
    }

    impl $name {
      pub fn new() -> Self {
        Self {
          $(
          $func_name: unsafe {
            mem::transmute(load_function(ptr, stringify!($func_name)))
          },
          )*
        }
      }
    }
  }
  }
}

macro_rules! define_load_functions {
pub FuncWrapper {
    func1,
}
}

However some functions are platform-dependent and I want to add #[cfg(target_os)] to them. How do I change this macro to match an optional #[cfg]? I want to make it work on following example code:

macro_rules! define_load_functions {
pub FuncWrapper {
    func1,
    func2,
    #[cfg(target_os = "windows")] func3,
    #[cfg(target_os = "linux")] func4,
}
}

Thank you.

Upvotes: 5

Views: 1490

Answers (1)

Jason
Jason

Reputation: 5575

Would the following work?

macro_rules! define_load_functions {
    (
        $struct_vis: vis $struct_name: ident {
            $( $(#[cfg($os_attr: meta)])? $fn_name: ident $(,)? )*
        }
    ) => {
        $struct_vis struct $struct_name {
            $(
                $(#[cfg($os_attr)])?
                $fn_name: String,
            )*
        }
        
        impl $struct_name {
            pub fn new() -> Self {
                Self {
                    $(
                        $(#[cfg($os_attr)])?
                        $fn_name: "Example".to_owned(),
                    )*
                }
            }
        }
    };
}

define_load_functions!(pub Example {
    func1,
    #[cfg(target_os = "linux")] func2,
    #[cfg(target_os = "windows")] func3,
    #[cfg(target_os = "macos")] func4,
    #[cfg(target_os = "linux")] func5,
    func6,
});

Expanding the above macro on a Linux based system gives the following:

pub struct Example {
    func1: String,
    #[cfg(target_os = "linux")]
    func2: String,
    #[cfg(target_os = "linux")]
    func5: String,
    func6: String,
}

impl Example {
    pub fn new() -> Self {
        Self {
            func1: "Example".to_owned(),
            #[cfg(target_os = "linux")]
            func2: "Example".to_owned(),
            #[cfg(target_os = "linux")]
            func5: "Example".to_owned(),
            func6: "Example".to_owned(),
        }
    }
}

Upvotes: 5

Related Questions