From 71072faf0595a8850639c526b11187e94be00c1f Mon Sep 17 00:00:00 2001 From: scirin Date: Tue, 10 Oct 2023 20:16:16 +0400 Subject: [PATCH 01/26] [DRAFT] --- src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f21c2a5..debcf97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,6 +161,7 @@ pub struct Event { /// is an `Arc` so it's important to keep in mind that it contributes to the [`Arc`]'s /// reference count. inner: AtomicPtr>, + listener_count: AtomicUsize, } unsafe impl Send for Event {} @@ -169,9 +170,37 @@ unsafe impl Sync for Event {} impl core::panic::UnwindSafe for Event {} impl core::panic::RefUnwindSafe for Event {} +struct EventDebugInfo { + notified_listeners: usize, + total_listeners: usize, +} + +impl fmt::Debug for EventDebugInfo { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Event {{ Notified Listeners: {}, Total Listeners: {} }}", + self.notified_listeners, + self.total_listeners + ) + } +} + impl fmt::Debug for Event { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("Event { .. }") + if let Some(inner) = self.try_inner() { + let notified_listeners = inner.notified.load(Ordering::Acquire); + let total_listeners = self.listener_count.load(Ordering::Relaxed); // Access listener_count directly + + let debug_info = EventDebugInfo { + notified_listeners, + total_listeners, + }; + + debug_info.fmt(f) + } else { + f.write_str("Event { Not Initialized }") + } } } @@ -200,9 +229,17 @@ impl Event { pub const fn with_tag() -> Self { Self { inner: AtomicPtr::new(ptr::null_mut()), + listener_count: AtomicUsize::new(0), } } + pub fn add_listener(&self) { + self.listener_count.fetch_add(1, Ordering::Relaxed); + } + + pub fn remove_listener(&self) { + self.listener_count.fetch_sub(1, Ordering::Relaxed); + } /// Tell whether any listeners are currently notified. /// /// # Examples @@ -454,9 +491,10 @@ impl Event<()> { pub const fn new() -> Self { Self { inner: AtomicPtr::new(ptr::null_mut()), + listener_count: AtomicUsize::new(0), } } - + /// Notifies a number of active listeners without emitting a `SeqCst` fence. /// /// The number is allowed to be zero or exceed the current number of listeners. From 6ca2488f1c9dd4a3a4f832eb37ec03238bcc3beb Mon Sep 17 00:00:00 2001 From: scirin Date: Tue, 10 Oct 2023 20:23:24 +0400 Subject: [PATCH 02/26] [FEATURE] some comments --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index debcf97..53cad8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -234,11 +234,11 @@ impl Event { } pub fn add_listener(&self) { - self.listener_count.fetch_add(1, Ordering::Relaxed); + self.listener_count.fetch_add(1, Ordering::Relaxed); // incerment } pub fn remove_listener(&self) { - self.listener_count.fetch_sub(1, Ordering::Relaxed); + self.listener_count.fetch_sub(1, Ordering::Relaxed); // decrement } /// Tell whether any listeners are currently notified. /// From 0bba9c80f2fb8158a5526a2b62e6850f93d8f92a Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 17:43:25 +0400 Subject: [PATCH 03/26] [FIX] replace separate debug struct --- src/lib.rs | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 53cad8c..cad57b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,37 +170,21 @@ unsafe impl Sync for Event {} impl core::panic::UnwindSafe for Event {} impl core::panic::RefUnwindSafe for Event {} -struct EventDebugInfo { - notified_listeners: usize, - total_listeners: usize, -} - -impl fmt::Debug for EventDebugInfo { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "Event {{ Notified Listeners: {}, Total Listeners: {} }}", - self.notified_listeners, - self.total_listeners - ) - } -} - impl fmt::Debug for Event { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut ds = f.debug_struct("Event"); + if let Some(inner) = self.try_inner() { let notified_listeners = inner.notified.load(Ordering::Acquire); - let total_listeners = self.listener_count.load(Ordering::Relaxed); // Access listener_count directly + let total_listeners = self.listener_count.load(Ordering::Relaxed); - let debug_info = EventDebugInfo { - notified_listeners, - total_listeners, - }; - - debug_info.fmt(f) + ds.field("Notified Listeners", ¬ified_listeners); + ds.field("Total Listeners count", &total_listeners); } else { - f.write_str("Event { Not Initialized }") + ds.field("Status", &"Not Initialized"); } + + ds.finish() } } @@ -234,11 +218,11 @@ impl Event { } pub fn add_listener(&self) { - self.listener_count.fetch_add(1, Ordering::Relaxed); // incerment + self.listener_count.fetch_add(1, Ordering::Relaxed); } pub fn remove_listener(&self) { - self.listener_count.fetch_sub(1, Ordering::Relaxed); // decrement + self.listener_count.fetch_sub(1, Ordering::Relaxed); } /// Tell whether any listeners are currently notified. /// From 9272ee4bef2f6ce6890138b8927572e98111f8df Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 17:52:47 +0400 Subject: [PATCH 04/26] [FEATURE] EventListener Update --- src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cad57b0..a96570b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -672,7 +672,15 @@ unsafe impl Sync for EventListener {} impl fmt::Debug for EventListener { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("EventListener { .. }") + let mut ds = f.debug_struct("EventListener"); + + if self.is_listening() { + ds.field("Listening", &"Yes"); + } else { + ds.field("Listening", &"No"); + } + + ds.finish() } } From e31a7a0a48123a97c55288b47e2d67a12fcf628c Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 20:07:30 +0400 Subject: [PATCH 05/26] [FIX] --- src/lib.rs | 38 ++++++++++++-------------------------- src/std.rs | 4 ++++ 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a96570b..1f16dbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,14 +116,14 @@ struct Inner { /// The number of notified entries, or `usize::MAX` if all of them have been notified. /// /// If there are no entries, this value is set to `usize::MAX`. - notified: AtomicUsize, + pub notified: AtomicUsize, /// Inner queue of event listeners. /// /// On `std` platforms, this is an intrusive linked list. On `no_std` platforms, this is a /// more traditional `Vec` of listeners, with an atomic queue used as a backup for high /// contention. - list: sys::List, + pub list: sys::List, } impl Inner { @@ -161,7 +161,6 @@ pub struct Event { /// is an `Arc` so it's important to keep in mind that it contributes to the [`Arc`]'s /// reference count. inner: AtomicPtr>, - listener_count: AtomicUsize, } unsafe impl Send for Event {} @@ -172,19 +171,16 @@ impl core::panic::RefUnwindSafe for Event {} impl fmt::Debug for Event { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut ds = f.debug_struct("Event"); - - if let Some(inner) = self.try_inner() { - let notified_listeners = inner.notified.load(Ordering::Acquire); - let total_listeners = self.listener_count.load(Ordering::Relaxed); - - ds.field("Notified Listeners", ¬ified_listeners); - ds.field("Total Listeners count", &total_listeners); - } else { - ds.field("Status", &"Not Initialized"); - } - - ds.finish() + let inner = self.inner(); + let notified_count = unsafe { (*inner).notified.load(Ordering::Relaxed) }; + let total_count = unsafe { (*inner).list.total_listeners() }; + + write!( + f, + "Event {{\n Number of notified listeners: {}\n Total number of listeners: {}\n}}", + notified_count, + total_count + ) } } @@ -213,17 +209,8 @@ impl Event { pub const fn with_tag() -> Self { Self { inner: AtomicPtr::new(ptr::null_mut()), - listener_count: AtomicUsize::new(0), } } - - pub fn add_listener(&self) { - self.listener_count.fetch_add(1, Ordering::Relaxed); - } - - pub fn remove_listener(&self) { - self.listener_count.fetch_sub(1, Ordering::Relaxed); - } /// Tell whether any listeners are currently notified. /// /// # Examples @@ -475,7 +462,6 @@ impl Event<()> { pub const fn new() -> Self { Self { inner: AtomicPtr::new(ptr::null_mut()), - listener_count: AtomicUsize::new(0), } } diff --git a/src/std.rs b/src/std.rs index b4de42c..a5f36ba 100644 --- a/src/std.rs +++ b/src/std.rs @@ -44,6 +44,10 @@ impl List { notified: 0, })) } + // Accessor methods because fields are private, not sure how to go around it + pub fn total_listeners(&self) -> usize { + self.0.lock().unwrap().len + } } impl crate::Inner { From 5075f466b4d4e25c6e8c16893e277db07fce117e Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 20:45:59 +0400 Subject: [PATCH 06/26] Retrigger CI --- src/std.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/std.rs b/src/std.rs index a5f36ba..0a64729 100644 --- a/src/std.rs +++ b/src/std.rs @@ -44,7 +44,7 @@ impl List { notified: 0, })) } - // Accessor methods because fields are private, not sure how to go around it + // Accessor method because fields are private, not sure how to go around it pub fn total_listeners(&self) -> usize { self.0.lock().unwrap().len } From 9ed2090a97bd8e0f87acc3754e26038d56b74d10 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 20:53:55 +0400 Subject: [PATCH 07/26] [FIX] List struct --- src/lib.rs | 3 +-- src/no_std.rs | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1f16dbb..ea06acc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -178,8 +178,7 @@ impl fmt::Debug for Event { write!( f, "Event {{\n Number of notified listeners: {}\n Total number of listeners: {}\n}}", - notified_count, - total_count + notified_count,total_count ) } } diff --git a/src/no_std.rs b/src/no_std.rs index 5e2f03b..e1e2dfb 100644 --- a/src/no_std.rs +++ b/src/no_std.rs @@ -240,6 +240,9 @@ impl List { queue: concurrent_queue::ConcurrentQueue::unbounded(), } } + pub fn total_listeners(&self) -> usize { + self.inner.lock().len() + } } /// The guard returned by [`Inner::lock`]. From 82f1d8d7e59eb84e6f4a78cb0df664fc9393b040 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 21:05:24 +0400 Subject: [PATCH 08/26] [FIX] fmt --- src/lib.rs | 2 +- src/no_std.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ea06acc..13d97da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -178,7 +178,7 @@ impl fmt::Debug for Event { write!( f, "Event {{\n Number of notified listeners: {}\n Total number of listeners: {}\n}}", - notified_count,total_count + notified_count, total_count ) } } diff --git a/src/no_std.rs b/src/no_std.rs index e1e2dfb..d63c50e 100644 --- a/src/no_std.rs +++ b/src/no_std.rs @@ -241,7 +241,7 @@ impl List { } } pub fn total_listeners(&self) -> usize { - self.inner.lock().len() + self.inner.try_lock().len() } } From 4a110cd2617c8031fe2b371a31cdfa58538be339 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 21:18:08 +0400 Subject: [PATCH 09/26] Retrigger CI --- src/lib.rs | 1 - src/no_std.rs | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 13d97da..874a209 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -463,7 +463,6 @@ impl Event<()> { inner: AtomicPtr::new(ptr::null_mut()), } } - /// Notifies a number of active listeners without emitting a `SeqCst` fence. /// /// The number is allowed to be zero or exceed the current number of listeners. diff --git a/src/no_std.rs b/src/no_std.rs index d63c50e..80d9244 100644 --- a/src/no_std.rs +++ b/src/no_std.rs @@ -241,7 +241,12 @@ impl List { } } pub fn total_listeners(&self) -> usize { - self.inner.try_lock().len() + self.inner + .try_lock() + .as_ref() + .map(|lock| &**lock) + .map(|listener_slab| listener_slab.listeners.len()) + .unwrap_or(0) } } From af4bbd46970e03ded71d606411a1b85ddb6c799d Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 21:22:44 +0400 Subject: [PATCH 10/26] [FIX] fix fmt --- src/no_std.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/no_std.rs b/src/no_std.rs index 80d9244..8a265c2 100644 --- a/src/no_std.rs +++ b/src/no_std.rs @@ -242,11 +242,11 @@ impl List { } pub fn total_listeners(&self) -> usize { self.inner - .try_lock() - .as_ref() - .map(|lock| &**lock) - .map(|listener_slab| listener_slab.listeners.len()) - .unwrap_or(0) + .try_lock() + .as_ref() + .map(|lock| &**lock) + .map(|listener_slab| listener_slab.listeners.len()) + .unwrap_or(0) } } From 9ef1d943760aeb3134db13eb4e093c5a506aa137 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 22:21:42 +0400 Subject: [PATCH 11/26] [FEATURE] return Result for total_listeners --- src/lib.rs | 2 +- src/std.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 874a209..dda65c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,7 +177,7 @@ impl fmt::Debug for Event { write!( f, - "Event {{\n Number of notified listeners: {}\n Total number of listeners: {}\n}}", + "Event {{\n Number of notified listeners: {:?}\n Total number of listeners: {:?}\n}}", notified_count, total_count ) } diff --git a/src/std.rs b/src/std.rs index 0a64729..a56eece 100644 --- a/src/std.rs +++ b/src/std.rs @@ -45,8 +45,15 @@ impl List { })) } // Accessor method because fields are private, not sure how to go around it - pub fn total_listeners(&self) -> usize { - self.0.lock().unwrap().len + pub fn total_listeners(&self) -> Result { + let guard_result = self.0.lock(); + match guard_result { + Ok(guard) => Ok(guard.len), + Err(_) => { + // If mutex is locked return value + Err("") + } + } } } From 416524c28c895c378b76a6920efa70c33e5f98a7 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 22:27:53 +0400 Subject: [PATCH 12/26] [FEATURE] use .map for total_listeners --- src/std.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/std.rs b/src/std.rs index a56eece..8076fac 100644 --- a/src/std.rs +++ b/src/std.rs @@ -46,14 +46,7 @@ impl List { } // Accessor method because fields are private, not sure how to go around it pub fn total_listeners(&self) -> Result { - let guard_result = self.0.lock(); - match guard_result { - Ok(guard) => Ok(guard.len), - Err(_) => { - // If mutex is locked return value - Err("") - } - } + self.0.lock().map(|guard| guard.len).map_err(|_| "") } } From 4a87b356d397b2318d1b5c01fdad49c2bf0f45a7 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 12 Oct 2023 23:17:08 +0400 Subject: [PATCH 13/26] [FIX] fix .map --- src/std.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/std.rs b/src/std.rs index 8076fac..a6bdb6c 100644 --- a/src/std.rs +++ b/src/std.rs @@ -46,7 +46,7 @@ impl List { } // Accessor method because fields are private, not sure how to go around it pub fn total_listeners(&self) -> Result { - self.0.lock().map(|guard| guard.len).map_err(|_| "") + self.0.lock().map(|mutex| mutex.len).map_err(|_| "") } } From 84c7a628bd1b1c475884863840b4e6cb51e2cd35 Mon Sep 17 00:00:00 2001 From: scirin Date: Fri, 13 Oct 2023 03:42:18 +0400 Subject: [PATCH 14/26] [FIX] pub field fix --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dda65c8..74d92b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,14 +116,14 @@ struct Inner { /// The number of notified entries, or `usize::MAX` if all of them have been notified. /// /// If there are no entries, this value is set to `usize::MAX`. - pub notified: AtomicUsize, + notified: AtomicUsize, /// Inner queue of event listeners. /// /// On `std` platforms, this is an intrusive linked list. On `no_std` platforms, this is a /// more traditional `Vec` of listeners, with an atomic queue used as a backup for high /// contention. - pub list: sys::List, + list: sys::List, } impl Inner { From e9a8dae623ff43afae72b6ad37bdd3d84715a18a Mon Sep 17 00:00:00 2001 From: scirin Date: Fri, 13 Oct 2023 04:39:48 +0400 Subject: [PATCH 15/26] [FIX] --- src/lib.rs | 37 ++++++++++++++++++------------------- src/no_std.rs | 8 +++----- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 74d92b9..fe5f353 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,16 +171,21 @@ impl core::panic::RefUnwindSafe for Event {} impl fmt::Debug for Event { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let inner = self.inner(); - let notified_count = unsafe { (*inner).notified.load(Ordering::Relaxed) }; - let total_count = unsafe { (*inner).list.total_listeners() }; - - write!( - f, - "Event {{\n Number of notified listeners: {:?}\n Total number of listeners: {:?}\n}}", - notified_count, total_count - ) - } + match self.try_inner() { + Some(inner) => { + let notified_count = inner.notified.load(Ordering::Relaxed); + let total_count = inner.list.total_listeners(); + + f.debug_struct("Event") + .field("listeners_notified", ¬ified_count) + .field("listeners_total", &total_count) + .finish() + } + None => { + f.debug_tuple("event").field(&"").finish() + } + } + } } impl Default for Event { @@ -656,15 +661,9 @@ unsafe impl Sync for EventListener {} impl fmt::Debug for EventListener { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut ds = f.debug_struct("EventListener"); - - if self.is_listening() { - ds.field("Listening", &"Yes"); - } else { - ds.field("Listening", &"No"); - } - - ds.finish() + f.debug_struct("EventListener") + .field("listening", &self.is_listening()) + .finish() } } diff --git a/src/no_std.rs b/src/no_std.rs index a26c40a..d37b1f2 100644 --- a/src/no_std.rs +++ b/src/no_std.rs @@ -240,13 +240,11 @@ impl List { queue: concurrent_queue::ConcurrentQueue::unbounded(), } } - pub fn total_listeners(&self) -> usize { + pub fn total_listeners(&self) -> Result { self.inner .try_lock() - .as_ref() - .map(|lock| &**lock) - .map(|listener_slab| listener_slab.listeners.len()) - .unwrap_or(0) + .map(|lock| Ok(lock.listeners.len())) + .unwrap_or(Err("")) } } From 5c8647a2d63b320cf580940793fbadd61a5ae1c8 Mon Sep 17 00:00:00 2001 From: scirin Date: Fri, 13 Oct 2023 04:44:42 +0400 Subject: [PATCH 16/26] [FIX] fmt --- src/lib.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fe5f353..9dbcf8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,21 +171,19 @@ impl core::panic::RefUnwindSafe for Event {} impl fmt::Debug for Event { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.try_inner() { - Some(inner) => { - let notified_count = inner.notified.load(Ordering::Relaxed); - let total_count = inner.list.total_listeners(); - - f.debug_struct("Event") - .field("listeners_notified", ¬ified_count) - .field("listeners_total", &total_count) - .finish() - } - None => { - f.debug_tuple("event").field(&"").finish() + match self.try_inner() { + Some(inner) => { + let notified_count = inner.notified.load(Ordering::Relaxed); + let total_count = inner.list.total_listeners(); + + f.debug_struct("Event") + .field("listeners_notified", ¬ified_count) + .field("listeners_total", &total_count) + .finish() + } + None => f.debug_tuple("event").field(&"").finish(), } - } - } + } } impl Default for Event { @@ -662,8 +660,8 @@ unsafe impl Sync for EventListener {} impl fmt::Debug for EventListener { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("EventListener") - .field("listening", &self.is_listening()) - .finish() + .field("listening", &self.is_listening()) + .finish() } } From ef10c5acece481347e244d1f7d2794619ff7a306 Mon Sep 17 00:00:00 2001 From: Irine <102310764+scirin@users.noreply.github.com> Date: Thu, 19 Oct 2023 17:30:30 +0400 Subject: [PATCH 17/26] Update src/lib.rs Co-authored-by: John Nunley --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9dbcf8a..3e4eb0a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -181,7 +181,7 @@ impl fmt::Debug for Event { .field("listeners_total", &total_count) .finish() } - None => f.debug_tuple("event").field(&"").finish(), + None => f.debug_tuple("event").field(&format_args!("")).finish(), } } } From 809429880d69e1babcc114df3fbbac60f338f7ac Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 19 Oct 2023 17:31:13 +0400 Subject: [PATCH 18/26] add line --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 3e4eb0a..1a2728e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -213,6 +213,7 @@ impl Event { inner: AtomicPtr::new(ptr::null_mut()), } } + /// Tell whether any listeners are currently notified. /// /// # Examples From eb152c578526b5403f5270f8344077e8550fd21e Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 19 Oct 2023 17:38:34 +0400 Subject: [PATCH 19/26] fmt fix --- src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a2728e..191d040 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -181,7 +181,10 @@ impl fmt::Debug for Event { .field("listeners_total", &total_count) .finish() } - None => f.debug_tuple("event").field(&format_args!("")).finish(), + None => f + .debug_tuple("event") + .field(&format_args!("")) + .finish(), } } } @@ -212,8 +215,8 @@ impl Event { Self { inner: AtomicPtr::new(ptr::null_mut()), } - } - + } + /// Tell whether any listeners are currently notified. /// /// # Examples From 944a5470f41d79796b750298c90126a2a0045fa2 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 19 Oct 2023 17:44:27 +0400 Subject: [PATCH 20/26] fmt fix vol2 --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 191d040..db37878 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -215,8 +215,8 @@ impl Event { Self { inner: AtomicPtr::new(ptr::null_mut()), } - } - + } + /// Tell whether any listeners are currently notified. /// /// # Examples From c5eb467f8e48aa5c184b7368758a3b717384f862 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 19 Oct 2023 17:48:57 +0400 Subject: [PATCH 21/26] fmt fix vol3 --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index db37878..0dca5f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -470,6 +470,7 @@ impl Event<()> { inner: AtomicPtr::new(ptr::null_mut()), } } + /// Notifies a number of active listeners without emitting a `SeqCst` fence. /// /// The number is allowed to be zero or exceed the current number of listeners. From 84b077e75191a28838e9d0ab70a4008014decf00 Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 19 Oct 2023 17:51:52 +0400 Subject: [PATCH 22/26] add try_locl --- src/std.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/std.rs b/src/std.rs index a6bdb6c..f42578b 100644 --- a/src/std.rs +++ b/src/std.rs @@ -46,7 +46,13 @@ impl List { } // Accessor method because fields are private, not sure how to go around it pub fn total_listeners(&self) -> Result { - self.0.lock().map(|mutex| mutex.len).map_err(|_| "") + match self.0.try_lock() { + Ok(mutex) => { + let len = mutex.len; + Ok(len) + } + Err(_) => Err("") + } } } From 7dfd50a5fe84801b10bfc2b9104bd37393f6200e Mon Sep 17 00:00:00 2001 From: scirin Date: Thu, 19 Oct 2023 17:53:54 +0400 Subject: [PATCH 23/26] fix --- src/std.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/std.rs b/src/std.rs index f42578b..4b9c13e 100644 --- a/src/std.rs +++ b/src/std.rs @@ -51,7 +51,7 @@ impl List { let len = mutex.len; Ok(len) } - Err(_) => Err("") + Err(_) => Err(""), } } } From c50fbd4c5b2930e770de42fac7ba81af82a110a0 Mon Sep 17 00:00:00 2001 From: Irine <102310764+scirin@users.noreply.github.com> Date: Sat, 21 Oct 2023 17:01:41 +0400 Subject: [PATCH 24/26] Update src/lib.rs Co-authored-by: John Nunley --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0dca5f7..8ec2582 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -174,7 +174,10 @@ impl fmt::Debug for Event { match self.try_inner() { Some(inner) => { let notified_count = inner.notified.load(Ordering::Relaxed); - let total_count = inner.list.total_listeners(); + 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) From 7b352ce9777e6812a4311735d2bae2da63fe256d Mon Sep 17 00:00:00 2001 From: scirin Date: Sat, 21 Oct 2023 17:09:10 +0400 Subject: [PATCH 25/26] Final fixes --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8ec2582..a872647 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,7 +185,7 @@ impl fmt::Debug for Event { .finish() } None => f - .debug_tuple("event") + .debug_tuple("Event") .field(&format_args!("")) .finish(), } From a10783b5c8ae6fbf744075a6c8746828ae6329ab Mon Sep 17 00:00:00 2001 From: scirin Date: Sat, 21 Oct 2023 17:12:46 +0400 Subject: [PATCH 26/26] fmt fix --- src/lib.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index a872647..6dc3967 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -176,7 +176,12 @@ impl fmt::Debug for Event { 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(), + Err(_) => { + return f + .debug_tuple("Event") + .field(&format_args!("")) + .finish() + } }; f.debug_struct("Event")