1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*******************************************************************************
*
* Copyright (c) 2025 - 2026 Haixing Hu.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0.
*
******************************************************************************/
//! Blocking timeout condition-wait capability.
use std::time::Duration;
use crate::monitor::{
ConditionWaiter,
WaitTimeoutResult,
};
/// Waits for predicates over protected state with relative timeouts.
pub trait TimeoutConditionWaiter: ConditionWaiter {
/// Blocks until the predicate becomes true or the timeout expires.
///
/// # Arguments
///
/// * `timeout` - Maximum relative duration to wait.
/// * `predicate` - Predicate that returns `true` when the state is ready.
/// * `action` - Action to run after the predicate becomes true.
///
/// # Returns
///
/// [`WaitTimeoutResult::Ready`] with the action result, or
/// [`WaitTimeoutResult::TimedOut`] when the timeout expires first.
fn wait_until_for<R, P, F>(
&self,
timeout: Duration,
predicate: P,
action: F,
) -> WaitTimeoutResult<R>
where
P: FnMut(&Self::State) -> bool,
F: FnOnce(&mut Self::State) -> R;
/// Blocks while the predicate remains true or until the timeout expires.
///
/// # Arguments
///
/// * `timeout` - Maximum relative duration to wait.
/// * `predicate` - Predicate that returns `true` while waiting should
/// continue.
/// * `action` - Action to run after the predicate becomes false.
///
/// # Returns
///
/// [`WaitTimeoutResult::Ready`] with the action result, or
/// [`WaitTimeoutResult::TimedOut`] when the timeout expires first.
fn wait_while_for<R, P, F>(
&self,
timeout: Duration,
predicate: P,
action: F,
) -> WaitTimeoutResult<R>
where
P: FnMut(&Self::State) -> bool,
F: FnOnce(&mut Self::State) -> R;
}