also_sync

Crates.ioalso_sync
lib.rsalso_sync
version0.1.0
sourcesrc
created_at2024-02-20 12:17:06.401622
updated_at2024-02-20 12:17:06.401622
descriptionMacros to automatically wrap async functions as sync
homepage
repositoryhttps://github.com/julianbuettner/also-sync
max_upload_size
id1146300
size48,225
Julian Büttner (julianbuettner)

documentation

README

Also Sync

This library provides a macro to automatically derive a *_sync version from your async functions. It does so by having a global tokio runtime with all features on all cores.

This allows a async-first library development while supporting sync.

How does it work?

Take a look at the following piece of code:

use also_sync::also_sync;

struct Struct;

impl Struct {
    #[also_sync]
    pub async fn foo(&self, a: i32) -> i32 {
        42 * a
    }
}

This is literally just converted to

struct Struct;

impl Struct {
    pub async fn foo(&self, a: i32) -> i32 {
        42
    }
    pub fn foo_sync(&self: a: i32) -> i32 {
        let handle = also_sync::TOKIO_RUNTIME.handle();
        handle.block_on(async move { self.foo(a).await })
    }
}

Of course if works for simple functions as well.

Commit count: 0

cargo fmt