#async-version #sync #await #macro #function #suffix #modifier

macro syncify

A simple macro for writing async functions once and getting both the async version and a sync version that strips all async function modifiers and all .await suffixes

1 unstable release

0.1.0 Jul 30, 2024

#1761 in Procedural macros

Download history 188/week @ 2024-08-17 385/week @ 2024-08-24 16/week @ 2024-08-31 184/week @ 2024-09-07 49/week @ 2024-09-14 26/week @ 2024-09-21 25/week @ 2024-09-28 6/week @ 2024-10-05 18/week @ 2024-10-12 81/week @ 2024-10-19 52/week @ 2024-10-26 32/week @ 2024-11-02 73/week @ 2024-11-09 42/week @ 2024-11-16 15/week @ 2024-11-23 24/week @ 2024-11-30

154 downloads per month
Used in 3 crates

MIT/Apache

7KB
52 lines

Syncify

Two functions for the price of one: a simplistic rust macro for writing async functions once and getting both the async version and a sync version that strips all async function modifiers and all .await suffixes.

Status: it works, but this is not intended to be a high-quality, general-purpose crate. The macro error messages are bad, the features are barebones. But it does what we need it to do, and that suffices.

Usage

Place the #[syncify(name_of_sync_mod_variant)] on a mod item. Annotate all use items inside that mod that should be different in the sync version with #[syncify_replace(<alternate-use-item>)].

Example:

use syncify::syncify;
use syncify::syncify_replace;

#[syncify(greet_sync)]
mod greet {
    #[syncify_replace(use crate::speaking_sync::speak;)] // A sync function for speaking.
    use crate::speaking::speak; // An async function for speaking.

    pub async fn do_greet(name: &str) -> usize {
        speak(name).await;
        return name.len();
    }
}

Expands to:

use syncify::syncify;
use syncify::syncify_replace;

mod greet {
    use crate::speaking::speak; // An async function for speaking.

    pub async fn do_greet(name: &str) -> usize {
        speak(name).await;
        return name.len();
    }
}

mod greet_sync {
    use crate::speaking_sync::speak; // A sync function for speaking.

    pub fn do_greet(name: &str) -> usize {
        speak(name);
        return name.len();
    }
}

Dependencies

~215–650KB
~16K SLoC