Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: add same_channel method to mpsc Senders #3532

Merged
merged 4 commits into from Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions tokio/src/sync/mpsc/bounded.rs
Expand Up @@ -698,6 +698,20 @@ impl<T> Sender<T> {

Ok(Permit { chan: &self.chan })
}

/// Returns `true` if senders belong to the same channel.
///
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
/// ```
/// let (tx, rx) = tokio::sync::mpsc::channel::<()>(1);
/// let tx2 = tx.clone();
/// assert!(tx.same_channel(&tx2));
///
/// let (tx3, rx3) = tokio::sync::mpsc::channel::<()>(1);
/// assert!(!tx3.same_channel(&tx2));
/// ```
pub fn same_channel(&self, other: &Self) -> bool {
self.chan.same_channel(&other.chan)
}
}

impl<T> Clone for Sender<T> {
Expand Down
5 changes: 5 additions & 0 deletions tokio/src/sync/mpsc/chan.rs
Expand Up @@ -139,6 +139,11 @@ impl<T, S> Tx<T, S> {
pub(crate) fn wake_rx(&self) {
self.inner.rx_waker.wake();
}

/// Returns `true` if senders belong to the same channel.
pub(crate) fn same_channel(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}

impl<T, S: Semaphore> Tx<T, S> {
Expand Down
14 changes: 14 additions & 0 deletions tokio/src/sync/mpsc/unbounded.rs
Expand Up @@ -291,4 +291,18 @@ impl<T> UnboundedSender<T> {
pub fn is_closed(&self) -> bool {
self.chan.is_closed()
}

/// Returns `true` if senders belong to the same channel.
///
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
/// ```
/// let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<()>();
/// let tx2 = tx.clone();
/// assert!(tx.same_channel(&tx2));
///
/// let (tx3, rx3) = tokio::sync::mpsc::unbounded_channel::<()>();
/// assert!(!tx3.same_channel(&tx2));
/// ```
pub fn same_channel(&self, other: &Self) -> bool {
self.chan.same_channel(&other.chan)
}
}