From a8b75dbdf4360c9fd7fb874022169f0c00d38c4a Mon Sep 17 00:00:00 2001 From: Matthew Ahrens Date: Fri, 25 Mar 2022 12:16:47 -0700 Subject: [PATCH] sync: add `watch::Receiver::same_channel` (#4581) --- 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 ab0ada7d0..42be67613 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -467,6 +467,22 @@ impl Receiver { } } + /// Returns `true` if receivers belong to the same channel. + /// + /// # Examples + /// + /// ``` + /// let (tx, rx) = tokio::sync::watch::channel(true); + /// let rx2 = rx.clone(); + /// assert!(rx.same_channel(&rx2)); + /// + /// let (tx3, rx3) = tokio::sync::watch::channel(true); + /// assert!(!rx3.same_channel(&rx2)); + /// ``` + pub fn same_channel(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.shared, &other.shared) + } + cfg_process_driver! { pub(crate) fn try_has_changed(&mut self) -> Option> { maybe_changed(&self.shared, &mut self.version)