WaitEvent

Struct WaitEvent 

Source
pub struct WaitEvent<T>(/* private fields */);
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§

Source§

impl<T> WaitEvent<T>

Source

pub fn new_init(initial_state: T) -> Self

Source

pub fn value(&self) -> Result<MutexGuard<'_, T>>

Source

pub fn wait( &self, timeout: Option<Duration>, checker: impl FnMut(&T) -> bool, ) -> Result<MutexGuard<'_, T>>

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
Source

pub fn wait_reset( &self, timeout: Option<Duration>, reset: impl FnMut() -> T, checker: impl FnMut(&T) -> bool, ) -> Result<T>

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);
Source

pub fn wait_with_waiter( &self, timeout: Option<Duration>, checker: impl FnMut(&T) -> bool, ) -> Result<MutexGuard<'_, T>>

Source

pub fn wait_and_reset_with_waiter( &self, timeout: Option<Duration>, checker: impl FnMut(&T) -> bool, reset: impl FnMut() -> T, ) -> Result<T>

Source

pub fn set_state(&mut self, new_state: T) -> Result<()>

Synchronously change state of WaitObject by value

Source

pub fn set_state_func<F>(&mut self, setter: F) -> Result<()>
where F: FnOnce(&T) -> T,

Synchronously change state of WaitObject by a function’s return value

§Example
use sync_wait_object::WaitEvent;

let wait = WaitEvent::new_init(0);
let mut w1 = wait.clone();
let mut w2 = wait.clone();
let mut w3 = wait.clone();

thread::spawn(move || w1.set_state_func(|v| v + 1));
thread::spawn(move || w2.set_state_func(|v| v + 1));
thread::spawn(move || w3.set_state_func(|v| v + 1));

let result = *wait.wait(Some(Duration::from_millis(200)), |v| *v == 3).unwrap();
assert_eq!(result, 3);

Trait Implementations§

Source§

impl<T: Clone> Clone for WaitEvent<T>

Source§

fn clone(&self) -> WaitEvent<T>

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 From<AutoResetEvent> for WaitEvent<bool>

Source§

fn from(value: AutoResetEvent) -> Self

Converts to this type from the input type.
Source§

impl From<ManualResetEvent> for WaitEvent<bool>

Source§

fn from(value: ManualResetEvent) -> Self

Converts to this type from the input type.
Source§

impl From<WaitEvent<bool>> for AutoResetEvent

Source§

fn from(value: WaitEvent<bool>) -> Self

Converts to this type from the input type.
Source§

impl From<WaitEvent<bool>> for ManualResetEvent

Source§

fn from(value: WaitEvent<bool>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for WaitEvent<T>

§

impl<T> RefUnwindSafe for WaitEvent<T>

§

impl<T> Send for WaitEvent<T>
where T: Send,

§

impl<T> Sync for WaitEvent<T>
where T: Send,

§

impl<T> Unpin for WaitEvent<T>

§

impl<T> UnwindSafe for WaitEvent<T>

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.