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]: Generates a wrapper that dispatches calls to platform-specific implementations (e.g.,fn run()callsSelf::run_impl()).#[sys_trait_function]: Applies platform configuration to methods within a trait definition.#[sys_struct]/#[sys_enum]: Applies platform gating to types and verifies trait bounds (e.g.,Send + Sync) at compile time.#[platform_mod]: Declares modules backed by OS-specific files (e.g.,linux.rs,windows.rs) with a unified internal alias and strict visibility control.- Smart Logic: Supports explicit
includeandexcludelists with group keywords likeposixandall.
Installation
Add this to your Cargo.toml:
[]
= "0.4.0"
Or run:
Usage
1. Platform-Dependent Functions (#[sys_function])
This macro generates a method body that delegates the call to an implementation suffixed with _impl. This keeps your public API clean while separating platform logic.
use sys_function;
;
// You implement the specific logic for each platform:
2. Platform-Gating & Trait Verification (#[sys_struct])
Standard #[cfg] attributes can hide implementation errors until you compile for that specific platform. #[sys_struct] allows you to assert that a type implements certain traits (like Send or Sync) immediately.
use sys_struct;
// 1. This struct only exists on Windows.
// 2. Asserts at compile time that `WinHandle` implements `Send` and `Sync`.
// If it doesn't, you get a clear error during the build.
3. Trait Definitions (#[sys_trait_function])
Use this to define methods in a trait that only exist when compiling for specific platforms.
use sys_trait_function;
4. Platform-Dependent Modules (#[platform_mod])
This automates the common pattern of having a linux.rs and windows.rs and aliasing them to a common name for internal use.
Visibility Logic:
- External: The specific module (e.g.,
mod linux;) inherits the visibility you declared (pub). External users seemy_crate::linux. - Internal: The logical alias (
driver) is generated as a private alias (use linux as driver;). Your code usesdriver::...regardless of the OS.
// Expects src/linux.rs and src/windows.rs to exist.
// --- Internal Usage ---
Configuration Logic
You can control which platforms are targeted using include(...) and exclude(...).
| Keyword | Target OS / Expansion |
|---|---|
linux |
target_os = "linux" |
windows |
target_os = "windows" |
macos |
target_os = "macos" |
posix |
linux + macos |
all |
linux + macos + windows |
How it is calculated
- Start: Defaults to
allifincludeis omitted. - Filter: Removes any platforms specified in
exclude. - Result: Generates the appropriate
#[cfg(any(...))]attribute.
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.