From a9c5e57387e803f8c02e597403cb19be6720fca9 Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Tue, 16 Jan 2024 01:00:59 +0100 Subject: [PATCH 1/8] add method total_listeners() when feature std --- src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ src/std.rs | 8 ++++++++ 2 files changed, 42 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 39497ef..f85eda1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -479,6 +479,40 @@ impl Event { inner } + + /// Return the listener count + /// + /// This is only available when `std` feature is enabled. + /// + /// # Examples + /// + /// ``` + /// use event_listener::Event; + /// + /// let event = Event::new(); + /// + /// assert_eq!(event.total_listeners().unwrap(), 0); + /// + /// let listener1 = event.listen(); + /// assert_eq!(event.total_listeners().unwrap(), 1); + /// + /// let listener2 = event.listen(); + /// assert_eq!(event.total_listeners().unwrap(), 2); + /// + /// drop(listener1); + /// drop(listener2); + /// assert_eq!(event.total_listeners().unwrap(), 0); + /// + /// ``` + #[cfg(feature = "std")] + #[inline] + pub fn total_listeners(&self) -> Result { + if let Some(inner) = self.try_inner() { + inner.list.total_listeners_wait() + } else { + Ok(0) + } + } } impl Event<()> { diff --git a/src/std.rs b/src/std.rs index d0d0144..af3aa7e 100644 --- a/src/std.rs +++ b/src/std.rs @@ -54,6 +54,14 @@ impl List { Err(_) => Err(""), } } + + // Get the listener count by blocking + pub(crate) fn total_listeners_wait(&self) -> Result { + match self.0.lock() { + Ok(mutex) => Ok(mutex.len), + Err(_) => Err("MutexPoisoned".into()), + } + } } impl crate::Inner { From c3a0f4a75f5d2d7cf869edb33270455e0f17afc9 Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Tue, 16 Jan 2024 03:16:23 +0100 Subject: [PATCH 2/8] review points --- src/lib.rs | 5 ++--- src/std.rs | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f85eda1..76e7f49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -502,15 +502,14 @@ impl Event { /// drop(listener1); /// drop(listener2); /// assert_eq!(event.total_listeners().unwrap(), 0); - /// /// ``` #[cfg(feature = "std")] #[inline] - pub fn total_listeners(&self) -> Result { + pub fn total_listeners(&self) -> usize { if let Some(inner) = self.try_inner() { inner.list.total_listeners_wait() } else { - Ok(0) + 0 } } } diff --git a/src/std.rs b/src/std.rs index af3aa7e..8d383c0 100644 --- a/src/std.rs +++ b/src/std.rs @@ -56,10 +56,12 @@ impl List { } // Get the listener count by blocking - pub(crate) fn total_listeners_wait(&self) -> Result { + // 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. + pub(crate) fn total_listeners_wait(&self) -> usize { match self.0.lock() { - Ok(mutex) => Ok(mutex.len), - Err(_) => Err("MutexPoisoned".into()), + Ok(mutex) => mutex.len, + Err(err) => panic!("{err}"), } } } From 396492dbd788ff98545474635637e42875cd8303 Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Tue, 16 Jan 2024 03:19:17 +0100 Subject: [PATCH 3/8] fix doc tests --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 76e7f49..556afd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -491,17 +491,17 @@ impl Event { /// /// let event = Event::new(); /// - /// assert_eq!(event.total_listeners().unwrap(), 0); + /// assert_eq!(event.total_listeners(), 0); /// /// let listener1 = event.listen(); - /// assert_eq!(event.total_listeners().unwrap(), 1); + /// assert_eq!(event.total_listeners(), 1); /// /// let listener2 = event.listen(); - /// assert_eq!(event.total_listeners().unwrap(), 2); + /// assert_eq!(event.total_listeners(), 2); /// /// drop(listener1); /// drop(listener2); - /// assert_eq!(event.total_listeners().unwrap(), 0); + /// assert_eq!(event.total_listeners(), 0); /// ``` #[cfg(feature = "std")] #[inline] From 57a0ea77f7c5dc5a3c0f410abe298e5a39fa507b Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Tue, 16 Jan 2024 03:21:06 +0100 Subject: [PATCH 4/8] adjust documentation --- src/lib.rs | 3 ++- src/std.rs | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 556afd3..5d0300b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -481,7 +481,8 @@ impl Event { } /// Return the listener count - /// + /// 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. /// /// # Examples diff --git a/src/std.rs b/src/std.rs index 8d383c0..63f69ac 100644 --- a/src/std.rs +++ b/src/std.rs @@ -56,8 +56,6 @@ impl List { } // Get the listener count by blocking - // 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. pub(crate) fn total_listeners_wait(&self) -> usize { match self.0.lock() { Ok(mutex) => mutex.len, From 63b4ce7f42ec86be01ebe00dfd75afa34beb77a5 Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Tue, 16 Jan 2024 03:22:28 +0100 Subject: [PATCH 5/8] adjust documentation --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5d0300b..4722db8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -480,7 +480,7 @@ impl Event { inner } - /// Return the listener count + /// Return the listener count by aquiring a lock /// 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. From 05eb69d0cbe929a6c225a4079dee9b2485d616ec Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Tue, 16 Jan 2024 03:22:47 +0100 Subject: [PATCH 6/8] adjust documentation --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 4722db8..6c834d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -480,7 +480,7 @@ impl Event { inner } - /// Return the listener count by aquiring a lock + /// Return the listener count by acquiring a lock /// 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. From 2d889750b3161f181380c461f511fb1afa68cc6c Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Sun, 21 Jan 2024 13:45:33 +0100 Subject: [PATCH 7/8] review points --- src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6c834d2..c5d338a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -480,8 +480,10 @@ impl Event { inner } - /// Return the listener count by acquiring a lock - /// 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. + /// Return the listener count by acquiring a lock. + /// + /// 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. /// From 896e1e87901f4d73587dfbec96d3b99fbcc61133 Mon Sep 17 00:00:00 2001 From: Linken Quy Dinh Date: Sun, 21 Jan 2024 13:46:28 +0100 Subject: [PATCH 8/8] review points --- src/std.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/std.rs b/src/std.rs index 63f69ac..fb06819 100644 --- a/src/std.rs +++ b/src/std.rs @@ -44,7 +44,7 @@ impl List { notified: 0, })) } - // Accessor method 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) -> Result { match self.0.try_lock() { Ok(mutex) => { @@ -55,7 +55,7 @@ impl List { } } - // Get the listener count by blocking + // Get the listener count by blocking. pub(crate) fn total_listeners_wait(&self) -> usize { match self.0.lock() { Ok(mutex) => mutex.len,