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 by procedural macro. - Write async code with normal async, await, and let maybe_async handles those async and await when you need a blocking code. - Switch between sync and async by toggling is_sync feature gate in Cargo.toml. - use must_be_async and must_be_sync to keep code in specified version - use impl_async and impl_sync to only compile code block on specified version - A handy macro to unify unit test code is also provided.

These procedural macros can be applied to the following codes: - trait item declaration - trait implmentation - function definition - struct definition

RECOMMENDATION: Enable resolver ver2 in your crate, which is introduced in Rust 1.51. If not, two crates in dependency with conflict version (one async and another blocking) can fail complilation.

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, we are forced to write an async and an sync implementation repectively.

Macros in Detail

maybe-async offers 4 set of attribute macros: maybe_async, sync_impl/async_impl, must_be_sync/must_be_async, and test.

To use maybe-async, we must know which block of codes is only used on blocking implementation, and which on async. These two implementation should share the same function signatures except for async/await keywords, and use sync_impl and async_impl to mark these implementation.

Use maybe_async macro on codes that share the same API on both async and blocking code except for async/await keywords. And use feature gate is_sync in Cargo.toml to toggle between async and blocking code.

What's Under the Hook

maybe-async compiles your code in different way with the is_sync feature gate. It remove all await and async keywords in your code under maybe_async macro and conditionally compiles codes under async_impl and sync_impl.

Here is an detailed example on what's going on whe the is_sync feature gate set or not.

```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 // Compiled code whenis_sync` is toggled off.

[asynctrait::asynctrait(?Send)]

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

struct Foo;

[asynctrait::asynctrait(?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 // Compiled code whenissync` is toggled on. trait A { fn maybeasyncfnname() -> Result<(), ()> { Ok(()) } fn syncfnname() -> 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(()) } ```

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.

The example service_client is a proof of concept that maybe_async can actually free us from writing almost the same code for sync and async. We can toggle between a sync AWZ3 client and async one by is_sync feature gate when we add maybe-async to dependency.

License

MIT