ergonomic-windows
Ergonomic, safe Rust wrappers for Windows APIs โ handles, processes, registry, file system, and UTF-16 strings with zero-cost abstractions.
๐ Documentation | ๐ API Reference
Features
- ๐ก๏ธ Safe by Default โ RAII wrappers automatically manage Windows handles
- ๐ค Zero-Cost Strings โ Small string optimization for UTF-16 conversions (no heap allocation for strings โค22 chars)
- โก High Performance โ Object pooling and optimized allocations for high-throughput scenarios
- ๐ฏ Ergonomic API โ Fluent builders and idiomatic Rust patterns
- ๐ Rich Error Handling โ Typed errors with Windows error code context
- ๐งช Well Tested โ 80+ tests covering edge cases, Unicode, and stress scenarios
Modules
| Module | Description |
|---|---|
string |
UTF-8 โ UTF-16 conversion with small string optimization and object pooling |
handle |
RAII wrappers for Windows HANDLE with automatic cleanup |
process |
Process creation, management, and querying |
registry |
Windows Registry read/write with type-safe values |
fs |
Windows-specific file system operations |
window |
Window creation and message handling |
error |
Rich error types with Windows error code support |
Installation
Add to your Cargo.toml:
[]
= "0.1"
Or use cargo:
Quick Start
use *;
Usage Examples
String Conversion
Convert between Rust UTF-8 strings and Windows UTF-16 strings:
use ;
// Basic conversion
let wide = to_wide;
let back = from_wide?;
assert_eq!;
// WideString for Windows API calls
let ws = new;
// ws.as_pcwstr() returns PCWSTR for Windows APIs
// Small strings are stored inline (no heap allocation!)
let small = new;
assert!; // true for strings โค22 UTF-16 chars
High-Throughput String Pool
For scenarios converting many strings, use the object pool to reuse buffers:
use WideStringPool;
let mut pool = new;
for filename in &
Handle Management
Windows handles are automatically closed when dropped:
use OwnedHandle;
use OpenOptions;
// Open a file - handle is automatically managed
let handle = new
.read
.open?;
// Clone handles safely
let cloned = handle.try_clone?;
// Handles close automatically when dropped
Process Management
Create and manage Windows processes:
use ;
// Spawn a process
let process = new
.arg
.current_dir
.env
.spawn?;
println!;
// Wait with timeout
use Duration;
match process.wait_timeout
// Open existing process
let current = open?;
println!;
Registry Access
Read and write Windows Registry values:
use ;
// Read system information
let key = open?;
if let Ok = key.get_value
// Write application settings
let app_key = create?;
app_key.set_value?;
app_key.set_value?;
app_key.set_value?;
// Enumerate keys and values
for subkey in app_key.subkeys?
File System Operations
Windows-specific file operations:
use ;
// Check file attributes
let attrs = get_attributes?;
assert!;
// Get system paths
let system_dir = get_system_directory?;
let temp_dir = get_temp_directory?;
println!;
println!;
// Move with options
move_file_with_options?;
Window Creation
Create windows and handle messages:
use ;
use *;
;
let window = new
.title
.size
.style
.build?;
window.show;
run_loop;
Safety
This crate uses unsafe code to interface with Windows APIs. All unsafe blocks are:
- Documented with safety invariants
- Minimal in scope
- Audited for correctness
The public API is entirely safe Rust. Handles are managed via RAII, preventing:
- Use-after-close bugs
- Double-close bugs
- Handle leaks
Performance
The crate is optimized for performance:
| Optimization | Impact |
|---|---|
| Small String Optimization | Zero allocation for strings โค22 chars |
| Object Pooling | Reusable buffers for high-throughput scenarios |
| Pre-allocated Buffers | Reduced allocations in string builders |
| Inline Hints | Hot paths marked for inlining |
Benchmark results (typical):
WideString::new("Hello"): 0 allocations, ~12nsto_wide/from_wide: 1 allocation each- String pool reuse: 0 allocations after warmup
Minimum Supported Rust Version
This crate requires Rust 1.70 or later.
Contributing
Contributions are welcome! Please see our Contributing Guidelines.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development
# Run tests
# Run benchmarks
# Run clippy
# Build docs
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.
Acknowledgments
Built with the excellent windows-rs crate from Microsoft.