
Rust 跨平台适配层,统一接口调用各平台 Native API,实现零开销抽象。
| 平台 |
系统标识 |
实现方式 |
| Android |
target_os = "android" |
NDK (ndk crate) + /proc/self/cmdline |
| iOS |
target_os = "ios" |
objc2-ui-kit / objc2-foundation |
| OpenHarmony |
target_os = "ohos" |
NDK C API |
| Windows |
target_os = "windows" |
dirs + sysinfo + windows crate |
| Linux |
target_os = "linux" |
dirs + sysinfo + x11rb (X11 RandR) |
| macOS |
target_os = "macos" |
dirs + sysinfo + objc2-app-kit + libc |
[dependencies]
rust-platform-adapter = { git = "https://github.com/mocikadev/rust-platform-adapter.git" }
use rust_platform_adapter::prelude::*;
fn main() -> Result<()> {
// 系统类型判断(编译时常量,零开销)
if is_android() {
println!("Running on Android");
}
if is_ohos() {
println!("Running on OpenHarmony/HarmonyOS");
}
// 获取平台信息
let info = platform_info()?;
println!("OS: {:?}", info.os_type);
println!("Version: {}", info.os_version);
println!("Model: {}", info.device_model);
println!("Arch: {:?}", info.cpu_arch);
println!("Form: {:?}", info.device_form);
// 获取路径
println!("Data: {:?}", data_dir()?);
println!("Cache: {:?}", cache_dir()?);
println!("Temp: {:?}", temp_dir()?);
println!("Documents: {:?}", document_dir()?);
// 获取屏幕信息
let screen = screen_info()?;
println!("Screen: {}x{}", screen.width, screen.height);
println!("DPI: {}", screen.dpi);
println!("Scale: {}", screen.scale_factor);
// 设备形态判断
if device_form().is_mobile() {
println!("Running on mobile device");
}
if device_form().is_desktop() {
println!("Running on desktop");
}
Ok(())
}
| 函数 |
说明 |
current_os() |
获取当前操作系统类型 |
is_android() |
是否为 Android |
is_ios() |
是否为 iOS |
is_ohos() |
是否为 OpenHarmony/HarmonyOS |
is_windows() |
是否为 Windows |
is_linux() |
是否为 Linux |
is_macos() |
是否为 macOS |
| 函数 |
说明 |
platform_info() |
获取完整平台信息 |
os_version() |
获取操作系统版本 |
device_model() |
获取设备型号 |
cpu_arch() |
获取 CPU 架构 |
device_form() |
获取设备形态 |
| 函数 |
说明 |
data_dir() |
应用数据目录(内部存储) |
cache_dir() |
缓存目录(内部存储) |
temp_dir() |
临时目录 |
document_dir() |
文档目录 |
external_data_dir() |
应用数据目录(外部存储) |
external_cache_dir() |
缓存目录(外部存储) |
| 函数 |
说明 |
screen_info() |
获取完整屏幕信息 |
screen_width() |
屏幕宽度 |
screen_height() |
屏幕高度 |
scale_factor() |
缩放因子 |
orientation() |
屏幕方向 |
# Android
cargo build --target aarch64-linux-android
cargo build --target armv7-linux-androideabi
cargo build --target x86_64-linux-android
# iOS
cargo build --target aarch64-apple-ios
# OpenHarmony
cargo build --target aarch64-unknown-linux-ohos
cargo build --target x86_64-unknown-linux-ohos
# Windows
cargo build --target x86_64-pc-windows-gnu
rust-platform-adapter/
├── Cargo.toml # workspace 根配置
├── .cargo/config.toml # 交叉编译 linker 配置
├── crates/
│ ├── adapter/ # 核心适配库
│ │ ├── Cargo.toml
│ │ └── src/
│ │ ├── lib.rs # 入口 + prelude
│ │ ├── api.rs # 便捷 API
│ │ ├── types.rs # 公共类型(OsType, DeviceForm, CpuArch 等)
│ │ ├── error.rs # 错误类型(PlatformError)
│ │ ├── traits/ # Trait 定义
│ │ │ ├── device.rs
│ │ │ ├── path.rs
│ │ │ └── screen.rs
│ │ └── platform/ # 平台实现
│ │ ├── linux/
│ │ ├── windows/
│ │ ├── macos/
│ │ ├── ios/
│ │ ├── android/
│ │ └── ohos/
│ └── test-app/ # 测试应用
└── docs/ # 文档
thiserror — 错误处理
dirs — 标准目录路径(桌面平台)
sysinfo — 系统信息(桌面平台)
- Android:
ndk
- iOS/macOS:
objc2, objc2-ui-kit, objc2-app-kit, objc2-foundation
- macOS:
libc (sysctl)
- Windows:
windows crate (Win32 API)
- Linux:
x11rb (X11 RandR,可选 feature x11)
- OpenHarmony: NDK C API(
libdeviceinfo_ndk.z.so, libability_runtime.so, libnative_display_manager.so)
MIT