Section 12 of 25

Platforms

Contents

New in version 1.1.

A product may ship on a single operating system today (for example, only macOS). SLAT lays out the code so that the next operating system is work in one folder, not a search through the whole repository. There is one rule: only drivers/src/platform/ knows the operating system. Everything else asks for concepts (a process group, a secret, the data folder, opening a file), never for system calls.

The list of supported platforms belongs to the repository (its contributor guide), not to SLAT.

coreopen_path · secret::*ProcessGroupdrivers::platformmod.rsone API · no logicunix.rsunixmacos.rsmacoslinux.rslinuxwindows.rswindowsunsupported.rsnot(macos)

12.1. Layout

drivers/src/platform/
  mod.rs          the only platform API: selects a file with #[cfg] and pub use; no logic
  unix.rs         shared by macOS and Linux: process groups, signals, file modes
  macos.rs        Keychain, revealing in Finder, the login shell, …
  linux.rs        only when the port starts: Secret Service, xdg-open, …
  windows.rs      only when the port starts: Job Objects, Credential Manager, ACLs, …
  unsupported.rs  every function returns Err(PlatformError::Unsupported)
  • A file for an operating system the product does not support yet does not exist, for the same reason an empty crate "for later" does not. unsupported.rs takes its place.
  • unsupported.rs is not dead code. Thanks to it, the whole workspace except app compiles for every platform from the first day. A leak of system code into core becomes a compile error in CI, not a discovery on the day of the port.
  • mod.rs looks the same in every repository:
#[cfg(unix)]
mod unix;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::*;

#[cfg(not(target_os = "macos"))]
mod unsupported;
#[cfg(not(target_os = "macos"))]
pub use unsupported::*;

A port changes only this list of cfg lines. Every platform file exposes the same set of functions and types with the same signatures. A missing function is a compile error on that platform. This is not a trait: on any given build there is always exactly one implementation.

12.2. Contracts

Each concept has a contract that means the same thing on every operating system. Only the inside differs.

Concept API in drivers::platform macOS Linux Windows
process group ProcessGroup::spawn, terminate(grace), proved_dead() process group id; SIGTERM, grace period, SIGKILL; kill(-pgid, 0) returns ESRCH as macOS Job Object; TerminateJobObject; the job has no processes left
secrets secret::read, secret::write, secret::delete Keychain Secret Service (libsecret) Credential Manager
private file and folder fs::write_private, fs::create_private_dir mode 0o600 / 0o700 as macOS ACL: owner only
open in the system open_path, reveal_path, open_url open, open -R xdg-open ShellExecuteW, explorer /select,
user environment login_env() the login shell (the PATH of an app started from the Dock is minimal) as macOS registry and process environment
find a program which(program) PATH PATH PATH plus PATHEXT
machine facts machine::cores, machine::memory, machine::free_space sysctl, statfs /proc, statfs system API

Contract rules:

  • A contract function does not return system types (pid_t, HANDLE). It returns types from drivers or kit.
  • Proof, not hope, on every platform: "the process is dead" is an established fact, not a signal that was sent.
  • Where an operating system cannot do what the contract requires, the function returns an error that names what is missing, never a silent substitute.

A short example of a contract and its macOS side:

drivers/src/platform/macos.rs:

pub fn open_path(path: &Path) -> Result<(), PlatformError> {
    let status = Command::new("/usr/bin/open").arg(path).status()?;
    status.success().then_some(()).ok_or(PlatformError::Refused("open"))
}

drivers/src/platform/unsupported.rs:

pub fn open_path(_path: &Path) -> Result<(), PlatformError> {
    Err(PlatformError::Unsupported("open_path"))
}

12.3. What is forbidden outside platform/

Forbidden outside drivers/src/platform/ Use instead
#[cfg(unix)], #[cfg(windows)], #[cfg(target_os = …)] a contract from 12.2
std::os::unix, std::os::windows, libc, nix, windows-sys, security-framework a contract from 12.2
sh -c, bash, zsh, cmd /c, powershell Command::new(program).args(…); if a shell is truly needed, platform chooses it
system program names: open, xdg-open, security, osascript, explorer open_path, secret::*, …
hard-coded paths: ~, $HOME, /tmp, /usr/bin, /Applications, Library/ kit::paths (12.4)
joining paths with a string / Path::join
file modes as numbers (0o600) fs::write_private

The only exception is app/src/platform.rs, for things only Tauri knows: the title bar style, the application menu, the window activation policy. Nothing but window settings.

12.4. Paths, files and text

  • Folders come only from kit::paths: data(), config(), cache(), logs(), temp(). Underneath is a library that follows each system's convention (macOS ~/Library/Application Support/<App>, Linux XDG, Windows %APPDATA%). Data that already lives under an older path stays there (the compatibility exception from 5.4): when the older folder exists, kit::paths returns it, so the code still asks only kit::paths. The choice depends on whether that folder exists, not on the operating system, so kit itself has no #[cfg]; the system's convention comes from the library.
  • File names the application creates are lower case and never differ only by letter case. The default file systems of macOS and Windows do not tell them apart; Linux does.
  • Do not rename or delete a file that someone holds open. Windows refuses. Atomic writes go through kit::durable_file, which closes the file before replacing it.
  • Long paths. Windows limits paths to about 260 characters without the \\?\ prefix. Keep working copies and run folders shallow; the prefix is added by platform, never by a slice.
  • Text from processes is read tolerantly of \r\n. Files the application writes use \n.
  • A path sent to the window is UTF-8. A path that cannot be written as UTF-8 is an error that names the file, never an unwrap().

12.5. Platform tests

  • The contract test lives in drivers/tests/it/platform_contract.rs, a module of the crate's single integration target (14.1), and is the same for every operating system: a process is dead after terminate, a secret comes back after it is written and is gone after it is deleted, a private file cannot be read by another user. Every supported platform passes the same set.
  • Tests outside drivers do not use a shell or signals. A process needed by a test is a small Rust program (a binary in a test crate), not sh -c 'sleep 10'.
  • A platform #[cfg] in tests is allowed only in the tests of drivers.

12.6. Enforcement

Rule Who enforces it
the prohibitions from 12.3 a grep check with a baseline list (section 16)
crates other than app compile on every platform cargo check --workspace --exclude <app> on native CI runners for Ubuntu and Windows. Not a cross-check from macOS: build scripts with C code need the target's compiler.
the contract works platform_contract.rs on a runner for each supported platform

12.7. Porting to a new operating system

  1. drivers/src/platform/<system>.rs: every function of the contract. The compiler lists what is missing.
  2. mod.rs: the new system's cfg stops pointing at unsupported.rs.
  3. platform_contract.rs is green on that system's runner, and the runner joins CI.
  4. app: bundle and signing configuration for the system, and app/src/platform.rs if needed.
  5. The repository's contributor guide adds the system to the list of supported platforms.

If a port requires a change in core, something leaked. First a separate pull request moves the leak behind a contract, then the port continues.