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>
impl<T> WaitEvent<T>
pub fn new_init(initial_state: T) -> Self
pub fn value(&self) -> Result<MutexGuard<'_, T>>
Sourcepub fn wait(
&self,
timeout: Option<Duration>,
checker: impl FnMut(&T) -> bool,
) -> Result<MutexGuard<'_, T>>
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 timechecker
- Checker function, once it returnstrue
, the wait ends
Sourcepub fn wait_reset(
&self,
timeout: Option<Duration>,
reset: impl FnMut() -> T,
checker: impl FnMut(&T) -> bool,
) -> Result<T>
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 timereset
- Function that provides a reset valuechecker
- Checker function, once it returnstrue
, 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);
pub fn wait_with_waiter( &self, timeout: Option<Duration>, checker: impl FnMut(&T) -> bool, ) -> Result<MutexGuard<'_, T>>
pub fn wait_and_reset_with_waiter( &self, timeout: Option<Duration>, checker: impl FnMut(&T) -> bool, reset: impl FnMut() -> T, ) -> Result<T>
Sourcepub fn set_state(&mut self, new_state: T) -> Result<()>
pub fn set_state(&mut self, new_state: T) -> Result<()>
Synchronously change state of WaitObject by value
Sourcepub fn set_state_func<F>(&mut self, setter: F) -> Result<()>
pub fn set_state_func<F>(&mut self, setter: F) -> Result<()>
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 From<AutoResetEvent> for WaitEvent<bool>
impl From<AutoResetEvent> for WaitEvent<bool>
Source§fn from(value: AutoResetEvent) -> Self
fn from(value: AutoResetEvent) -> Self
Converts to this type from the input type.
Source§impl From<ManualResetEvent> for WaitEvent<bool>
impl From<ManualResetEvent> for WaitEvent<bool>
Source§fn from(value: ManualResetEvent) -> Self
fn from(value: ManualResetEvent) -> 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more