qubit_lock/monitor/std_monitor.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//! # StdMonitor
11//!
12//! Provides a synchronous monitor built from a mutex and a condition variable.
13//! A monitor protects one shared state value and binds that state to the
14//! condition variable used to wait for changes. This is the same low-level
15//! mechanism as using [`std::sync::Mutex`] and [`std::sync::Condvar`] directly,
16//! but packaged so callers do not have to keep a mutex and its matching
17//! condition variable as separate fields.
18//!
19//! The high-level APIs ([`StdMonitor::read`], [`StdMonitor::write`],
20//! [`StdMonitor::wait_while`], and [`StdMonitor::wait_until`]) are intended for
21//! short critical sections and simple guarded-suspension flows. The lower-level
22//! [`StdMonitor::lock`] API returns a [`StdMonitorGuard`], which supports
23//! [`StdMonitorGuard::wait`] and [`StdMonitorGuard::wait_timeout`] for more complex
24//! state machines such as thread pools.
25//!
26
27use std::{
28 sync::{
29 Condvar,
30 Mutex,
31 },
32 time::{
33 Duration,
34 Instant,
35 },
36};
37
38use super::std_monitor_guard::StdMonitorGuard;
39use super::{
40 wait_timeout_result::WaitTimeoutResult,
41 wait_timeout_status::WaitTimeoutStatus,
42};
43
44/// Shared state protected by a mutex and a condition variable.
45///
46/// `StdMonitor` is useful when callers need more than a short critical section.
47/// It models the classic monitor object pattern: one mutex protects the state,
48/// and one condition variable lets threads wait until that state changes. This
49/// is the same relationship used by `std::sync::Mutex` and
50/// `std::sync::Condvar`, but represented as one object so the condition
51/// variable is not accidentally used with unrelated state.
52///
53/// `StdMonitor` deliberately has two levels of API:
54///
55/// * `read` and `write` acquire the mutex, run a closure, and release it.
56/// * `wait_while`, `wait_until`, and their timeout variants implement common
57/// predicate-based waits.
58/// * `lock` returns a [`StdMonitorGuard`] for callers that need to write their own
59/// loop around [`StdMonitorGuard::wait`] or [`StdMonitorGuard::wait_timeout`].
60///
61/// A poisoned mutex is recovered by taking the inner state. This makes
62/// `StdMonitor` suitable for coordination state that should remain observable
63/// after another thread panics while holding the lock.
64///
65/// # Difference from `Mutex` and `Condvar`
66///
67/// With the standard library primitives, callers usually store two fields and
68/// manually keep them paired:
69///
70/// ```rust
71/// # use std::sync::{Condvar, Mutex};
72/// # struct State;
73/// struct Shared {
74/// state: Mutex<State>,
75/// changed: Condvar,
76/// }
77/// ```
78///
79/// `StdMonitor<State>` stores the same pair internally. A [`StdMonitorGuard`] is a
80/// wrapper around the standard library's `MutexGuard`; it keeps the protected
81/// state locked and knows which monitor it belongs to, so its wait methods use
82/// the matching condition variable.
83///
84/// # Type Parameters
85///
86/// * `T` - The state protected by this monitor.
87///
88/// # Example
89///
90/// ```rust
91/// use std::thread;
92///
93/// use qubit_lock::ArcStdMonitor;
94///
95/// let monitor = ArcStdMonitor::new(false);
96/// let waiter_monitor = monitor.clone();
97///
98/// let waiter = thread::spawn(move || {
99/// waiter_monitor.wait_until(
100/// |ready| *ready,
101/// |ready| {
102/// *ready = false;
103/// },
104/// );
105/// });
106///
107/// monitor.write(|ready| {
108/// *ready = true;
109/// });
110/// monitor.notify_all();
111///
112/// waiter.join().expect("waiter should finish");
113/// assert!(!monitor.read(|ready| *ready));
114/// ```
115///
116pub struct StdMonitor<T> {
117 /// Mutex protecting the monitor state.
118 state: Mutex<T>,
119 /// Condition variable used to wake predicate waiters after state changes.
120 pub(super) changed: Condvar,
121}
122
123impl<T> StdMonitor<T> {
124 /// Creates a monitor protecting the supplied state value.
125 ///
126 /// # Arguments
127 ///
128 /// * `state` - Initial state protected by the monitor.
129 ///
130 /// # Returns
131 ///
132 /// A monitor initialized with the supplied state.
133 ///
134 /// # Example
135 ///
136 /// ```rust
137 /// use qubit_lock::StdMonitor;
138 ///
139 /// let monitor = StdMonitor::new(0_u32);
140 /// assert_eq!(monitor.read(|n| *n), 0);
141 /// ```
142 #[inline]
143 pub fn new(state: T) -> Self {
144 Self {
145 state: Mutex::new(state),
146 changed: Condvar::new(),
147 }
148 }
149
150 /// Acquires the monitor and returns a guard for explicit state-machine code.
151 ///
152 /// The returned [`StdMonitorGuard`] keeps the monitor mutex locked until the
153 /// guard is dropped. It can also be passed through
154 /// [`StdMonitorGuard::wait`] or [`StdMonitorGuard::wait_timeout`] to temporarily
155 /// release the lock while waiting on this monitor's condition variable.
156 ///
157 /// If the mutex is poisoned, this method recovers the inner state and still
158 /// returns a guard.
159 ///
160 /// # Returns
161 ///
162 /// A guard that provides read and write access to the protected state.
163 ///
164 /// # Example
165 ///
166 /// ```rust
167 /// use qubit_lock::StdMonitor;
168 ///
169 /// let monitor = StdMonitor::new(1);
170 /// {
171 /// let mut value = monitor.lock();
172 /// *value += 1;
173 /// }
174 ///
175 /// assert_eq!(monitor.read(|value| *value), 2);
176 /// ```
177 #[inline]
178 pub fn lock(&self) -> StdMonitorGuard<'_, T> {
179 StdMonitorGuard::new(
180 self,
181 self.state
182 .lock()
183 .unwrap_or_else(std::sync::PoisonError::into_inner),
184 )
185 }
186
187 /// Acquires the monitor and reads the protected state.
188 ///
189 /// The closure runs while the mutex is held. Keep the closure short and do
190 /// not call code that may block for a long time.
191 ///
192 /// If the mutex is poisoned, this method recovers the inner state and still
193 /// executes the closure.
194 ///
195 /// # Arguments
196 ///
197 /// * `f` - Closure that receives an immutable reference to the state.
198 ///
199 /// # Returns
200 ///
201 /// The value returned by the closure.
202 ///
203 /// # Example
204 ///
205 /// ```rust
206 /// use qubit_lock::StdMonitor;
207 ///
208 /// let monitor = StdMonitor::new(10_i32);
209 /// let n = monitor.read(|x| *x);
210 /// assert_eq!(n, 10);
211 /// ```
212 #[inline]
213 pub fn read<R, F>(&self, f: F) -> R
214 where
215 F: FnOnce(&T) -> R,
216 {
217 let guard = self.lock();
218 f(&*guard)
219 }
220
221 /// Acquires the monitor and mutates the protected state.
222 ///
223 /// The closure runs while the mutex is held. This method only changes the
224 /// state; callers should explicitly call [`Self::notify_one`] or
225 /// [`Self::notify_all`] after changing a condition that waiters may be
226 /// observing.
227 ///
228 /// If the mutex is poisoned, this method recovers the inner state and still
229 /// executes the closure.
230 ///
231 /// # Arguments
232 ///
233 /// * `f` - Closure that receives a mutable reference to the state.
234 ///
235 /// # Returns
236 ///
237 /// The value returned by the closure.
238 ///
239 /// # Example
240 ///
241 /// ```rust
242 /// use qubit_lock::StdMonitor;
243 ///
244 /// let monitor = StdMonitor::new(String::new());
245 /// let len = monitor.write(|s| {
246 /// s.push_str("hi");
247 /// s.len()
248 /// });
249 /// assert_eq!(len, 2);
250 /// ```
251 #[inline]
252 pub fn write<R, F>(&self, f: F) -> R
253 where
254 F: FnOnce(&mut T) -> R,
255 {
256 let mut guard = self.lock();
257 f(&mut *guard)
258 }
259
260 /// Acquires the monitor, mutates the protected state, and wakes one waiter.
261 ///
262 /// The closure runs while the mutex is held. After the closure returns, the
263 /// mutex guard is dropped and one thread waiting on this monitor's condition
264 /// variable is notified. This is a convenience method for the common
265 /// "update state, then notify one waiter" pattern.
266 ///
267 /// If the mutex is poisoned, this method recovers the inner state before
268 /// running the closure. If `f` panics, the panic is propagated and no
269 /// notification is sent.
270 ///
271 /// # Arguments
272 ///
273 /// * `f` - Closure that receives a mutable reference to the state.
274 ///
275 /// # Returns
276 ///
277 /// The value returned by the closure.
278 ///
279 /// # Example
280 ///
281 /// ```rust
282 /// use qubit_lock::StdMonitor;
283 ///
284 /// let monitor = StdMonitor::new(Vec::<i32>::new());
285 /// let len = monitor.write_notify_one(|items| {
286 /// items.push(7);
287 /// items.len()
288 /// });
289 ///
290 /// assert_eq!(len, 1);
291 /// ```
292 #[inline]
293 pub fn write_notify_one<R, F>(&self, f: F) -> R
294 where
295 F: FnOnce(&mut T) -> R,
296 {
297 let result = self.write(f);
298 self.notify_one();
299 result
300 }
301
302 /// Acquires the monitor, mutates the protected state, and wakes all waiters.
303 ///
304 /// The closure runs while the mutex is held. After the closure returns, the
305 /// mutex guard is dropped and all threads waiting on this monitor's condition
306 /// variable are notified. This is a convenience method for state changes that
307 /// may allow multiple waiters to make progress.
308 ///
309 /// If the mutex is poisoned, this method recovers the inner state before
310 /// running the closure. If `f` panics, the panic is propagated and no
311 /// notification is sent.
312 ///
313 /// # Arguments
314 ///
315 /// * `f` - Closure that receives a mutable reference to the state.
316 ///
317 /// # Returns
318 ///
319 /// The value returned by the closure.
320 ///
321 /// # Example
322 ///
323 /// ```rust
324 /// use qubit_lock::StdMonitor;
325 ///
326 /// let monitor = StdMonitor::new(false);
327 /// let ready = monitor.write_notify_all(|ready| {
328 /// *ready = true;
329 /// *ready
330 /// });
331 ///
332 /// assert!(ready);
333 /// ```
334 #[inline]
335 pub fn write_notify_all<R, F>(&self, f: F) -> R
336 where
337 F: FnOnce(&mut T) -> R,
338 {
339 let result = self.write(f);
340 self.notify_all();
341 result
342 }
343
344 /// Waits for a notification or timeout without checking state.
345 ///
346 /// This convenience method locks the monitor, waits once on the condition
347 /// variable, and returns why the timed wait completed. It is useful only
348 /// when the caller genuinely needs a notification wait without inspecting
349 /// state before or after the wait. Most coordination code should prefer
350 /// [`Self::wait_while`], [`Self::wait_until`], or the explicit
351 /// [`StdMonitorGuard::wait_timeout`] loop.
352 ///
353 /// Condition variables may wake spuriously, so
354 /// [`WaitTimeoutStatus::Woken`] does not prove that a notifier changed the
355 /// state.
356 ///
357 /// If the mutex is poisoned, this method recovers the inner state and
358 /// continues waiting.
359 ///
360 /// # Arguments
361 ///
362 /// * `timeout` - Maximum duration to wait for a notification.
363 ///
364 /// # Returns
365 ///
366 /// [`WaitTimeoutStatus::Woken`] if the wait returned before the timeout,
367 /// or [`WaitTimeoutStatus::TimedOut`] if the timeout elapsed.
368 ///
369 /// # Example
370 ///
371 /// ```rust
372 /// use std::time::Duration;
373 ///
374 /// use qubit_lock::{StdMonitor, WaitTimeoutStatus};
375 ///
376 /// let monitor = StdMonitor::new(false);
377 /// let status = monitor.wait_notify(Duration::from_millis(1));
378 ///
379 /// assert_eq!(status, WaitTimeoutStatus::TimedOut);
380 /// ```
381 #[inline]
382 pub fn wait_notify(&self, timeout: Duration) -> WaitTimeoutStatus {
383 let guard = self.lock();
384 let (_guard, status) = guard.wait_timeout(timeout);
385 status
386 }
387
388 /// Waits while a predicate remains true, then mutates the protected state.
389 ///
390 /// This is the monitor equivalent of the common `while condition { wait }`
391 /// guarded-suspension pattern. The predicate is evaluated while holding the
392 /// mutex. If it returns `true`, the current thread waits on the condition
393 /// variable and atomically releases the mutex. After a notification or
394 /// spurious wakeup, the mutex is reacquired and the predicate is evaluated
395 /// again. When the predicate returns `false`, `f` runs while the mutex is
396 /// still held.
397 ///
398 /// This method may block indefinitely if no thread changes the state so
399 /// that `waiting` becomes false and sends a notification.
400 ///
401 /// If the mutex is poisoned before or during the wait, this method recovers
402 /// the inner state and continues waiting or executes the closure.
403 ///
404 /// # Arguments
405 ///
406 /// * `waiting` - Predicate that returns `true` while the caller should
407 /// keep waiting.
408 /// * `f` - Closure that receives mutable access after waiting is no longer
409 /// required.
410 ///
411 /// # Returns
412 ///
413 /// The value returned by `f`.
414 ///
415 /// # Example
416 ///
417 /// ```rust
418 /// use std::{
419 /// sync::Arc,
420 /// thread,
421 /// };
422 ///
423 /// use qubit_lock::StdMonitor;
424 ///
425 /// let monitor = Arc::new(StdMonitor::new(Vec::<i32>::new()));
426 /// let worker_monitor = Arc::clone(&monitor);
427 ///
428 /// let worker = thread::spawn(move || {
429 /// worker_monitor.wait_while(
430 /// |items| items.is_empty(),
431 /// |items| items.pop().expect("item should be ready"),
432 /// )
433 /// });
434 ///
435 /// monitor.write(|items| items.push(7));
436 /// monitor.notify_one();
437 ///
438 /// assert_eq!(worker.join().expect("worker should finish"), 7);
439 /// ```
440 #[inline]
441 pub fn wait_while<R, P, F>(&self, mut waiting: P, f: F) -> R
442 where
443 P: FnMut(&T) -> bool,
444 F: FnOnce(&mut T) -> R,
445 {
446 let mut guard = self.lock();
447 while waiting(&*guard) {
448 guard = guard.wait();
449 }
450 f(&mut *guard)
451 }
452
453 /// Waits until the protected state satisfies a predicate, then mutates it.
454 ///
455 /// This is the positive-predicate counterpart of [`Self::wait_while`]. The
456 /// predicate is evaluated while holding the mutex. If it returns `false`,
457 /// the current thread waits on the condition variable and atomically
458 /// releases the mutex. After a notification or spurious wakeup, the mutex
459 /// is reacquired and the predicate is evaluated again. When the predicate
460 /// returns `true`, `f` runs while the mutex is still held.
461 ///
462 /// This method may block indefinitely if no thread changes the state to
463 /// satisfy the predicate and sends a notification.
464 ///
465 /// If the mutex is poisoned before or during the wait, this method recovers
466 /// the inner state and continues waiting or executes the closure.
467 ///
468 /// # Arguments
469 ///
470 /// * `ready` - Predicate that returns `true` when the state is ready.
471 /// * `f` - Closure that receives mutable access to the ready state.
472 ///
473 /// # Returns
474 ///
475 /// The value returned by `f` after the predicate has become true.
476 ///
477 /// # Example
478 ///
479 /// ```rust
480 /// use std::{
481 /// sync::Arc,
482 /// thread,
483 /// };
484 ///
485 /// use qubit_lock::StdMonitor;
486 ///
487 /// let monitor = Arc::new(StdMonitor::new(false));
488 /// let waiter_monitor = Arc::clone(&monitor);
489 ///
490 /// let waiter = thread::spawn(move || {
491 /// waiter_monitor.wait_until(
492 /// |ready| *ready,
493 /// |ready| {
494 /// *ready = false;
495 /// "done"
496 /// },
497 /// )
498 /// });
499 ///
500 /// monitor.write(|ready| *ready = true);
501 /// monitor.notify_one();
502 ///
503 /// assert_eq!(waiter.join().expect("waiter should finish"), "done");
504 /// ```
505 #[inline]
506 pub fn wait_until<R, P, F>(&self, mut ready: P, f: F) -> R
507 where
508 P: FnMut(&T) -> bool,
509 F: FnOnce(&mut T) -> R,
510 {
511 self.wait_while(|state| !ready(state), f)
512 }
513
514 /// Waits while a predicate remains true, with an overall time limit.
515 ///
516 /// This method is the timeout-aware form of [`Self::wait_while`]. It keeps
517 /// rechecking `waiting` under the monitor lock and waits only for the
518 /// remaining portion of `timeout`. If `waiting` becomes false before the
519 /// timeout expires, `f` runs while the lock is still held. If the timeout
520 /// expires first, the closure is not called.
521 ///
522 /// Condition variables may wake spuriously, and timeout status alone is not
523 /// used as proof that the predicate is still true; the predicate is always
524 /// rechecked under the lock.
525 ///
526 /// If the mutex is poisoned before or during the wait, this method recovers
527 /// the inner state and continues waiting or executes the closure.
528 ///
529 /// # Arguments
530 ///
531 /// * `timeout` - Maximum total duration to wait.
532 /// * `waiting` - Predicate that returns `true` while the caller should
533 /// continue waiting.
534 /// * `f` - Closure that receives mutable access when waiting is no longer
535 /// required.
536 ///
537 /// # Returns
538 ///
539 /// [`WaitTimeoutResult::Ready`] with the value returned by `f` when the
540 /// predicate stops blocking before the timeout. Returns
541 /// [`WaitTimeoutResult::TimedOut`] when the timeout expires first.
542 ///
543 /// # Example
544 ///
545 /// ```rust
546 /// use std::time::Duration;
547 ///
548 /// use qubit_lock::{StdMonitor, WaitTimeoutResult};
549 ///
550 /// let monitor = StdMonitor::new(Vec::<i32>::new());
551 /// let result = monitor.wait_timeout_while(
552 /// Duration::from_millis(1),
553 /// |items| items.is_empty(),
554 /// |items| items.pop(),
555 /// );
556 ///
557 /// assert_eq!(result, WaitTimeoutResult::TimedOut);
558 /// ```
559 #[inline]
560 pub fn wait_timeout_while<R, P, F>(
561 &self,
562 timeout: Duration,
563 mut waiting: P,
564 f: F,
565 ) -> WaitTimeoutResult<R>
566 where
567 P: FnMut(&T) -> bool,
568 F: FnOnce(&mut T) -> R,
569 {
570 let mut guard = self.lock();
571 let start = Instant::now();
572 loop {
573 if !waiting(&*guard) {
574 return WaitTimeoutResult::Ready(f(&mut *guard));
575 }
576
577 let elapsed = start.elapsed();
578 let remaining = timeout.checked_sub(elapsed).unwrap_or_default();
579 if remaining.is_zero() {
580 return WaitTimeoutResult::TimedOut;
581 }
582
583 let (next_guard, _status) = guard.wait_timeout(remaining);
584 guard = next_guard;
585 }
586 }
587
588 /// Waits until a predicate becomes true, with an overall time limit.
589 ///
590 /// This is the positive-predicate counterpart of
591 /// [`Self::wait_timeout_while`]. If `ready` becomes true before the timeout
592 /// expires, `f` runs while the monitor lock is still held. If the timeout
593 /// expires first, the closure is not called.
594 ///
595 /// Condition variables may wake spuriously, and timeout status alone is not
596 /// used as proof that the predicate is still false; the predicate is always
597 /// rechecked under the lock.
598 ///
599 /// If the mutex is poisoned before or during the wait, this method recovers
600 /// the inner state and continues waiting or executes the closure.
601 ///
602 /// # Arguments
603 ///
604 /// * `timeout` - Maximum total duration to wait.
605 /// * `ready` - Predicate that returns `true` when the caller may continue.
606 /// * `f` - Closure that receives mutable access to the ready state.
607 ///
608 /// # Returns
609 ///
610 /// [`WaitTimeoutResult::Ready`] with the value returned by `f` when the
611 /// predicate becomes true before the timeout. Returns
612 /// [`WaitTimeoutResult::TimedOut`] when the timeout expires first.
613 ///
614 /// # Example
615 ///
616 /// ```rust
617 /// use std::{
618 /// sync::Arc,
619 /// thread,
620 /// time::Duration,
621 /// };
622 ///
623 /// use qubit_lock::{StdMonitor, WaitTimeoutResult};
624 ///
625 /// let monitor = Arc::new(StdMonitor::new(false));
626 /// let waiter_monitor = Arc::clone(&monitor);
627 ///
628 /// let waiter = thread::spawn(move || {
629 /// waiter_monitor.wait_timeout_until(
630 /// Duration::from_secs(1),
631 /// |ready| *ready,
632 /// |ready| {
633 /// *ready = false;
634 /// 5
635 /// },
636 /// )
637 /// });
638 ///
639 /// monitor.write(|ready| *ready = true);
640 /// monitor.notify_one();
641 ///
642 /// assert_eq!(
643 /// waiter.join().expect("waiter should finish"),
644 /// WaitTimeoutResult::Ready(5),
645 /// );
646 /// ```
647 #[inline]
648 pub fn wait_timeout_until<R, P, F>(
649 &self,
650 timeout: Duration,
651 mut ready: P,
652 f: F,
653 ) -> WaitTimeoutResult<R>
654 where
655 P: FnMut(&T) -> bool,
656 F: FnOnce(&mut T) -> R,
657 {
658 self.wait_timeout_while(timeout, |state| !ready(state), f)
659 }
660
661 /// Wakes one thread waiting on this monitor's condition variable.
662 ///
663 /// Notifications do not carry state by themselves. A waiting thread only
664 /// proceeds safely after rechecking the protected state. Call this after
665 /// changing state that may make one waiter able to continue.
666 ///
667 /// # Example
668 ///
669 /// ```rust
670 /// use std::thread;
671 ///
672 /// use qubit_lock::ArcStdMonitor;
673 ///
674 /// let monitor = ArcStdMonitor::new(0_u32);
675 /// let waiter = {
676 /// let m = monitor.clone();
677 /// thread::spawn(move || {
678 /// m.wait_until(|n| *n > 0, |n| {
679 /// *n -= 1;
680 /// });
681 /// })
682 /// };
683 ///
684 /// monitor.write(|n| *n = 1);
685 /// monitor.notify_one();
686 /// waiter.join().expect("waiter should finish");
687 /// ```
688 #[inline]
689 pub fn notify_one(&self) {
690 self.changed.notify_one();
691 }
692
693 /// Wakes all threads waiting on this monitor's condition variable.
694 ///
695 /// Notifications do not carry state by themselves. Every awakened thread
696 /// must recheck the protected state before continuing. Call this after a
697 /// state change that may allow multiple waiters to make progress.
698 ///
699 /// # Example
700 ///
701 /// ```rust
702 /// use std::thread;
703 ///
704 /// use qubit_lock::ArcStdMonitor;
705 ///
706 /// let monitor = ArcStdMonitor::new(false);
707 /// let mut handles = Vec::new();
708 /// for _ in 0..2 {
709 /// let m = monitor.clone();
710 /// handles.push(thread::spawn(move || {
711 /// m.wait_until(|ready| *ready, |_| ());
712 /// }));
713 /// }
714 ///
715 /// monitor.write(|ready| *ready = true);
716 /// monitor.notify_all();
717 /// for h in handles {
718 /// h.join().expect("waiter should finish");
719 /// }
720 /// ```
721 #[inline]
722 pub fn notify_all(&self) {
723 self.changed.notify_all();
724 }
725}
726
727impl<T> From<T> for StdMonitor<T> {
728 /// Creates a standard monitor from an initial state value.
729 ///
730 /// # Arguments
731 ///
732 /// * `value` - Initial state protected by the monitor.
733 ///
734 /// # Returns
735 ///
736 /// A standard monitor initialized with `value`.
737 #[inline]
738 fn from(value: T) -> Self {
739 Self::new(value)
740 }
741}
742
743impl<T: Default> Default for StdMonitor<T> {
744 /// Creates a monitor containing `T::default()`.
745 ///
746 /// # Returns
747 ///
748 /// A monitor protecting the default value for `T`.
749 ///
750 /// # Example
751 ///
752 /// ```rust
753 /// use qubit_lock::StdMonitor;
754 ///
755 /// let monitor: StdMonitor<String> = StdMonitor::default();
756 /// assert!(monitor.read(|s| s.is_empty()));
757 /// ```
758 #[inline]
759 fn default() -> Self {
760 Self::new(T::default())
761 }
762}