Define module as same name on condition

I have two rust files, say impl_linux.rs and impl_mac.rs, export the same data struct for different platforms.

How can I define these two modules in parent module as same on different #[cfg()]?

Something like this:

#[cfg(linux)]
pub mod impl_linux as impl;

#[cfg(mac)]
pub mod impl_mac as impl;

On my phone right now, but a combination of #[cfg_attr] and #[path] should work.

Using cfg(target_os = "linux") and target_os = "macos" this should work already like you wrote.
Like this:

#[cfg(target_os = "linux")]
mod impl_linux;
#[cfg(target_os = "linux")]
pub use impl_linux as impl;

and similar for target_os = "macos"

(impl might be reserved, you might need to use some other name).
@H2CO3's proposed way would also work.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.