Why bother writing similar code twice for blocking and async code?
When implementing both sync and async versions of API in a crate, most API of the two version are almost the same except for some async/await keyword.
maybe-async
help unifying async and sync implementation.
Write async code with normal async
, await
, and let maybe_async
handles
those async
and await
when you need a synchronized code. Switch between
sync and async by toggling is_sync
feature gate. A handy macro to
unify unit test code is also provided.
maybe-async
offers three attribute macros: maybe_async
, must_be_sync
and must_be_async
.
These macros can be applied to trait item, trait impl, function and struct impl.
must_be_async
Keep async. Add async_trait
attribute macro for trait declaration
or implementation to bring async fn support in traits.
To avoid having "Send" and "Sync" bounds placed on the async trait methods, invoke the maybeasync macro as #[mustbe_async(?Send)].
must_be_sync
Convert to sync code. Convert the async code into sync code by
removing all async move
, async
and await
keyword
maybe_async
Offers a unified feature gate to provide sync and async conversion on
demand by feature gate is_sync
, with async first policy.
Want to keep async code? add maybe_async
in dependencies with default
features, which means maybe_async
is the same as must_be_async
:
toml
[dependencies]
maybe_async = "0.1"
Wanna convert async code to sync? Add maybe_async
to dependencies with
an is_sync
feature gate. In this way, maybe_async
is the same as
must_be_sync
:
toml
[dependencies]
maybe_async = { version = "0.1", features = ["is_sync"] }
Not all async traits need futures that are dyn Future + Send
.
To avoid having "Send" and "Sync" bounds placed on the async trait
methods, invoke the maybeasync macro as #[maybeasync(?Send)] on both
the trait and the impl blocks.
sync_impl
Although most of the API are almost the same, there definitely come to a point when the async and sync version should differ greatly. For example, a MongoDB client may use the same API for async and sync verison, but the code to actually send reqeust are quite different.
Here, we can use sync_impl
to mark a synchronous implementation, and a
sync implementation shoule disappear when we want async version.
async_impl
An async implementation shoule simply disappear when we want sync version.
To avoid having "Send" and "Sync" bounds placed on the async trait methods, invoke the maybeasync macro as #[asyncimpl(?Send)].
test
Handy macro to unify async and sync unit test code.
You can specify the condition to compile to sync test code
and also the conditions to compile to async test code with given test
macro, e.x. tokio::test
, async_std::test
and etc. When only sync
condition is specified,the test code only compiles when sync condition
is met.
```rust
feature="is_sync",
async(all(not(feature="is_sync"), feature="async_std"), async_std::test),
async(all(not(feature="issync"), feature="tokio"), tokio::test) )] async fn testasyncfn() { let res = asyncfn().await; assert_eq!(res, true); } ```
The async/await language feature alters the async world of rust. Comparing with the map/and_then style, now the async code really resembles sync version code.
In many crates, the async and sync version of crates shares the same API, but the minor difference that all async code must be awaited prevent the unification of async and sync code. In other words, an async and sync implementation must be written repectively.
When implementing rust client for any services, like awz3. The higher level API of async and sync version is almost the same, such as creating or deleting a bucket, retrieving an object and etc.
Here is a proof of concept that maybe_async
can actually free us from
writing almost the same code for sync and async.
```rust type Response = String; type Url = &'static str; type Method = String;
/// InnerClient are used to actually send request, /// which differ a lot between sync and async.
trait InnerClient { async fn request(method: Method, url: Url, data: String) -> Response; #[inline] async fn post(url: Url, data: String) -> Response { Self::request(String::from("post"), url, data).await } #[inline] async fn delete(url: Url, data: String) -> Response { Self::request(String::from("delete"), url, data).await } }
/// The higher level API for end user. pub struct ServiceClient;
/// Code of upstream API are almost the same for sync and async, /// except for async/await keyword. impl ServiceClient { #[maybeasync::maybeasync] async fn createbucket(name: String) -> Response { Self::post("http://correcturl4create", String::from("mybucket")).await } #[maybeasync::maybeasync] async fn deletebucket(name: String) -> Response { Self::delete("http://correcturl4delete", String::from("mybucket")).await } // and another thousands of functions that interact with service side }
/// Synchronous implementation, will be deleted when we want an async implementation. /// Else the compiler will complain that request is defined multiple times and blabla.
impl InnerClient for ServiceClient {
fn request(method: Method, url: Url, data: String) -> Response {
// your implementation for sync, like use
// reqwest::blocking
to send request
String::from("pretend we have a response")
}
}
/// Asynchronous implementation, will be deleted when we want a sync implementation
impl InnerClient for ServiceClient {
async fn request(method: Method, url: Url, data: String) -> Response {
// your implementation for async, like use reqwest::client
// or async_std
to send request
String::from("pretend we have a response")
}
}
```
With the code above, we can toggle between a sync AWZ3 client and async one
by is_sync
feature gate when we add maybe-async
to dependency.
```rust
trait A { async fn asyncfnname() -> Result<(), ()> { Ok(()) } fn syncfnname() -> Result<(), ()> { Ok(()) } }
struct Foo;
impl A for Foo { async fn asyncfnname() -> Result<(), ()> { Ok(()) } fn syncfnname() -> Result<(), ()> { Ok(()) } }
async fn maybeasyncfn() -> Result<(), ()> { let a = Foo::asyncfnname().await?;
let b = Foo::sync_fn_name()?;
Ok(())
} ```
When maybe-async
feature gate is_sync
is NOT set, the generated code
is async code:
```rust
trait A { async fn maybeasyncfnname() -> Result<(), ()> { Ok(()) } fn syncfn_name() -> Result<(), ()> { Ok(()) } }
struct Foo;
impl A for Foo { async fn maybeasyncfnname() -> Result<(), ()> { Ok(()) } fn syncfn_name() -> Result<(), ()> { Ok(()) } }
async fn maybeasyncfn() -> Result<(), ()> { let a = Foo::maybeasyncfnname().await?; let b = Foo::syncfn_name()?; Ok(()) } ```
When maybe-async
feature gate is_sync
is set, all async keyword is
ignored and yields a sync version code:
```rust trait A { fn maybeasyncfnname() -> Result<(), ()> { Ok(()) } fn syncfn_name() -> Result<(), ()> { Ok(()) } }
struct Foo;
impl A for Foo { fn maybeasyncfnname() -> Result<(), ()> { Ok(()) } fn syncfn_name() -> Result<(), ()> { Ok(()) } }
fn maybeasyncfn() -> Result<(), ()> { let a = Foo::maybeasyncfnname()?; let b = Foo::syncfn_name()?; Ok(()) } ```
MIT