From 3bf4f938543bfb4c7122b7f21e9154f4a3b27122 Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Sat, 15 Jun 2024 21:11:35 +0200 Subject: [PATCH] sync: add `watch::Sender::same_channel` (#6637) --- tokio/src/sync/watch.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index 5810c67de..366066797 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -1324,6 +1324,22 @@ impl Sender { pub fn receiver_count(&self) -> usize { self.shared.ref_count_rx.load(Relaxed) } + + /// Returns `true` if senders belong to the same channel. + /// + /// # Examples + /// + /// ``` + /// let (tx, rx) = tokio::sync::watch::channel(true); + /// let tx2 = tx.clone(); + /// assert!(tx.same_channel(&tx2)); + /// + /// let (tx3, rx3) = tokio::sync::watch::channel(true); + /// assert!(!tx3.same_channel(&tx2)); + /// ``` + pub fn same_channel(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.shared, &other.shared) + } } impl Drop for Sender {