Skip to content

Commit

Permalink
m: Update fmt::Debug to produce new info
Browse files Browse the repository at this point in the history
Co-authored-by: John Nunley <dev@notgull.net>
  • Loading branch information
mamaicode and notgull committed Oct 22, 2023
1 parent e8a1727 commit 74e8231
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,29 @@ impl<T> core::panic::RefUnwindSafe for Event<T> {}

impl<T> fmt::Debug for Event<T> {
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!("<locked>"))
.finish()
}
};

f.debug_struct("Event")
.field("listeners_notified", &notified_count)
.field("listeners_total", &total_count)
.finish()
}
None => f
.debug_tuple("Event")
.field(&format_args!("<uninitialized>"))
.finish(),
}
}
}

Expand Down Expand Up @@ -741,7 +763,9 @@ unsafe impl<T: Send> Sync for EventListener<T> {}

impl<T> fmt::Debug for EventListener<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("EventListener { .. }")
f.debug_struct("EventListener")
.field("listening", &self.is_listening())
.finish()
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ impl<T> List<T> {
queue: concurrent_queue::ConcurrentQueue::unbounded(),
}
}
pub fn total_listeners(&self) -> Result<usize, &str> {
self.inner
.try_lock()
.map(|lock| Ok(lock.listeners.len()))
.unwrap_or(Err("<locked>"))
}
}

/// The guard returned by [`Inner::lock`].
Expand Down
10 changes: 10 additions & 0 deletions src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ impl<T> List<T> {
notified: 0,
}))
}
// Accessor method because fields are private, not sure how to go around it
pub fn total_listeners(&self) -> Result<usize, &str> {
match self.0.try_lock() {
Ok(mutex) => {
let len = mutex.len;
Ok(len)
}
Err(_) => Err("<locked>"),
}
}
}

impl<T> crate::Inner<T> {
Expand Down

0 comments on commit 74e8231

Please sign in to comment.