Skip to main content

orx_concurrent_option/common_traits/
debug.rs

1use crate::{concurrent_option::ConcurrentOption, states::SOME};
2use core::fmt::Debug;
3
4impl<T: Debug> Debug for ConcurrentOption<T> {
5    /// Creates the debug representation.
6    ///
7    /// ```rust
8    /// use orx_concurrent_option::*;
9    /// use core::sync::atomic::Ordering;
10    ///
11    /// let x = ConcurrentOption::some(3.to_string());
12    /// let y = format!("{:?}", x); // debug with default Relaxed ordering
13    /// assert_eq!(y, "ConcurrentSome(\"3\")");
14    ///
15    /// let x = ConcurrentOption::<String>::none();
16    /// let y = format!("{:?}", x);
17    /// assert_eq!(y, "ConcurrentNone");
18    /// ```
19    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20        // hold the lock while formatting; `as_ref` alone would release it before the value is read
21        match self.spin_get_handle(SOME, SOME) {
22            Some(_handle) => {
23                let x = unsafe { (*self.value.get()).assume_init_ref() };
24                write!(f, "Concurrent{:?}", Some(x))
25            }
26            None => write!(f, "ConcurrentNone"),
27        }
28    }
29}