Skip to main content

WaitableCompletion

Struct WaitableCompletion 

Source
pub struct WaitableCompletion { /* private fields */ }
Expand description

A completion signal for synchronizing async operations.

This struct provides a thread-safe mechanism for one thread to signal completion while another thread waits for that signal with optional timeout. It handles mutex poisoning gracefully, preferring progress over panic.

§Thread Safety

All methods are safe to call from multiple threads. The implementation uses:

  • An atomic boolean for lock-free completion checks
  • A mutex + condvar for efficient blocking waits
  • Poison recovery to avoid panics in Drop contexts

§Example

use std::time::Duration;
use std::thread;

let completion = WaitableCompletion::new();

// Spawn a thread that signals completion after a delay
let completion_clone = completion.clone();
thread::spawn(move || {
    thread::sleep(Duration::from_millis(10));
    completion_clone.signal();
});

// Wait for completion with timeout
match completion.wait_timeout(Duration::from_secs(1)) {
    Ok(()) => println!("Operation completed"),
    Err(e) => println!("Timed out: {e}"),
}

Implementations§

Source§

impl WaitableCompletion

Source

pub fn new() -> Self

Creates a new completion signal in the incomplete state.

Source

pub fn new_completed() -> Self

Creates a new completion signal in the completed state.

Useful when initializing a sender that has no pending async operations.

Source

pub fn signal(&self)

Signals completion and wakes all waiting threads.

This method is safe to call multiple times; subsequent calls are no-ops for the atomic flag but will still notify waiting threads.

Source

pub fn is_complete(&self) -> bool

Checks if the operation has completed.

This is a non-blocking, lock-free check using atomic load.

Source

pub fn reset(&self)

Resets the completion state to incomplete.

Call this before starting a new async operation to reuse the signal.

Source

pub fn wait_timeout(&self, timeout: Duration) -> Result<(), String>

Waits for completion with timeout, returning a Result.

This method blocks until either:

  • The completion signal is received (returns Ok(()))
  • The timeout elapses (returns Err with timeout message)
§Poison Recovery

If the mutex is poisoned (e.g., a thread panicked while holding it), this method recovers the guard and continues waiting. This ensures robust behavior even in exceptional circumstances.

§Errors

Returns an error string if the timeout elapses before completion.

Trait Implementations§

Source§

impl Clone for WaitableCompletion

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WaitableCompletion

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WaitableCompletion

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.