Crates.io | tokio-wrap |
lib.rs | tokio-wrap |
version | 0.0.3 |
source | src |
created_at | 2024-09-09 23:15:46.799065 |
updated_at | 2024-09-10 01:11:06.737784 |
description | run asynchronous code within synchronous functions |
homepage | |
repository | https://github.com/themackabu/tokio-wrap |
max_upload_size | |
id | 1369856 |
size | 11,365 |
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.
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);
}
Result