summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMica White <botahamec@outlook.com>2024-03-10 21:27:01 -0400
committerMica White <botahamec@outlook.com>2024-03-10 21:27:01 -0400
commit5eaa4fe1d3bfcda696122ba3d6b4914dba19ef96 (patch)
tree611014ce92c7a52873019b6d52d2468f6083a9ab /src
parent815c0adedd6207eb406c67ea09c2634f304f8adf (diff)
implement Debug
Diffstat (limited to 'src')
-rw-r--r--src/mutex/mutex.rs26
-rw-r--r--src/rwlock/rwlock.rs26
2 files changed, 48 insertions, 4 deletions
diff --git a/src/mutex/mutex.rs b/src/mutex/mutex.rs
index ce93cae..52a4848 100644
--- a/src/mutex/mutex.rs
+++ b/src/mutex/mutex.rs
@@ -26,9 +26,31 @@ impl<T, R: RawMutex> Mutex<T, R> {
}
}
-impl<T: ?Sized, R> Debug for Mutex<T, R> {
+impl<T: ?Sized + Default, R: RawMutex> Default for Mutex<T, R> {
+ fn default() -> Self {
+ Self::new(T::default())
+ }
+}
+
+impl<T: ?Sized + Debug, R: RawMutex> Debug for Mutex<T, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.write_str(&format!("Mutex<{}>", std::any::type_name::<T>()))
+ // safety: this is just a try lock, and the value is dropped
+ // immediately after, so there's no risk of blocking ourselves
+ // or any other threads
+ if let Some(value) = unsafe { self.try_lock_no_key() } {
+ f.debug_struct("Mutex").field("data", &&*value).finish()
+ } else {
+ struct LockedPlaceholder;
+ impl Debug for LockedPlaceholder {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("<locked>")
+ }
+ }
+
+ f.debug_struct("Mutex")
+ .field("data", &LockedPlaceholder)
+ .finish()
+ }
}
}
diff --git a/src/rwlock/rwlock.rs b/src/rwlock/rwlock.rs
index 946f67e..c1d1792 100644
--- a/src/rwlock/rwlock.rs
+++ b/src/rwlock/rwlock.rs
@@ -17,9 +17,31 @@ impl<T, R: RawRwLock> RwLock<T, R> {
}
}
-impl<T: ?Sized, R> Debug for RwLock<T, R> {
+impl<T: ?Sized + Default, R: RawRwLock> Default for RwLock<T, R> {
+ fn default() -> Self {
+ Self::new(T::default())
+ }
+}
+
+impl<T: ?Sized + Debug, R: RawRwLock> Debug for RwLock<T, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.write_str(&format!("RwLock<{}>", std::any::type_name::<T>()))
+ // safety: this is just a try lock, and the value is dropped
+ // immediately after, so there's no risk of blocking ourselves
+ // or any other threads
+ if let Some(value) = unsafe { self.try_read_no_key() } {
+ f.debug_struct("RwLock").field("data", &&*value).finish()
+ } else {
+ struct LockedPlaceholder;
+ impl Debug for LockedPlaceholder {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str("<locked>")
+ }
+ }
+
+ f.debug_struct("RwLock")
+ .field("data", &LockedPlaceholder)
+ .finish()
+ }
}
}