Skip to content

Commit e06b257

Browse files
authored
sync: add same_channel method to mpsc Senders (#3532)
1 parent 0867a6f commit e06b257

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

tokio/src/sync/mpsc/bounded.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,22 @@ impl<T> Sender<T> {
698698

699699
Ok(Permit { chan: &self.chan })
700700
}
701+
702+
/// Returns `true` if senders belong to the same channel.
703+
///
704+
/// # Examples
705+
///
706+
/// ```
707+
/// let (tx, rx) = tokio::sync::mpsc::channel::<()>(1);
708+
/// let tx2 = tx.clone();
709+
/// assert!(tx.same_channel(&tx2));
710+
///
711+
/// let (tx3, rx3) = tokio::sync::mpsc::channel::<()>(1);
712+
/// assert!(!tx3.same_channel(&tx2));
713+
/// ```
714+
pub fn same_channel(&self, other: &Self) -> bool {
715+
self.chan.same_channel(&other.chan)
716+
}
701717
}
702718

703719
impl<T> Clone for Sender<T> {

tokio/src/sync/mpsc/chan.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ impl<T, S> Tx<T, S> {
139139
pub(crate) fn wake_rx(&self) {
140140
self.inner.rx_waker.wake();
141141
}
142+
143+
/// Returns `true` if senders belong to the same channel.
144+
pub(crate) fn same_channel(&self, other: &Self) -> bool {
145+
Arc::ptr_eq(&self.inner, &other.inner)
146+
}
142147
}
143148

144149
impl<T, S: Semaphore> Tx<T, S> {

tokio/src/sync/mpsc/unbounded.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,20 @@ impl<T> UnboundedSender<T> {
291291
pub fn is_closed(&self) -> bool {
292292
self.chan.is_closed()
293293
}
294+
295+
/// Returns `true` if senders belong to the same channel.
296+
///
297+
/// # Examples
298+
///
299+
/// ```
300+
/// let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<()>();
301+
/// let tx2 = tx.clone();
302+
/// assert!(tx.same_channel(&tx2));
303+
///
304+
/// let (tx3, rx3) = tokio::sync::mpsc::unbounded_channel::<()>();
305+
/// assert!(!tx3.same_channel(&tx2));
306+
/// ```
307+
pub fn same_channel(&self, other: &Self) -> bool {
308+
self.chan.same_channel(&other.chan)
309+
}
294310
}

0 commit comments

Comments
 (0)