Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fmt::Debug to produce new info #86

Merged
merged 27 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,21 @@ 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 = inner.list.total_listeners();
mamaicode marked this conversation as resolved.
Show resolved Hide resolved

f.debug_struct("Event")
.field("listeners_notified", &notified_count)
.field("listeners_total", &total_count)
.finish()
}
None => f
.debug_tuple("event")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize "Event"

.field(&format_args!("<uninitialized>"))
.finish(),
}
}
}

Expand Down Expand Up @@ -650,7 +664,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
Loading