Skip to main content

memlink_shm/
platform.rs

1//! Platform detection for selecting OS-specific implementations at compile time.
2
3use cfg_if::cfg_if;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Platform {
7    Linux,
8    MacOS,
9    Windows,
10}
11
12impl Platform {
13    pub fn current() -> Self {
14        cfg_if! {
15            if #[cfg(target_os = "linux")] {
16                Platform::Linux
17            } else if #[cfg(target_os = "macos")] {
18                Platform::MacOS
19            } else if #[cfg(target_os = "windows")] {
20                Platform::Windows
21            } else {
22                compile_error!("Unsupported platform. Only Linux, macOS, and Windows are supported.");
23            }
24        }
25    }
26
27    pub fn is_unix(&self) -> bool {
28        matches!(self, Platform::Linux | Platform::MacOS)
29    }
30
31    pub fn as_str(&self) -> &'static str {
32        match self {
33            Platform::Linux => "linux",
34            Platform::MacOS => "macos",
35            Platform::Windows => "windows",
36        }
37    }
38}
39
40impl std::fmt::Display for Platform {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "{}", self.as_str())
43    }
44}