Crates.io | susync |
lib.rs | susync |
version | 0.2.0 |
source | src |
created_at | 2023-12-26 23:08:47.22137 |
updated_at | 2024-02-18 17:07:05.108141 |
description | An util crate to complete futures through a handle. Its main purpose is to bridge async Rust and callback-based APIs. |
homepage | |
repository | https://github.com/tcerqueira/susync |
max_upload_size | |
id | 1081269 |
size | 23,559 |
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.
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()
}
Full documentation here.