MoosicBox Async
Async runtime abstraction and utilities for MoosicBox applications.
Overview
The MoosicBox Async package provides:
- Runtime Abstraction: Generic async runtime interface
- Multi-Backend: Support for Tokio and simulation runtimes
- Builder Pattern: Flexible runtime configuration
- Feature-Gated: Modular async functionality
- Thread Management: Thread ID tracking and management
Features
Runtime Abstraction
- GenericRuntime: Common interface for all async runtimes
- Runtime Builder: Configurable runtime construction
- Backend Selection: Choose between Tokio and simulation runtimes
- Future Support: Standard Future trait integration
Backend Support
- Tokio: Production async runtime
- Simulator: Deterministic simulation runtime for testing
- Feature-Gated: Enable only needed backends
Async Utilities
- Thread ID: Unique thread identification
- Task Management: Task spawning and joining
- IO Operations: Async I/O primitives (feature-gated)
- Synchronization: Async synchronization primitives (feature-gated)
- Timers: Async timing utilities (feature-gated)
Installation
Add this to your Cargo.toml:
[]
= { = "../async" }
# Enable specific features
= {
path = "../async",
= ["tokio", "rt-multi-thread", "io", "sync", "time", "macros"]
}
# For testing with simulation
= {
path = "../async",
= ["simulator", "macros"]
}
Usage
Runtime Creation
use ;
// Create runtime with builder
let runtime = new
.max_blocking_threads
.build?;
// Use generic runtime interface
runtime.block_on;
// Wait for runtime to complete
runtime.wait?;
Backend-Specific Usage
// Tokio backend (when tokio feature enabled)
use ;
Simulation Backend
// Simulation backend (when simulator feature enabled)
use simulator;
Thread Management
use thread_id;
// Get unique thread ID
let id = thread_id;
println!;
Macros and Utilities
use ;
// Inject yields for simulation testing
async
Error Handling
use Error;
match runtime.wait
Feature Flags
Backend Selection
tokio: Enable Tokio async runtimesimulator: Enable simulation runtime for testing
Tokio Features
rt-multi-thread: Multi-threaded Tokio runtimeio: Async I/O operationssync: Synchronization primitivestime: Timing utilitiesutil: Additional utilitiesmacros: Async macros (select!, etc.)
Additional Features
macros: Enable async macros and yield injection
Runtime Comparison
Tokio Runtime
- Production: Optimized for production use
- Performance: High-performance async execution
- Ecosystem: Full Tokio ecosystem support
- Threading: Multi-threaded execution
Simulation Runtime
- Testing: Deterministic execution for tests
- Reproducible: Consistent behavior across runs
- Debugging: Easier debugging and tracing
- Controlled: Precise control over execution order
Dependencies
- Futures: Core Future trait and utilities
- Tokio: Optional Tokio runtime (feature-gated)
- Switchy Async Macros: Macro utilities
- ThisError: Error handling
Integration
This package is designed for:
- Application Runtime: Main async runtime for applications
- Testing: Deterministic async testing with simulation
- Library Development: Runtime-agnostic async libraries
- Performance: High-performance async applications
- Cross-Platform: Consistent async behavior across platforms