I am writing a cross-platform library in Rust where I need to use specific ABI strings for Windows and Unix. My current solution is:
#[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)
}
}
// ...
}
Is there any way to reduce the copy-and-pasted code in definitions?