Skip to main content

Crate platify

Crate platify 

Source
Expand description

§Platify

Platify streamlines the development of cross-platform Rust applications by reducing the boilerplate associated with #[cfg(...)] attributes.

Instead of manually cluttering your code with complex cfg checks and duplicate function definitions, Platify allows you to define platform-specific behavior using a clean, declarative attribute syntax.

§Features

  • #[sys_function]: Generates a wrapper function that dispatches calls to a platform-specific implementation (e.g., fn run() calls Self::run_impl()).
  • #[sys_trait_function]: Simple wrapper to apply platform-specific #[cfg(...)] to trait methods.
  • #[sys_struct] / #[sys_enum]: Applies platform gating to types and optionally enforces trait bounds (e.g., Send + Sync) at compile time.
  • #[platform_mod]: Declares platform-dependent modules backed by OS-specific files, providing a unified internal alias.

§Supported Keywords

Inside include(...) and exclude(...), you can use:

  • Platforms: linux, macos, windows
  • Groups: posix (Linux + macOS), all (Linux, macOS, Windows)

§Logic

  1. Start with the include list (defaults to all if omitted).
  2. Remove any platforms specified in the exclude list.
  3. Generate the resulting #[cfg(any(target_os = "..."))] attribute.

§Examples

§1. Using #[sys_function]

This macro generates a method body that delegates to an implementation suffixed with _impl.

struct SystemManager;

impl SystemManager {
    /// Dispatched on all platforms. Calls `Self::reboot_impl`.
    #[sys_function]
    pub fn reboot(&self) -> Result<(), String>;

    /// Only available on Linux. Calls `Self::update_kernel_impl`.
    #[sys_function(include(linux))]
    pub fn update_kernel(&self);
}

impl SystemManager {
    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
    fn reboot_impl(&self) -> Result<(), String> { Ok(()) }

    #[cfg(target_os = "linux")]
    fn update_kernel_impl(&self) { println!("Updating..."); }
}

§2. Using #[sys_struct] and traits

Unlike standard #[cfg], this allows you to verify that your platform-specific type actually implements specific traits, preventing “missing implementation” errors at a later stage.

// This struct only exists on Windows and MUST implement Send and Sync.
#[sys_struct(traits(Send, Sync), include(windows))]
pub struct WinHandle {
    handle: u64,
}

§3. Using #[platform_mod]

This automates the pattern of having a linux.rs and windows.rs and aliasing them to a common name.

// In src/lib.rs
// 1. Generates: #[cfg(target_os = "linux")] pub mod linux;
// 2. Generates: #[cfg(target_os = "linux")] use linux as driver;
#[platform_mod(include(linux, windows))]
pub mod driver;

fn init() {
    // Use the private alias 'driver' internally regardless of the OS.
    driver::init_hardware();
}

Attribute Macros§

platform_mod
Declares platform-dependent modules with a unified internal alias.
sys_enum
Applies platform config to an enum and verifies trait bounds at compile time.
sys_function
Generates a platform-dependent method implementation dispatcher.
sys_struct
Applies platform config to a struct and verifies trait bounds at compile time.
sys_trait_function
Applies platform configuration to trait method definitions.