A cute trick I've used a couple times:
macro_rules! m {( $($tt:tt)* ) => { $($tt)* }};
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
m! {
#[derive(Debug)]
pub struct RouteItem {
pfx: u8,
target: IpAddr,
ip: IpAddr,
mac: Vec<u8>,
}
#[cfg_attr(not(target_os = "windows"), path = "posix/posix.rs")]
mod platform;
pub use platform::*;
#[test]
fn test() {
let v = none_host_route_get().unwrap();
println!("{v:?}");
}
}
The macro which just takes any source and emits it unchanged enables a single #[cfg]
to apply to multiple items without putting them into a mod
.