Make sure that a struct is the same across platforms


#1

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?


#2

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


#3

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


#4

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.


#5

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


#6

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.