#handle #future #bridge #callback #async

macro susync-macros

A proc macro crate for susync crate

1 unstable release

0.1.0 Feb 18, 2024

#87 in #handle

Download history 133/week @ 2024-02-14 25/week @ 2024-02-21 19/week @ 2024-02-28 4/week @ 2024-03-06 11/week @ 2024-03-13 3/week @ 2024-03-20 70/week @ 2024-03-27 9/week @ 2024-04-03

80 downloads per month
Used in susync

MIT license

7KB
97 lines

Overview

An util crate to complete futures through a handle. Its main purpose is to bridge async Rust and callback-based APIs.

Inspired on the future_handles crate.

The susync crate uses standard library channels under the hood. It uses thread-safe primitives but expects low contention, so it uses a single SpinMutex for shared state. By design handles are allowed to race to complete the future so it is ok to call complete on handle of a completed future.

Examples

Channel-like API:

async fn func() -> Option<u32> {
    let (future, handle) = susync::create();
    func_with_callback(|res| {
        handle.complete(res);
    });
    future.await.ok()
}

Scoped API:

async fn func() -> Option<u32> {
    let future = susync::suspend(|handle| {
        func_with_callback(|res| {
            handle.complete(res);
        });
    });
    future.await.ok()
}

Macro:

async fn func() -> Option<u32> {
    sus!(func_with_callback(|res| {})).await.ok()
}

Docs

Full documentation here.

Dependencies

~285–730KB
~17K SLoC