Platform

Trait Platform 

Source
pub trait Platform: Send + Sync {
    // Required methods
    fn page_size(&self) -> usize;
    fn cpu_count(&self) -> usize;
    fn total_memory(&self) -> Result<u64, PlatformError>;
    fn available_memory(&self) -> Result<u64, PlatformError>;
    fn line_separator(&self) -> &'static str;
    fn path_separator(&self) -> char;
    fn platform_name(&self) -> &'static str;
    fn temp_dir(&self) -> PathBuf;
    fn is_elevated(&self) -> bool;
    fn set_permissions(
        &self,
        path: &Path,
        mode: u32,
    ) -> Result<(), PlatformError>;
    fn is_executable(&self, path: &Path) -> bool;
    fn sync_file<'life0, 'life1, 'async_trait>(
        &'life0 self,
        file: &'life1 File,
    ) -> Pin<Box<dyn Future<Output = Result<(), PlatformError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

Platform abstraction trait for OS-specific operations

This trait provides a clean interface for platform-specific functionality, allowing the bootstrap layer to work with different operating systems without conditional compilation throughout the codebase.

§Design Principles

  • Stateless: All methods are stateless and thread-safe
  • Async-aware: File operations are async-compatible
  • Error-handling: All fallible operations return Result
  • Cross-platform: Same interface works on Unix and Windows

§Implementation Notes

Implementations should use native platform APIs:

  • Unix: POSIX APIs via libc, /proc, /sys
  • Windows: Windows API via winapi
  • Fallbacks: Standard Rust APIs when platform APIs unavailable

Required Methods§

Source

fn page_size(&self) -> usize

Get the system page size for memory alignment

Used for:

  • Memory-mapped I/O alignment
  • Buffer sizing optimizations
  • Cache-friendly allocations
§Returns

Page size in bytes (typically 4096 on most systems)

Source

fn cpu_count(&self) -> usize

Get the number of available CPU cores

Returns the number of logical processors available to the process. Used for determining optimal parallelism levels.

§Returns

Number of CPU cores (at least 1)

Source

fn total_memory(&self) -> Result<u64, PlatformError>

Get total system memory in bytes

§Returns

Total physical memory in bytes

§Errors

Returns error if system information cannot be retrieved

Source

fn available_memory(&self) -> Result<u64, PlatformError>

Get available system memory in bytes

§Returns

Available (free) memory in bytes

§Errors

Returns error if system information cannot be retrieved

Source

fn line_separator(&self) -> &'static str

Get the platform-specific line separator

§Returns
  • Unix: "\n"
  • Windows: "\r\n"
Source

fn path_separator(&self) -> char

Get the platform-specific path separator for PATH environment variable

§Returns
  • Unix: ':'
  • Windows: ';'
Source

fn platform_name(&self) -> &'static str

Get the platform name

§Returns

Platform identifier: “linux”, “macos”, “windows”, etc.

Source

fn temp_dir(&self) -> PathBuf

Get the platform-specific temporary directory

§Returns

Path to system temp directory

Source

fn is_elevated(&self) -> bool

Check if running with elevated privileges

§Returns
  • Unix: true if effective UID is 0 (root)
  • Windows: true if running as Administrator
Source

fn set_permissions(&self, path: &Path, mode: u32) -> Result<(), PlatformError>

Set file permissions (Unix-specific, no-op on Windows)

§Arguments
  • path: Path to file
  • mode: Unix permission bits (e.g., 0o644)
§Errors

Returns error if permissions cannot be set

Source

fn is_executable(&self, path: &Path) -> bool

Check if a path points to an executable file

§Arguments
  • path: Path to check
§Returns
  • Unix: true if execute bit set
  • Windows: true if extension is .exe, .bat, .cmd, .com
Source

fn sync_file<'life0, 'life1, 'async_trait>( &'life0 self, file: &'life1 File, ) -> Pin<Box<dyn Future<Output = Result<(), PlatformError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Flush file buffers to disk

Ensures all buffered data is written to physical storage.

§Arguments
  • file: File to sync
§Errors

Returns error if sync operation fails

Implementors§