我正在Rust中编写一个跨平台库,我需要为Windows和Unix使用特定的ABI字符串。我目前的解决方案是:
#[macro_export]
macro_rules! create_plugin {
(@internal $name:ident) => {
#[no_mangle]
#[cfg(target_os = "windows")]
pub unsafe extern "stdcall" fn Test(data: *const *const u32) -> bool {
// Source 1
}
#[no_mangle]
#[cfg(not(target_os = "windows"))]
pub unsafe extern "C" fn Test(data: *const *const u32) -> bool {
// Source 1 (duplicate)
}
}
// ...
}
有没有办法减少定义中的复制粘贴代码?