1use std::{
13 ops::Deref,
14 sync::Arc,
15 time::Duration,
16};
17
18#[cfg(feature = "async")]
19use super::{
20 AsyncConditionWaiter,
21 AsyncMonitorFuture,
22 AsyncNotificationWaiter,
23 AsyncTimeoutConditionWaiter,
24 AsyncTimeoutNotificationWaiter,
25};
26use super::{
27 ConditionWaiter,
28 MockMonitor,
29 NotificationWaiter,
30 Notifier,
31 TimeoutConditionWaiter,
32 TimeoutNotificationWaiter,
33 WaitTimeoutResult,
34 WaitTimeoutStatus,
35};
36
37pub struct ArcMockMonitor<T> {
39 inner: Arc<MockMonitor<T>>,
41}
42
43impl<T> ArcMockMonitor<T> {
44 pub fn new(state: T) -> Self {
54 Self {
55 inner: Arc::new(MockMonitor::new(state)),
56 }
57 }
58
59 pub fn elapsed(&self) -> Duration {
61 self.inner.elapsed()
62 }
63
64 pub fn set_elapsed(&self, elapsed: Duration) {
70 self.inner.set_elapsed(elapsed);
71 }
72
73 pub fn advance(&self, duration: Duration) {
79 self.inner.advance(duration);
80 }
81
82 pub fn reset_elapsed(&self) {
84 self.inner.reset_elapsed();
85 }
86
87 pub fn read<R, F>(&self, f: F) -> R
89 where
90 F: FnOnce(&T) -> R,
91 {
92 self.inner.read(f)
93 }
94
95 pub fn write<R, F>(&self, f: F) -> R
97 where
98 F: FnOnce(&mut T) -> R,
99 {
100 self.inner.write(f)
101 }
102
103 pub fn write_notify_one<R, F>(&self, f: F) -> R
105 where
106 F: FnOnce(&mut T) -> R,
107 {
108 self.inner.write_notify_one(f)
109 }
110
111 pub fn write_notify_all<R, F>(&self, f: F) -> R
113 where
114 F: FnOnce(&mut T) -> R,
115 {
116 self.inner.write_notify_all(f)
117 }
118
119 pub fn notify_one(&self) {
121 self.inner.notify_one();
122 }
123
124 pub fn notify_all(&self) {
126 self.inner.notify_all();
127 }
128
129 pub fn wait(&self) {
131 <MockMonitor<T> as NotificationWaiter>::wait(self.inner.as_ref());
132 }
133
134 pub fn wait_for(&self, timeout: Duration) -> WaitTimeoutStatus {
144 <MockMonitor<T> as TimeoutNotificationWaiter>::wait_for(self.inner.as_ref(), timeout)
145 }
146
147 pub fn wait_until<R, P, F>(&self, predicate: P, action: F) -> R
158 where
159 P: FnMut(&T) -> bool,
160 F: FnOnce(&mut T) -> R,
161 {
162 <MockMonitor<T> as ConditionWaiter>::wait_until(self.inner.as_ref(), predicate, action)
163 }
164
165 pub fn wait_while<R, P, F>(&self, predicate: P, action: F) -> R
176 where
177 P: FnMut(&T) -> bool,
178 F: FnOnce(&mut T) -> R,
179 {
180 <MockMonitor<T> as ConditionWaiter>::wait_while(self.inner.as_ref(), predicate, action)
181 }
182
183 pub fn wait_until_for<R, P, F>(
196 &self,
197 timeout: Duration,
198 predicate: P,
199 action: F,
200 ) -> WaitTimeoutResult<R>
201 where
202 P: FnMut(&T) -> bool,
203 F: FnOnce(&mut T) -> R,
204 {
205 <MockMonitor<T> as TimeoutConditionWaiter>::wait_until_for(
206 self.inner.as_ref(),
207 timeout,
208 predicate,
209 action,
210 )
211 }
212
213 pub fn wait_while_for<R, P, F>(
226 &self,
227 timeout: Duration,
228 predicate: P,
229 action: F,
230 ) -> WaitTimeoutResult<R>
231 where
232 P: FnMut(&T) -> bool,
233 F: FnOnce(&mut T) -> R,
234 {
235 <MockMonitor<T> as TimeoutConditionWaiter>::wait_while_for(
236 self.inner.as_ref(),
237 timeout,
238 predicate,
239 action,
240 )
241 }
242
243 #[cfg(feature = "async")]
245 pub fn wait_async(&self) -> AsyncMonitorFuture<'_, ()>
246 where
247 T: Send,
248 {
249 <MockMonitor<T> as AsyncNotificationWaiter>::wait_async(self.inner.as_ref())
250 }
251
252 #[cfg(feature = "async")]
262 pub fn wait_for_async(&self, timeout: Duration) -> AsyncMonitorFuture<'_, WaitTimeoutStatus>
263 where
264 T: Send,
265 {
266 <MockMonitor<T> as AsyncTimeoutNotificationWaiter>::wait_for_async(
267 self.inner.as_ref(),
268 timeout,
269 )
270 }
271
272 #[cfg(feature = "async")]
283 pub fn wait_until_async<'a, R, P, F>(
284 &'a self,
285 predicate: P,
286 action: F,
287 ) -> AsyncMonitorFuture<'a, R>
288 where
289 T: Send,
290 R: Send + 'a,
291 P: FnMut(&T) -> bool + Send + 'a,
292 F: FnOnce(&mut T) -> R + Send + 'a,
293 {
294 <MockMonitor<T> as AsyncConditionWaiter>::wait_until_async(
295 self.inner.as_ref(),
296 predicate,
297 action,
298 )
299 }
300
301 #[cfg(feature = "async")]
312 pub fn wait_while_async<'a, R, P, F>(
313 &'a self,
314 predicate: P,
315 action: F,
316 ) -> AsyncMonitorFuture<'a, R>
317 where
318 T: Send,
319 R: Send + 'a,
320 P: FnMut(&T) -> bool + Send + 'a,
321 F: FnOnce(&mut T) -> R + Send + 'a,
322 {
323 <MockMonitor<T> as AsyncConditionWaiter>::wait_while_async(
324 self.inner.as_ref(),
325 predicate,
326 action,
327 )
328 }
329
330 #[cfg(feature = "async")]
342 pub fn wait_until_for_async<'a, R, P, F>(
343 &'a self,
344 timeout: Duration,
345 predicate: P,
346 action: F,
347 ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
348 where
349 T: Send,
350 R: Send + 'a,
351 P: FnMut(&T) -> bool + Send + 'a,
352 F: FnOnce(&mut T) -> R + Send + 'a,
353 {
354 <MockMonitor<T> as AsyncTimeoutConditionWaiter>::wait_until_for_async(
355 self.inner.as_ref(),
356 timeout,
357 predicate,
358 action,
359 )
360 }
361
362 #[cfg(feature = "async")]
374 pub fn wait_while_for_async<'a, R, P, F>(
375 &'a self,
376 timeout: Duration,
377 predicate: P,
378 action: F,
379 ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
380 where
381 T: Send,
382 R: Send + 'a,
383 P: FnMut(&T) -> bool + Send + 'a,
384 F: FnOnce(&mut T) -> R + Send + 'a,
385 {
386 <MockMonitor<T> as AsyncTimeoutConditionWaiter>::wait_while_for_async(
387 self.inner.as_ref(),
388 timeout,
389 predicate,
390 action,
391 )
392 }
393}
394
395impl<T> Notifier for ArcMockMonitor<T> {
396 fn notify_one(&self) {
398 Self::notify_one(self);
399 }
400
401 fn notify_all(&self) {
403 Self::notify_all(self);
404 }
405}
406
407impl<T> NotificationWaiter for ArcMockMonitor<T> {
408 fn wait(&self) {
410 self.inner.wait();
411 }
412}
413
414impl<T> TimeoutNotificationWaiter for ArcMockMonitor<T> {
415 fn wait_for(&self, timeout: Duration) -> WaitTimeoutStatus {
417 self.inner.wait_for(timeout)
418 }
419}
420
421impl<T> ConditionWaiter for ArcMockMonitor<T> {
422 type State = T;
423
424 fn wait_while<R, P, F>(&self, predicate: P, action: F) -> R
426 where
427 P: FnMut(&Self::State) -> bool,
428 F: FnOnce(&mut Self::State) -> R,
429 {
430 self.inner.wait_while(predicate, action)
431 }
432}
433
434impl<T> TimeoutConditionWaiter for ArcMockMonitor<T> {
435 fn wait_while_for<R, P, F>(
437 &self,
438 timeout: Duration,
439 predicate: P,
440 action: F,
441 ) -> WaitTimeoutResult<R>
442 where
443 P: FnMut(&Self::State) -> bool,
444 F: FnOnce(&mut Self::State) -> R,
445 {
446 self.inner.wait_while_for(timeout, predicate, action)
447 }
448}
449
450#[cfg(feature = "async")]
451impl<T: Send> AsyncNotificationWaiter for ArcMockMonitor<T> {
452 fn wait_async<'a>(&'a self) -> AsyncMonitorFuture<'a, ()> {
454 self.inner.wait_async()
455 }
456}
457
458#[cfg(feature = "async")]
459impl<T: Send> AsyncTimeoutNotificationWaiter for ArcMockMonitor<T> {
460 fn wait_for_async<'a>(
462 &'a self,
463 timeout: Duration,
464 ) -> AsyncMonitorFuture<'a, WaitTimeoutStatus> {
465 self.inner.wait_for_async(timeout)
466 }
467}
468
469#[cfg(feature = "async")]
470impl<T: Send> AsyncConditionWaiter for ArcMockMonitor<T> {
471 type State = T;
472
473 fn wait_while_async<'a, R, P, F>(&'a self, predicate: P, action: F) -> AsyncMonitorFuture<'a, R>
475 where
476 R: Send + 'a,
477 P: FnMut(&Self::State) -> bool + Send + 'a,
478 F: FnOnce(&mut Self::State) -> R + Send + 'a,
479 {
480 self.inner.wait_while_async(predicate, action)
481 }
482}
483
484#[cfg(feature = "async")]
485impl<T: Send> AsyncTimeoutConditionWaiter for ArcMockMonitor<T> {
486 fn wait_while_for_async<'a, R, P, F>(
488 &'a self,
489 timeout: Duration,
490 predicate: P,
491 action: F,
492 ) -> AsyncMonitorFuture<'a, WaitTimeoutResult<R>>
493 where
494 R: Send + 'a,
495 P: FnMut(&Self::State) -> bool + Send + 'a,
496 F: FnOnce(&mut Self::State) -> R + Send + 'a,
497 {
498 self.inner.wait_while_for_async(timeout, predicate, action)
499 }
500}
501
502impl<T> AsRef<MockMonitor<T>> for ArcMockMonitor<T> {
503 fn as_ref(&self) -> &MockMonitor<T> {
505 self.inner.as_ref()
506 }
507}
508
509impl<T> Deref for ArcMockMonitor<T> {
510 type Target = MockMonitor<T>;
511
512 fn deref(&self) -> &Self::Target {
514 self.inner.as_ref()
515 }
516}
517
518impl<T> Clone for ArcMockMonitor<T> {
519 fn clone(&self) -> Self {
521 Self {
522 inner: self.inner.clone(),
523 }
524 }
525}
526
527impl<T> From<T> for ArcMockMonitor<T> {
528 fn from(value: T) -> Self {
530 Self::new(value)
531 }
532}
533
534impl<T: Default> Default for ArcMockMonitor<T> {
535 fn default() -> Self {
537 Self::new(T::default())
538 }
539}