sync: add receiver_count to watch::Sender (#3729)

This commit is contained in:
Sören Meier
2021-05-15 11:56:24 +02:00
committed by GitHub
parent ff9b0ef7ca
commit 652f0ae728
+22
View File
@@ -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> {