pub struct WaitEvent<T>(_);
Expand description

Create a wait event object of any type T. To use this wait object in multi-threaded scenario, just clone the object and distribute it.

This wait object is just a wrapper of Mutex and Condvar combination with the suggested pattern (from Rust document) for waiting a value.

There are two ways to wait. The first is just to want until an expected value.

use std::thread;
let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();

thread::spawn(move || {
    for i in 1..=3 {
        wait_handle.set_state(i).unwrap();
    }
});

let timeout = std::time::Duration::from_secs(1);
let r#final = *wait3.wait(Some(timeout), |i| *i == 3).unwrap();
let current = *wait3.value().unwrap();
assert_eq!(r#final, 3);
assert_eq!(current, 3);

The second is to wait and then reset the value to a desired state.

use std::thread;
let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();

thread::spawn(move || {
    for i in 1..=3 {
        wait_handle.set_state(i).unwrap();
    }
});

let timeout = std::time::Duration::from_secs(1);
let r#final = wait3.wait_reset(Some(timeout), || 1, |i| *i == 3).unwrap();
let current = *wait3.value().unwrap();
assert_eq!(r#final, 3);
assert_eq!(current, 1);

Implementations§

Wait until the checker returns true, or timed-out from timeout.

Arguments
  • timeout - Maximum wait time
  • checker - Checker function, once it returns true, the wait ends

Wait until the checker returns true, or timed-out from timeout. If the wait ends from checker condition, the interval value is reset by reset.

Arguments
  • timeout - Maximum wait time
  • reset - Function that provides a reset value
  • checker - Checker function, once it returns true, the wait ends
Examples
use sync_wait_object::{WaitEvent, WaitObjectError};

let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();

thread::spawn(move || {
    for i in 1..=3 {
        thread::sleep(Duration::from_millis(50));
        wait_handle.set_state(i).unwrap();
    }
});

let timeout = Duration::from_millis(250);
let r#final = wait3.wait_reset(Some(timeout), || 0, |i| *i == 5);
let current = *wait3.value().unwrap();
assert_eq!(r#final, Err(WaitObjectError::Timeout));
assert_eq!(current, 3);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.