chiark / gitweb /
Mutex debug: Use Debug for DebugIdentify, not Display
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Mar 2022 12:54:01 +0000 (13:54 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Mar 2022 23:50:50 +0000 (00:50 +0100)
This will make the types useable with dbg!.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/debugmutex.rs

index 46c532ab7cdd2d537f4898efa4c5b9e34ed6c66d..8caa704c6b0b1545ce957886143913d9b59eb228 100644 (file)
@@ -25,14 +25,14 @@ impl<'g,T> DerefMut for MutexGuard<'g,T> where T: DebugIdentify {
   fn deref_mut(&mut self) -> &mut T { &mut *self.0 }
 }
 
-pub struct DisplayFormatter<F>(F);
-impl<F> Display for DisplayFormatter<F>
-where F: Fn(&mut fmt::Formatter) -> fmt::Result {
+pub struct DebugFormatter<C>(C);
+impl<C> Debug for DebugFormatter<C>
+where C: for<'f,'ff> Fn(&'f mut fmt::Formatter<'ff>) -> fmt::Result {
   fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0(f) }
 }
 
-pub struct DisplayDebugIdentify<'t,T>(&'t T) where T: DebugIdentify;
-impl<T> Display for DisplayDebugIdentify<'_,T> where T: DebugIdentify {
+pub struct DebugDebugIdentify<'t,T>(&'t T) where T: DebugIdentify;
+impl<T> Debug for DebugDebugIdentify<'_,T> where T: DebugIdentify {
   fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     self.0.debug_identify(f)
   }
@@ -50,13 +50,13 @@ pub trait DebugIdentify {
 impl<T> DebugIdentify for Option<T> where T: DebugIdentify {
   #[throws(fmt::Error)]
   fn debug_identify_type(f: &mut fmt::Formatter) {
-    write!(f, "Option<{}>", DisplayFormatter(T::debug_identify_type))?;
+    write!(f, "Option<{:?}>", DebugFormatter(T::debug_identify_type))?;
   }
 
   #[throws(fmt::Error)]
   fn debug_identify(&self, f: &mut fmt::Formatter) {
     match self {
-      None => write!(f, "None<{}>", DisplayFormatter(T::debug_identify_type))?,
+      None => write!(f, "None<{:?}>", DebugFormatter(T::debug_identify_type))?,
       Some(t) => t.debug_identify(f)?,
     }
   }
@@ -65,7 +65,7 @@ impl<T> DebugIdentify for Option<T> where T: DebugIdentify {
 impl<T> DebugIdentify for VecDeque<T> where T: DebugIdentify {
   #[throws(fmt::Error)]
   fn debug_identify_type(f: &mut fmt::Formatter) {
-    write!(f, "VecDeque<{}>", DisplayFormatter(T::debug_identify_type))?;
+    write!(f, "VecDeque<{:?}>", DebugFormatter(T::debug_identify_type))?;
   }
 }