qubit_lock/monitor/async_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 condition-wait capability.
11
12use crate::monitor::AsyncMonitorFuture;
13
14/// Waits asynchronously for predicates over protected monitor state.
15pub trait AsyncConditionWaiter {
16 /// State protected by the monitor.
17 type State;
18
19 /// Returns a future that waits until the predicate becomes true.
20 ///
21 /// The predicate and action run while the monitor state is locked.
22 fn wait_until_async<'a, R, P, F>(
23 &'a self,
24 mut predicate: P,
25 action: F,
26 ) -> AsyncMonitorFuture<'a, R>
27 where
28 R: Send + 'a,
29 P: FnMut(&Self::State) -> bool + Send + 'a,
30 F: FnOnce(&mut Self::State) -> R + Send + 'a,
31 {
32 self.wait_while_async(move |state| !predicate(state), action)
33 }
34
35 /// Returns a future that waits while the predicate remains true.
36 ///
37 /// The predicate and action run while the monitor state is locked.
38 fn wait_while_async<'a, R, P, F>(
39 &'a self,
40 predicate: P,
41 action: F,
42 ) -> AsyncMonitorFuture<'a, R>
43 where
44 R: Send + 'a,
45 P: FnMut(&Self::State) -> bool + Send + 'a,
46 F: FnOnce(&mut Self::State) -> R + Send + 'a;
47}