From 81849057e807f2c45f5864c140461f39448e6c12 Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Tue, 23 Jul 2024 20:17:47 +0300 Subject: [PATCH 1/2] Add a fn to check if two senders or two receivers use the same underlying channel. --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 245a216..685ac3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -493,6 +493,11 @@ impl Sender { channel: self.channel.clone(), } } + + /// Returns whether the senders belong to the same channel. + pub fn same_channel(&self, other: &Sender) -> bool { + Arc::ptr_eq(&self.channel, &other.channel) + } } impl Drop for Sender { @@ -820,6 +825,11 @@ impl Receiver { channel: self.channel.clone(), } } + + /// Returns whether the receivers belong to the same channel. + pub fn same_channel(&self, other: &Receiver) -> bool { + Arc::ptr_eq(&self.channel, &other.channel) + } } impl fmt::Debug for Receiver { From b6d906d7a2ee23b0fc27c5a24331f9bbfa2fb0fd Mon Sep 17 00:00:00 2001 From: Ruediger Klaehn Date: Mon, 29 Jul 2024 10:29:57 +0300 Subject: [PATCH 2/2] Add example usage for fn same_channel in Sender and Receiver --- src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 685ac3e..3bdc944 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -495,6 +495,19 @@ impl Sender { } /// Returns whether the senders belong to the same channel. + /// + /// # Examples + /// + /// ``` + /// # futures_lite::future::block_on(async { + /// use async_channel::unbounded; + /// + /// let (s, r) = unbounded::<()>(); + /// let s2 = s.clone(); + /// + /// assert!(s.same_channel(&s2)); + /// # }); + /// ``` pub fn same_channel(&self, other: &Sender) -> bool { Arc::ptr_eq(&self.channel, &other.channel) } @@ -827,6 +840,19 @@ impl Receiver { } /// Returns whether the receivers belong to the same channel. + /// + /// # Examples + /// + /// ``` + /// # futures_lite::future::block_on(async { + /// use async_channel::unbounded; + /// + /// let (s, r) = unbounded::<()>(); + /// let r2 = r.clone(); + /// + /// assert!(r.same_channel(&r2)); + /// # }); + /// ``` pub fn same_channel(&self, other: &Receiver) -> bool { Arc::ptr_eq(&self.channel, &other.channel) }