From 652f0ae7287794d7cd476d4965e143b2f3901bb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Meier?= Date: Sat, 15 May 2021 11:56:24 +0200 Subject: [PATCH] sync: add receiver_count to watch::Sender (#3729) --- tokio/src/sync/watch.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index db65e5a5e29..42d417a0fe5 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -417,6 +417,28 @@ impl Sender { 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 Drop for Sender {