tokio-wrap

Crates.iotokio-wrap
lib.rstokio-wrap
version0.0.3
sourcesrc
created_at2024-09-09 23:15:46.799065
updated_at2024-09-10 01:11:06.737784
descriptionrun asynchronous code within synchronous functions
homepage
repositoryhttps://github.com/themackabu/tokio-wrap
max_upload_size
id1369856
size11,365
Mack (theMackabu)

documentation

README

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
Commit count: 0

cargo fmt