| Crates.io | async_sync |
| lib.rs | async_sync |
| version | 0.1.0 |
| created_at | 2024-12-05 00:31:45.120415+00 |
| updated_at | 2024-12-05 00:31:45.120415+00 |
| description | A crate for handling asynchronous and synchronous operations seamlessly. |
| homepage | |
| repository | https://github.com/packetandpine/async_sync |
| max_upload_size | |
| id | 1472709 |
| size | 19,172 |
Effortlessly integrate synchronous and asynchronous code in Rust.
The async_sync crate simplifies the boundaries between sync and async workflows, providing tools for retry mechanisms, backoff strategies, timeouts, diagnostics, and parallel execution of sync tasks.
Tokio and Async-std runtimes.Add async_sync to your Cargo.toml:
[dependencies]
async_sync = "0.1.0"
tokio = { version = "1", features = ["full"] }
Handle transient failures in sync tasks with retries and exponential backoff.
use async_sync::{sync_to_async_with_retries, Backoff};
use std::sync::{Arc, Mutex};
use std::time::Duration;
fn flaky_function(counter: Arc<Mutex<i32>>) -> Result<String, &'static str> {
let mut count = counter.lock().unwrap();
*count += 1;
if *count < 3 {
Err("Failure")
} else {
Ok("Success")
}
}
#[tokio::main]
async fn main() {
let counter = Arc::new(Mutex::new(0));
let result = sync_to_async_with_retries(
|| flaky_function(Arc::clone(&counter)),
5, // Max retries
Duration::from_millis(100), // Initial delay
Backoff::Exponential, // Backoff strategy
)
.await;
match result {
Ok(value) => println!("Task succeeded: {}", value),
Err(err) => eprintln!("Task failed: {:?}", err),
}
}
Handle transient failures in sync tasks with retries and exponential backoff.
use async_sync::{sync_to_async_with_retries, Backoff};
use std::sync::{Arc, Mutex};
use std::time::Duration;
fn flaky_function(counter: Arc<Mutex<i32>>) -> Result<String, &'static str> {
let mut count = counter.lock().unwrap();
*count += 1;
if *count < 3 {
Err("Failure")
} else {
Ok("Success")
}
}
#[tokio::main]
async fn main() {
let counter = Arc::new(Mutex::new(0));
let result = sync_to_async_with_retries(
|| flaky_function(Arc::clone(&counter)),
5, // Max retries
Duration::from_millis(100), // Initial delay
Backoff::Exponential, // Backoff strategy
)
.await;
match result {
Ok(value) => println!("Task succeeded: {}", value),
Err(err) => eprintln!("Task failed: {:?}", err),
}
}
Execute multiple synchronous tasks in parallel and collect their results.
use async_sync::parallel_sync_to_async;
fn heavy_computation(x: i32) -> i32 {
std::thread::sleep(std::time::Duration::from_millis(200));
x * x
}
#[tokio::main]
async fn main() {
let tasks: Vec<_> = (1..=5).map(|x| move || heavy_computation(x)).collect();
let results = parallel_sync_to_async(tasks).await;
for (i, result) in results.into_iter().enumerate() {
println!("Task {} result: {:?}", i + 1, result.unwrap());
}
}
Run sync functions using the Tokio or Async-std runtime.
use async_sync::{sync_to_async_with_runtime, Runtime};
fn heavy_computation(x: i32) -> i32 {
std::thread::sleep(std::time::Duration::from_millis(200));
x * x
}
fn main() {
let result = sync_to_async_with_runtime(Runtime::Tokio, || heavy_computation(4));
println!("Result: {:?}", result.unwrap());
}
If you have any questions, feedback, or just want to connect, feel free to reach out!