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

Added a is_connected_to to (Unbounded)Sender #2179

Merged
merged 3 commits into from Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions futures-channel/src/mpsc/mod.rs
Expand Up @@ -481,6 +481,11 @@ impl<T> UnboundedSenderInner<T> {
Arc::ptr_eq(&self.inner, &other.inner)
}

/// Returns whether the sender send to this receiver.
fn is_connected_to(&self, inner: &Arc<UnboundedInner<T>>) -> bool {
Arc::ptr_eq(&self.inner, &inner)
}

/// Returns pointer to the Arc containing sender
///
/// The returned pointer is not referenced and should be only used for hashing!
Expand Down Expand Up @@ -657,6 +662,11 @@ impl<T> BoundedSenderInner<T> {
Arc::ptr_eq(&self.inner, &other.inner)
}

/// Returns whether the sender send to this receiver.
fn is_connected_to(&self, receiver: &Arc<BoundedInner<T>>) -> bool {
Arc::ptr_eq(&self.inner, &receiver)
}

/// Returns pointer to the Arc containing sender
///
/// The returned pointer is not referenced and should be only used for hashing!
Expand Down Expand Up @@ -779,6 +789,14 @@ impl<T> Sender<T> {
}
}

/// Returns whether the sender send to this receiver.
pub fn is_connected_to(&self, receiver: &Receiver<T>) -> bool {
match (&self.0, &receiver.inner) {
(Some(inner), Some(receiver)) => inner.is_connected_to(receiver),
_ => false,
}
}

/// Hashes the receiver into the provided hasher
pub fn hash_receiver<H>(&self, hasher: &mut H) where H: std::hash::Hasher {
use std::hash::Hash;
Expand Down Expand Up @@ -860,6 +878,14 @@ impl<T> UnboundedSender<T> {
}
}

/// Returns whether the sender send to this receiver.
pub fn is_connected_to(&self, receiver: &UnboundedReceiver<T>) -> bool {
match (&self.0, &receiver.inner) {
(Some(inner), Some(receiver)) => inner.is_connected_to(receiver),
_ => false,
}
}

/// Hashes the receiver into the provided hasher
pub fn hash_receiver<H>(&self, hasher: &mut H) where H: std::hash::Hasher {
use std::hash::Hash;
Expand Down
11 changes: 11 additions & 0 deletions futures-channel/tests/mpsc.rs
Expand Up @@ -529,6 +529,17 @@ fn same_receiver() {
assert!(txb1.same_receiver(&txb2));
}

#[test]
fn is_connected_to() {
let (txa, rxa) = mpsc::channel::<i32>(1);
let (txb, rxb) = mpsc::channel::<i32>(1);

assert!(txa.is_connected_to(&rxa));
assert!(txb.is_connected_to(&rxb));
assert!(!txa.is_connected_to(&rxb));
assert!(!txb.is_connected_to(&rxa));
}

#[test]
fn hash_receiver() {
use std::hash::Hasher;
Expand Down