3 releases
0.0.3 | Sep 10, 2024 |
---|---|
0.0.2 | Sep 9, 2024 |
0.0.1 | Sep 9, 2024 |
#389 in Procedural macros
8KB
139 lines
tokio-wrap
Purpose
When working with asynchronous Rust code, especially with the Tokio runtime, you sometimes need to call async functions from synchronous contexts. This macro simplifies that process by automatically wrapping your function in a Tokio runtime, allowing you to use await
syntax in otherwise synchronous functions.
Usage
Here's a basic example of how to use the tokio-wrap
macro:
async fn async_function() -> String {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
"Hello, async world!".to_string()
}
#[tokio_wrap::sync]
fn sync_function() -> String {
async_function().await
}
fn main() {
let result = sync_function();
println!("Result: {}", result);
}
Features
- Automatically wraps functions in a Tokio runtime
- Supports functions with and without arguments
- Handles different return types, including
Result
Dependencies
~230–680KB
~16K SLoC