Platify
Platify streamlines cross-platform Rust development by removing the boilerplate associated with #[cfg(...)] attributes.
Instead of cluttering your code with repetitive checks and manual dispatch logic, Platify allows you to define platform-specific behavior using a clean, declarative attribute syntax.
Features
#[sys_function]: Automatically dispatches method calls to platform-specific implementations (e.g.,fn run()->fn run_impl()).#[sys_struct]: Generates platform-specific type aliases (e.g.,MyStruct->MyStructLinux).- Smart Logic: Supports explicit
includeandexcludelists. - Group Keywords: Use helpers like
posix(Linux + macOS) orall.
Installation
Add this to your Cargo.toml:
[]
= "0.1.1"
Or run:
Usage
1. Platform-Dependent Functions (#[sys_function])
This macro generates a default method that delegates the call to a suffixed implementation (e.g., _impl). It automatically applies the correct #[cfg] guards based on your configuration.
use sys_function;
;
// Implementation details
2. Platform-Specific Struct Aliases (#[sys_struct])
Useful when you need specific types for FFI or OS interactions but want to keep a unified naming convention in your platform-agnostic code.
use sys_struct;
// This generates:
//
// #[cfg(target_os = "windows")]
// pub type HandleWindows = Handle;
Configuration Logic
You can control which platforms are targeted using include(...) and exclude(...).
| Keyword | Description |
|---|---|
linux |
Target Linux (target_os = "linux") |
windows |
Target Windows (target_os = "windows") |
macos |
Target macOS (target_os = "macos") |
posix |
Expands to linux and macos |
all |
Expands to linux, macos, and windows |
How it is calculated
- Start: If
includeis present, start with that set. If omitted, start withall. - Filter: Remove any platforms specified in
exclude. - Result: The macro generates
#[cfg(any(target_os = "..."))]for the remaining platforms.
Examples
include(linux)→ Only Linux.exclude(windows)→ Linux + macOS.include(posix), exclude(macos)→ Only Linux.
License
This project is licensed under the MIT License - see the LICENSE file for details.