Skip to main content

Module async

Module async 

Source
Expand description

Asynchronous (.await-based) one-shot broadcast event.

Same three-state machine and one-shot semantics as sync; the only difference is the wait side. Waiter::wait returns a Wait future that resolves to Ok(()) when the event fires or Err(Disconnected) when every signaler is dropped first. Signaling itself is non-blocking, so Signaler::signal stays a plain (non-async) method.

This variant is runtime-agnostic: it depends only on std and the std::task machinery, so it works under any executor (Tokio, async-std, smol, a hand-rolled one, …). There is deliberately no wait_timeout: timeouts are a runtime concern — wrap wait() with your executor’s timeout combinator (e.g. tokio::time::timeout).

§Design notes

  • The PENDING -> {FIRED, DISCONNECTED} transition is the same once-only atomic compare-exchange as the sync version (Release on success).
  • Parked tasks register their Waker in a mutex-guarded map. A terminal transition drains the map and wakes every waker after releasing the lock, so a woken task re-polling cannot deadlock on re-entry.
  • Wait::poll re-checks the terminal state while holding the registry lock before parking — the async analog of checking the predicate under the condvar mutex — which is what prevents a lost wakeup.
  • A Wait future deregisters its waker on drop, so cancelling a pending await leaves no stale entry behind.

§Example

let (tx, rx) = rsignals::r#async::create();
assert_eq!(tx.signal(), Ok(true));
assert_eq!(block_on(rx.wait()), Ok(()));

Structs§

Disconnected
The event can no longer fire: every signaler was dropped without signaling.
NoReceivers
No waiter can ever receive the signal: every waiter handle was dropped.
Signaler
A handle used to fire the event. Cheap to clone; all clones share state.
Wait
The future returned by Waiter::wait.
Waiter
A handle used to await the event. Cheap to clone; all clones share state.

Enums§

TryWaitError
Error from the non-blocking try_wait().

Functions§

create
Create a fresh, unfired async event, returning a signaler and a waiter.