Skip to content

Commit

Permalink
m: Update fmt::Debug to produce useful output
Browse files Browse the repository at this point in the history
cc #86, it's harder to get this info on no_std so I've ignored it for
now

Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Apr 14, 2024
1 parent da7b6f8 commit d6bd475
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,13 @@ impl<T> Drop for Event<T> {

impl fmt::Debug for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("Event { .. }")
match self.try_inner() {
None => f
.debug_tuple("Event")
.field(&format_args!("<uninitialized>"))
.finish(),
Some(inner) => inner.debug_fmt(f),
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/linked_list/lock_free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::notify::{GenericNotify, Internal, Notification};

use core::cell::{Cell, UnsafeCell};
use core::cmp::Reverse;
use core::fmt;
use core::hint::spin_loop;
use core::iter;
use core::marker::PhantomData;
Expand Down Expand Up @@ -95,6 +96,12 @@ impl<T> Inner<T> {
}
}

/// Debug output.
#[inline]
pub(crate) fn debug_fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Event").finish_non_exhaustive()
}

/// We never have enough info to tell this for sure.
pub(crate) fn notifyable(&self, _limit: usize) -> bool {
true
Expand Down
25 changes: 24 additions & 1 deletion src/linked_list/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::notify::{GenericNotify, Internal, Notification};

use std::boxed::Box;
use std::cell::{Cell, UnsafeCell};
use std::fmt;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::ptr::{self, NonNull};
use std::sync::{Mutex, MutexGuard};
use std::sync::{Mutex, MutexGuard, TryLockError};
use std::task::{Context, Poll, Waker};
use std::thread::{self, Thread};
use std::time::Instant;
Expand Down Expand Up @@ -49,6 +50,28 @@ impl<T> Inner<T> {
}
}

/// Debug output.
#[inline]
pub(crate) fn debug_fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let guard = match self.list.try_lock() {
Err(TryLockError::WouldBlock) => {
return f
.debug_tuple("Event")
.field(&format_args!("<locked>"))
.finish()
}

Err(TryLockError::Poisoned(err)) => err.into_inner(),

Ok(lock) => lock,
};

f.debug_struct("Event")
.field("listeners_notified", &guard.notified)
.field("listeners_total", &guard.len)
.finish()
}

/// Tell whether there is enough room to notify this list.
#[inline]
pub(crate) fn notifyable(&self, limit: usize) -> bool {
Expand Down

0 comments on commit d6bd475

Please sign in to comment.