| Crates.io | switchy_async |
| lib.rs | switchy_async |
| version | 0.1.4 |
| created_at | 2025-05-07 20:53:18.116281+00 |
| updated_at | 2025-07-21 19:17:20.63975+00 |
| description | Switchy Async runtime package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1664459 |
| size | 55,976 |
Async runtime abstraction and utilities for MoosicBox applications.
The MoosicBox Async package provides:
Add this to your Cargo.toml:
[dependencies]
switchy_async = { path = "../async" }
# Enable specific features
switchy_async = {
path = "../async",
features = ["tokio", "rt-multi-thread", "io", "sync", "time", "macros"]
}
# For testing with simulation
switchy_async = {
path = "../async",
features = ["simulator", "macros"]
}
use switchy_async::{Builder, GenericRuntime};
// Create runtime with builder
let runtime = Builder::new()
.max_blocking_threads(Some(4))
.build()?;
// Use generic runtime interface
runtime.block_on(async {
println!("Hello from async runtime!");
});
// Wait for runtime to complete
runtime.wait()?;
// Tokio backend (when tokio feature enabled)
#[cfg(feature = "tokio")]
use switchy_async::{task, time, io, sync};
#[cfg(feature = "tokio")]
{
// Spawn tasks
let handle = task::spawn(async {
time::sleep(time::Duration::from_millis(100)).await;
"Task completed"
});
let result = handle.await?;
println!("{}", result);
}
// Simulation backend (when simulator feature enabled)
#[cfg(feature = "simulator")]
use switchy_async::simulator;
#[cfg(feature = "simulator")]
{
let runtime = Builder::new().build()?;
runtime.block_on(async {
// Deterministic async execution for testing
println!("Simulation runtime");
});
}
use switchy_async::thread_id;
// Get unique thread ID
let id = thread_id();
println!("Current thread ID: {}", id);
#[cfg(feature = "macros")]
use switchy_async::{select, inject_yields, inject_yields_mod};
#[cfg(feature = "macros")]
{
// Use async macros
select! {
result1 = async_operation_1() => {
println!("Operation 1 completed: {:?}", result1);
}
result2 = async_operation_2() => {
println!("Operation 2 completed: {:?}", result2);
}
}
}
// Inject yields for simulation testing
#[inject_yields]
async fn my_async_function() {
// Function body with automatic yield injection
}
use switchy_async::Error;
match runtime.wait() {
Ok(()) => println!("Runtime completed successfully"),
Err(Error::IO(io_err)) => println!("I/O error: {}", io_err),
Err(Error::Join(join_err)) => println!("Join error: {}", join_err),
}
tokio: Enable Tokio async runtimesimulator: Enable simulation runtime for testingrt-multi-thread: Multi-threaded Tokio runtimeio: Async I/O operationssync: Synchronization primitivestime: Timing utilitiesutil: Additional utilitiesmacros: Async macros (select!, etc.)macros: Enable async macros and yield injectionThis package is designed for: