Maybe-Async Procedure Macro

Why bother writing similar code twice for blocking and async code?

Build Status MIT licensed Latest Version maybe-async

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.

Key features

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.

Motivation

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.

Examples

rust client for services

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.

[maybeasync::maybeasync]

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.

[maybeasync::syncimpl]

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

[maybeasync::asyncimpl]

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.

Example for maybe_async conversion

```rust

[maybeasync::maybeasync(?Send)]

trait A { async fn asyncfnname() -> Result<(), ()> { Ok(()) } fn syncfnname() -> Result<(), ()> { Ok(()) } }

struct Foo;

[maybeasync::maybeasync(?Send)]

impl A for Foo { async fn asyncfnname() -> Result<(), ()> { Ok(()) } fn syncfnname() -> Result<(), ()> { Ok(()) } }

[maybeasync::maybeasync]

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

[async_trait(?Send)]

trait A { async fn maybeasyncfnname() -> Result<(), ()> { Ok(()) } fn syncfn_name() -> Result<(), ()> { Ok(()) } }

struct Foo;

[async_trait(?Send)]

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(()) } ```

License

MIT