| Crates.io | switchy_async_macros |
| lib.rs | switchy_async_macros |
| version | 0.1.4 |
| created_at | 2025-05-07 20:39:42.910933+00 |
| updated_at | 2025-07-21 19:16:16.280371+00 |
| description | Switchy Async macros package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1664442 |
| size | 12,378 |
Procedural macros for async function transformation and yield injection.
The MoosicBox Async Macros package provides:
#[inject_yields] and inject_yields_mod! macrossimulator feature is enabled.await#[inject_yields]: Attribute macro for individual functions and impl blocksinject_yields_mod!: Procedural macro for entire modulessimulator feature flag.await expressions with yield pointsAdd this to your Cargo.toml:
[dependencies]
switchy_async_macros = { path = "../async/macros" }
# Enable for simulation testing
switchy_async_macros = {
path = "../async/macros",
features = ["simulator"]
}
use switchy_async_macros::inject_yields;
// Original function
#[inject_yields]
async fn my_async_function() {
let result1 = some_async_operation().await;
let result2 = another_async_operation().await;
result1 + result2
}
// With simulator feature enabled, transforms to:
// async fn my_async_function() {
// let result1 = {
// let __yield_res = some_async_operation().await;
// ::switchy::unsync::task::yield_now().await;
// __yield_res
// };
// let result2 = {
// let __yield_res = another_async_operation().await;
// ::switchy::unsync::task::yield_now().await;
// __yield_res
// };
// result1 + result2
// }
use switchy_async_macros::inject_yields;
#[inject_yields]
impl MyStruct {
async fn method1(&self) -> i32 {
self.async_operation().await
}
async fn method2(&self) -> String {
self.another_operation().await
}
// Non-async methods are unaffected
fn sync_method(&self) -> bool {
true
}
}
use switchy_async_macros::inject_yields_mod;
// Transform entire module
inject_yields_mod! {
mod my_module {
async fn function1() {
operation1().await;
}
async fn function2() {
operation2().await;
}
impl SomeStruct {
async fn method(&self) {
self.operation().await;
}
}
}
}
// Without simulator feature - no transformation
#[cfg(not(feature = "simulator"))]
#[inject_yields]
async fn my_function() {
// Executes normally without yield injection
some_operation().await;
}
// With simulator feature - yield injection enabled
#[cfg(feature = "simulator")]
#[inject_yields]
async fn my_function() {
// Automatically transformed with yield points
some_operation().await; // -> wrapped with yield_now()
}
Before:
let result = async_call().await;
After (with simulator feature):
let result = {
let __yield_res = async_call().await;
::switchy::unsync::task::yield_now().await;
__yield_res
};
async fn declarations#[cfg(test)]
mod tests {
use switchy_async_macros::inject_yields;
#[inject_yields]
async fn test_function() {
// Deterministic execution for testing
let result = async_operation().await;
assert_eq!(result, expected_value);
}
}
// Library functions that need deterministic testing
#[inject_yields]
pub async fn library_function() -> Result<Data, Error> {
let data = fetch_data().await?;
let processed = process_data(data).await?;
Ok(processed)
}
simulator: Enable yield injection transformationThis package is designed for: