Traits for compile-time interface checks
⚓ Rust 📅 2026-03-11 👤 surdeus 👁️ 3I'm creating a library with multiple backends selected by feature flags (e.g. by OS: LinuxPlatform, MacOsPlatform, WindowsPlatform), where only one backend should be compiled depending on the enabled feature and available just as Platform.
I want to ensure that all backends expose the same interface by creating traits (e.g. PlatformOps, FileOps, DeviceOps, etc.) - in this case IDE will highlight all missing methods.
But this means users of my library always need to import these traits to access the methods.
What is the usual best practice in this situation?
As I can see many libraries don't seem to use traits for this, but in this case I need to compile with every feature flag to make sure it's correct.
Of course, I could temporarily change impl File to impl FileOps for File during develompent, but that feels wrong.
3 posts - 3 participants
🏷️ Rust_feed