From: Ian Jackson Date: Sun, 27 Mar 2022 12:32:24 +0000 (+0100) Subject: Mutex debug: Introduce DebugIdentify trait X-Git-Tag: otter-1.0.0~105 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=3c61a94dcfaa3764954caae9657f07c35ac03329;p=otter.git Mutex debug: Introduce DebugIdentify trait And implement it for many of our types. Signed-off-by: Ian Jackson --- diff --git a/src/bundles.rs b/src/bundles.rs index 6c2c5ea5..2dc4b715 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -312,6 +312,13 @@ impl Display for State { } } +impl DebugIdentify for InstanceBundles { + #[throws(fmt::Error)] + fn debug_identify_type(f: &mut fmt::Formatter) { + write!(f, "InstanceBundles")?; + } +} + #[ext(pub)] impl MgmtBundleList { #[throws(IE)] diff --git a/src/childio.rs b/src/childio.rs index 647a5e79..1a8e9513 100644 --- a/src/childio.rs +++ b/src/childio.rs @@ -30,6 +30,17 @@ impl Display for ChildWrapper { } } +impl DebugIdentify for ChildWrapper { + #[throws(fmt::Error)] + fn debug_identify(&self, f: &mut fmt::Formatter) { + write!(f, "ChildWrapper([{}] {})", self.child.id(), &self.desc)?; + } + #[throws(fmt::Error)] + fn debug_identify_type(f: &mut fmt::Formatter) { + write!(f, "ChildWrapper")?; + } +} + impl ChildIo { fn rw_result(&self, eofblock: bool, r: io::Result) -> io::Result diff --git a/src/debugmutex.rs b/src/debugmutex.rs index a9960fe8..46c532ab 100644 --- a/src/debugmutex.rs +++ b/src/debugmutex.rs @@ -5,9 +5,10 @@ use crate::prelude::*; pub struct Mutex(parking_lot::Mutex); #[derive(Debug, Display)] -pub struct MutexGuard<'g, T>(parking_lot::MutexGuard<'g, T>); +pub struct MutexGuard<'g, T>(parking_lot::MutexGuard<'g, T>) +where T: DebugIdentify; -impl Mutex { +impl Mutex where T: DebugIdentify { pub fn new(t: T) -> Self { Mutex(parking_lot::Mutex::new(t)) } @@ -16,10 +17,66 @@ impl Mutex { } } -impl<'g,T> Deref for MutexGuard<'g,T> { +impl<'g,T> Deref for MutexGuard<'g,T> where T: DebugIdentify { type Target = T; fn deref(&self) -> &T { &*self.0 } } -impl<'g,T> DerefMut for MutexGuard<'g,T> { +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); +impl Display for DisplayFormatter +where F: Fn(&mut fmt::Formatter) -> 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 Display for DisplayDebugIdentify<'_,T> where T: DebugIdentify { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.debug_identify(f) + } +} + +pub trait DebugIdentify { + fn debug_identify_type(f: &mut fmt::Formatter) -> fmt::Result; + fn debug_identify(&self, f: &mut fmt::Formatter) -> fmt::Result { + Self::debug_identify_type(f)?; + write!(f, "({:?})", self as *const _)?; + Ok(()) + } +} + +impl DebugIdentify for Option where T: DebugIdentify { + #[throws(fmt::Error)] + fn debug_identify_type(f: &mut fmt::Formatter) { + write!(f, "Option<{}>", DisplayFormatter(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))?, + Some(t) => t.debug_identify(f)?, + } + } +} + +impl DebugIdentify for VecDeque where T: DebugIdentify { + #[throws(fmt::Error)] + fn debug_identify_type(f: &mut fmt::Formatter) { + write!(f, "VecDeque<{}>", DisplayFormatter(T::debug_identify_type))?; + } +} + +impl DebugIdentify for File{ + #[throws(fmt::Error)] + fn debug_identify_type(f: &mut fmt::Formatter) { + write!(f, "File")?; + } + + #[throws(fmt::Error)] + fn debug_identify(&self, f: &mut fmt::Formatter) { + write!(f, "File({})", self.as_raw_fd())?; + } +} diff --git a/src/global.rs b/src/global.rs index 83ff49a0..7587ed92 100644 --- a/src/global.rs +++ b/src/global.rs @@ -566,6 +566,24 @@ impl Display for InstanceName { } hformat_as_display!{InstanceName} +impl DebugIdentify for InstanceContainer { + #[throws(fmt::Error)] + fn debug_identify(&self, f: &mut fmt::Formatter) { + write!(f, "InstanceContainer({})", &self.g.name)?; + } + #[throws(fmt::Error)] + fn debug_identify_type(f: &mut fmt::Formatter) { + write!(f, "InstanceContainer")?; + } +} + +impl DebugIdentify for InstanceRef { + #[throws(fmt::Error)] + fn debug_identify_type(f: &mut fmt::Formatter) { + write!(f, "InstanceRef")?; + } +} + fn link_a_href(k: &HtmlStr, v: &str) -> Html { hformat!("{}", v, k) } diff --git a/src/prelude.rs b/src/prelude.rs index 882381c1..2dfb5cd9 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -147,6 +147,7 @@ pub use crate::commands::{MgmtGameInstruction, MgmtGameResponse}; pub use crate::commands::{MgmtBundleList, MgmtGameUpdateMode}; pub use crate::commands::{ProgressUpdateMode}; pub use crate::config::*; +pub use crate::debugmutex::DebugIdentify; pub use crate::debugreader::DebugReader; pub use crate::error::*; pub use crate::fake_rng::*;