qubit_lock/monitor/async_timeout_condition_waiter.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Asynchronous timeout condition-wait capability.
11
12use std::time::Duration;
13
14use crate::monitor::{
15 AsyncConditionWaiter,
16 AsyncMonitorFuture,
17 WaitTimeoutResult,
18};
19
20/// Waits asynchronously for predicates over protected state with timeouts.
21pub trait AsyncTimeoutConditionWaiter: AsyncConditionWaiter {
22 /// Returns a future that waits until the predicate becomes true or times out.
23 ///
24 /// The timeout budget is measured from this method call.
25 fn wait_until_for_async<'a, R, P, F>(
26 &'a self,
27 timeout: Duration,
28 mut predicate: P,
29 action: F,
30 ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
31 where
32 R: Send + 'a,
33 P: FnMut(&Self::State) -> bool + Send + 'a,
34 F: FnOnce(&mut Self::State) -> R + Send + 'a,
35 {
36 self.wait_while_for_async(timeout, move |state| !predicate(state), action)
37 }
38
39 /// Returns a future that waits while the predicate remains true or times out.
40 ///
41 /// The timeout budget is measured from this method call.
42 fn wait_while_for_async<'a, R, P, F>(
43 &'a self,
44 timeout: Duration,
45 predicate: P,
46 action: F,
47 ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
48 where
49 R: Send + 'a,
50 P: FnMut(&Self::State) -> bool + Send + 'a,
51 F: FnOnce(&mut Self::State) -> R + Send + 'a;
52}