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]: Automatically dispatches method calls to platform-specific implementations based on the OS.#[sys_struct]: Generates platform-specific type aliases for structs (e.g.,MyStructLinux,MyStructWindows).- Flexible Logic: Supports explicit inclusion (
include) and exclusion (exclude) of platforms. - Platform Groups: Includes helper keywords like
posix(Linux + macOS) orall.
§Supported Keywords
The following keywords can be used inside include(...) and exclude(...):
linuxmacoswindowsposix(Expands to:linux,macos)all(Expands to:linux,macos,windows)
§Logic
The set of allowed platforms is calculated as follows:
- Start with the
includelist. Ifincludeis omitted, it defaults toall. - Remove any platforms specified in the
excludelist. - Generate the corresponding
#[cfg(any(...))]attributes.
§Examples
§1. Using #[sys_function]
This macro generates a default method that delegates to a _impl suffixed method.
struct SystemManager;
impl SystemManager {
/// This method is available on ALL supported platforms (default).
/// It calls `reboot_impl` internally.
#[sys_function]
pub fn reboot(&self) -> Result<(), String>;
/// This method is ONLY available on Linux.
#[sys_function(include(linux))]
pub fn update_kernel(&self);
/// This method is available on Linux and macOS, but NOT Windows.
#[sys_function(exclude(windows))]
pub fn posix_magic(&self);
}
// You then implement the specific logic:
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 Linux kernel...");
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn posix_magic_impl(&self) {
println!("Running POSIX specific logic");
}
}§2. Using #[sys_struct]
This creates handy type aliases for platform-specific builds.
#[sys_struct(include(windows))]
pub struct Handle {
handle: u64,
}
// Generates:
// #[cfg(target_os = "windows")]
// pub type HandleWindows = Handle;Attribute Macros§
- sys_
function - Generates a platform-dependent method implementation.
- sys_
struct - Generates platform-specific type aliases for a struct.