Make sure that a struct is the same across platforms

If I have a module like this:

#[cfg(unix)] mod unix {
    pub struct Test { ... }
    impl Test { ... }
}

#[cfg(windows)] mod windows {
    pub struct Test { ... }
    impl Test { ... }
}

#[cfg(unix)] pub use self::unix::*;
#[cfg(windows)] pub use self::windows::*;

How can I make sure that its interface is exactly the same across platforms, including auto-traits like Send and Sync?

Define in the outer scope a trait (or trait alias) with all the dependencies that you need and have both structs implement it?

2 Likes

What if I accidentally make one Send and the other not Send, and the user incorrectly assumes it's always Send?

If your common trait/alias inherits from Send, then both structs must be Send, otherwise a compilation error about unfulfilled trait bounds will result when you will try to implement the common trait.

That'll only work until Rust adds more/custom auto-traits, as far as I know.

This is true, however I think the addition of a new auto-trait is (and should remain) sufficiently infrequent that you can monitor the Rust changelog and update your library when it happens.