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 1 commit
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
2 changes: 2 additions & 0 deletions tokio/src/sync/mpsc/bounded.rs
Expand Up @@ -16,6 +16,7 @@ use std::task::{Context, Poll};
/// Send values to the associated `Receiver`.
///
/// Instances are created by the [`channel`](channel) function.
#[derive(PartialEq, Eq)]
pub struct Sender<T> {
chan: chan::Tx<T, Semaphore>,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A channel can only ever have one Sender, so this doesn't make much sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Receiver, yes it doesn't make much sense. For users it will be better to use false directly instead of depends on it's eq or same_channel method.

Expand All @@ -38,6 +39,7 @@ pub struct Permit<'a, T> {
/// This receiver can be turned into a `Stream` using [`ReceiverStream`].
///
/// [`ReceiverStream`]: https://docs.rs/tokio-stream/0.1/tokio_stream/wrappers/struct.ReceiverStream.html
#[derive(PartialEq, Eq)]
pub struct Receiver<T> {
/// The channel receiver
chan: chan::Rx<T, Semaphore>,
Expand Down
16 changes: 16 additions & 0 deletions tokio/src/sync/mpsc/chan.rs
Expand Up @@ -22,6 +22,14 @@ impl<T, S: fmt::Debug> fmt::Debug for Tx<T, S> {
}
}

impl<T, S> PartialEq for Tx<T, S> {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}

impl<T, S> Eq for Tx<T, S> {}

/// Channel receiver
pub(crate) struct Rx<T, S: Semaphore> {
inner: Arc<Chan<T, S>>,
Expand All @@ -33,6 +41,14 @@ impl<T, S: Semaphore + fmt::Debug> fmt::Debug for Rx<T, S> {
}
}

impl<T, S: Semaphore> PartialEq for Rx<T, S> {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}

impl<T, S: Semaphore> Eq for Rx<T, S> {}

pub(crate) trait Semaphore {
fn is_idle(&self) -> bool;

Expand Down
2 changes: 2 additions & 0 deletions tokio/src/sync/mpsc/unbounded.rs
Expand Up @@ -9,6 +9,7 @@ use std::task::{Context, Poll};
///
/// Instances are created by the
/// [`unbounded_channel`](unbounded_channel) function.
#[derive(PartialEq, Eq)]
pub struct UnboundedSender<T> {
chan: chan::Tx<T, Semaphore>,
}
Expand Down Expand Up @@ -37,6 +38,7 @@ impl<T> fmt::Debug for UnboundedSender<T> {
/// This receiver can be turned into a `Stream` using [`UnboundedReceiverStream`].
///
/// [`UnboundedReceiverStream`]: https://docs.rs/tokio-stream/0.1/tokio_stream/wrappers/struct.UnboundedReceiverStream.html
#[derive(PartialEq, Eq)]
pub struct UnboundedReceiver<T> {
/// The channel receiver
chan: chan::Rx<T, Semaphore>,
Expand Down