diff --git a/src/lib.rs b/src/lib.rs index 0b10ca6..6eb262e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,7 +171,29 @@ impl core::panic::RefUnwindSafe for Event {} impl fmt::Debug for Event { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("Event { .. }") + match self.try_inner() { + Some(inner) => { + let notified_count = inner.notified.load(Ordering::Relaxed); + let total_count = match inner.list.total_listeners() { + Ok(total_count) => total_count, + Err(_) => { + return f + .debug_tuple("Event") + .field(&format_args!("")) + .finish() + } + }; + + f.debug_struct("Event") + .field("listeners_notified", ¬ified_count) + .field("listeners_total", &total_count) + .finish() + } + None => f + .debug_tuple("Event") + .field(&format_args!("")) + .finish(), + } } } @@ -741,7 +763,9 @@ unsafe impl Sync for EventListener {} impl fmt::Debug for EventListener { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("EventListener { .. }") + f.debug_struct("EventListener") + .field("listening", &self.is_listening()) + .finish() } } diff --git a/src/no_std.rs b/src/no_std.rs index cbf0347..d37b1f2 100644 --- a/src/no_std.rs +++ b/src/no_std.rs @@ -240,6 +240,12 @@ impl List { queue: concurrent_queue::ConcurrentQueue::unbounded(), } } + pub fn total_listeners(&self) -> Result { + self.inner + .try_lock() + .map(|lock| Ok(lock.listeners.len())) + .unwrap_or(Err("")) + } } /// The guard returned by [`Inner::lock`]. diff --git a/src/std.rs b/src/std.rs index b4de42c..4b9c13e 100644 --- a/src/std.rs +++ b/src/std.rs @@ -44,6 +44,16 @@ impl List { notified: 0, })) } + // Accessor method because fields are private, not sure how to go around it + pub fn total_listeners(&self) -> Result { + match self.0.try_lock() { + Ok(mutex) => { + let len = mutex.len; + Ok(len) + } + Err(_) => Err(""), + } + } } impl crate::Inner {