chore: Fix up some minor bits before release

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley 2024-01-27 12:21:00 -08:00 committed by GitHub
parent a68f5ee1b0
commit ac18bdf617
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 29 deletions

View File

@ -174,9 +174,9 @@ impl<T> fmt::Debug for Event<T> {
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(_) => {
let total_count = match inner.list.try_total_listeners() {
Some(total_count) => total_count,
None => {
return f
.debug_tuple("Event")
.field(&format_args!("<locked>"))
@ -480,12 +480,20 @@ impl<T> Event<T> {
inner
}
/// Return the listener count by acquiring a lock.
/// Get the number of listeners currently listening to this [`Event`].
///
/// This is just a snapshot of the number of listeners at this point in time.
/// It is possible for the actual number to change at any point.
/// The number should only ever be used as a hint.
/// This is only available when `std` feature is enabled.
/// This call returns the number of [`EventListener`]s that are currently listening to
/// this event. It does this by acquiring the internal event lock and reading the listener
/// count. Therefore it is only available for `std`-enabled platforms.
///
/// # Caveats
///
/// This function returns just a snapshot of the number of listeners at this point in time.
/// Due to the nature of multi-threaded CPUs, it is possible that this number will be
/// inaccurate by the time that this function returns.
///
/// It is possible for the actual number to change at any point. Therefore, the number should
/// only ever be used as a hint.
///
/// # Examples
///
@ -510,7 +518,7 @@ impl<T> Event<T> {
#[inline]
pub fn total_listeners(&self) -> usize {
if let Some(inner) = self.try_inner() {
inner.list.total_listeners_wait()
inner.list.total_listeners()
} else {
0
}

View File

@ -240,11 +240,10 @@ 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>"))
/// Try to get the total number of listeners without blocking.
pub(super) fn try_total_listeners(&self) -> Option<usize> {
self.inner.try_lock().map(|lock| lock.listeners.len())
}
}

View File

@ -44,23 +44,15 @@ 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>"),
}
/// Get the total number of listeners without blocking.
pub(crate) fn try_total_listeners(&self) -> Option<usize> {
self.0.try_lock().ok().map(|list| list.len)
}
// Get the listener count by blocking.
pub(crate) fn total_listeners_wait(&self) -> usize {
match self.0.lock() {
Ok(mutex) => mutex.len,
Err(err) => panic!("{err}"),
}
/// Get the total number of listeners with blocking.
pub(crate) fn total_listeners(&self) -> usize {
self.0.lock().unwrap_or_else(|e| e.into_inner()).len
}
}