Crates.io | bisync |
lib.rs | bisync |
version | 0.2.3 |
source | src |
created_at | 2024-02-08 16:44:07.227292 |
updated_at | 2024-09-03 21:21:13.97266 |
description | write async-generic code |
homepage | |
repository | https://github.com/JM4ier/bisync |
max_upload_size | |
id | 1132340 |
size | 6,668 |
Easily write synchronous and asynchronous code at the same time.
This crate is inspired by maybe-async, but uses a different approach and tries to be more minimalistic.
Annotate the functions you'd like to have generic w.r.t. asynchronisity with #[bisync]
.
You can specialize functions by annotating them with #[only_sync]
or #[only_async]
.
You need a tiny bit of boilerplate in the parent module but that's all there is to it.
// lib.rs
#[path = "."]
pub mod asynchronous {
use bisync::asynchronous::*;
mod inner;
pub use inner::*;
}
// here you could also add `#[cfg]` attributes to enable or disable this module
#[path = "."]
pub mod blocking {
use bisync::synchronous::*;
mod inner;
pub use inner::*;
}
// inner.rs
// these are all the available definitions:
use super::{bisync, only_sync, only_async, SYNC, ASYNC};
#[bisync]
pub async fn foo() -> String {
bar().await
}
#[bisync]
async fn bar() -> String {
if ASYNC {
println!("We are in async code.");
} else if SYNC {
println!("We are in blocking code.");
} else {
panic!("This is neither async nor blocking code but a secret third thing.");
}
baz().await
}
#[only_sync]
fn baz() -> String {
ureq::get("https://example.com")
.call()
.unwrap()
.into_string()
.unwrap()
}
#[only_async]
async fn baz() -> String {
reqwest::get("https://example.com")
.await
.unwrap()
.text()
.await
.unwrap()
}
The example above will strip away all async
and await
from the functions in the blocking
module, but will leave them in the asynchronous
module.
Hence, you can easily use the functions either in a synchronous or an asynchronous context.