sync: add receiver_count to watch::Sender
@@ -417,6 +417,28 @@ impl<T> Sender<T> {
Receiver::from_shared(version, shared)
}
}
+
+ /// Returns the number of receivers that currently exist
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use tokio::sync::watch;
+ ///
+ /// #[tokio::main]
+ /// async fn main() {
+ /// let (tx, rx1) = watch::channel("hello");
+ ///
+ /// assert_eq!(1, tx.receiver_count());
+ ///
+ /// let mut _rx2 = rx1.clone();
+ ///
+ /// assert_eq!(2, tx.receiver_count());
+ /// }
+ /// ```
+ pub fn receiver_count(&self) -> usize {
+ self.shared.ref_count_rx.load(Relaxed)
+ }
}
impl<T> Drop for Sender<T> {
