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.