async_file
Asynchronous file I/O operations with priority handling.
async_file
provides a simple yet powerful API for performing asynchronous file operations
in Rust. It closely follows the standard library's file API design while adding async
support and priority-based scheduling.
Features
- Async Operations: All file operations are asynchronous, allowing for non-blocking I/O
- Priority Scheduling: Every operation accepts a priority parameter for fine-grained control
- Memory Safety: Uses an opaque
Data
type to safely handle OS-managed memory allocations - Platform Agnostic: Backend-agnostic API with a default std implementation
Quick Start
use ;
// Open a file with unit test priority
let file = open.await?;
// Read up to 1KB of data
let data = file.read.await?;
println!;
Architecture Overview
Opaque Type Design
The library uses opaque wrapper types that hide platform-specific implementations:
File
: Wraps platform file handles behind a unified async interfaceData
: Encapsulates OS-managed memory buffers for safe async I/OMetadata
: Provides file information in a platform-agnostic wayError
: Wraps platform-specific error types
This design ensures API stability while allowing platform-specific optimizations.
Single Operation Constraint
Important: Only one operation may be in-flight at a time per file handle.
This constraint:
- Prevents race conditions on file position
- Simplifies the implementation
- Avoids many classes of concurrency bugs
- Matches typical file I/O patterns
Attempting concurrent operations on the same file handle will result in undefined behavior.
Memory Management Strategy
The library uses an opaque Data
type instead of user-provided buffers. This design:
- Prevents use-after-free bugs: If an async operation is cancelled (by dropping the future), the OS might still write to the buffer. OS-managed allocation prevents this.
- Enables platform optimizations: Different platforms can use their optimal memory allocation strategies.
- Simplifies the API: Users don't need to manage buffer lifetimes across await points.
Common Usage Patterns
Reading a File Completely
use ;
// For small files, use read_all()
let file = open.await?;
let contents = file.read_all.await?;
// Convert to String if needed
let text = String from_utf8
.expect;
Sequential Reading with Seeking
use ;
use SeekFrom;
let mut file = open.await?;
// Read header (first 128 bytes)
let header = file.read.await?;
// Skip to data section at byte 1024
file.seek.await?;
let data = file.read.await?;
Checking File Existence
use ;
// Check if file exists before opening
if exists.await
Priority-Based Operations
use ;
// High priority for critical operations
let file = open.await?;
let data = file.read.await?;
// Unit test priority for testing
let test_file = open.await?;
let test_data = test_file.read.await?;
API Overview
File Operations
use ;
use SeekFrom;
// Open a file
let mut file = open.await?;
// Read data
let data = file.read.await?;
// Seek to position
let pos = file.seek.await?;
// Get metadata
let metadata = file.metadata.await?;
println!;
// Read entire file
let contents = file.read_all.await?;
Memory Management
The Data
type provides safe access to OS-managed memory:
let data = file.read.await?;
// Access as a slice
let bytes: & = data.as_ref;
// Convert to owned data (may require copying)
let boxed: = data.into_boxed_slice;
Utility Functions
// Check if a file exists
let exists = exists.await;
// Configure default origin for relative paths
set_default_origin;
Priority System
All operations require a priority parameter from the priority
crate for scheduling control:
use Priority;
// Different priority levels
let high_priority = highest_async;
let test_priority = unit_test;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.