如何使用 macro_rules 定义带有可选 #[cfg] 的结构?
How do I use macro_rules to define a struct with optional #[cfg]?
我正在编写一个 Rust 程序,它从 C DLL 动态加载一些函数。我写了一个辅助结构来加载它们:
// 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")) },
}
}
}
因为有 100 多个函数要加载,我写了一个宏来完成脏活:
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,
}
}
但是有些函数是平台相关的,我想给它们添加#[cfg(target_os)]。如何更改此宏以匹配可选的#[cfg]?我想让它适用于以下示例代码:
macro_rules! define_load_functions {
pub FuncWrapper {
func1,
func2,
#[cfg(target_os = "windows")] func3,
#[cfg(target_os = "linux")] func4,
}
}
谢谢。
下面的方法行得通吗?
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,
});
在基于 Linux 的系统上扩展上述宏给出以下结果:
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(),
}
}
}
我正在编写一个 Rust 程序,它从 C DLL 动态加载一些函数。我写了一个辅助结构来加载它们:
// 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")) },
}
}
}
因为有 100 多个函数要加载,我写了一个宏来完成脏活:
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,
}
}
但是有些函数是平台相关的,我想给它们添加#[cfg(target_os)]。如何更改此宏以匹配可选的#[cfg]?我想让它适用于以下示例代码:
macro_rules! define_load_functions {
pub FuncWrapper {
func1,
func2,
#[cfg(target_os = "windows")] func3,
#[cfg(target_os = "linux")] func4,
}
}
谢谢。
下面的方法行得通吗?
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,
});
在基于 Linux 的系统上扩展上述宏给出以下结果:
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(),
}
}
}