Skip to content

Commit

Permalink
Add a is_connected_to to (Unbounded)Sender (#2179)
Browse files Browse the repository at this point in the history
Co-authored-by: LinkTed <LinkTed@users.noreply.github.com>
  • Loading branch information
LinkTed and LinkTed committed Sep 5, 2020
1 parent c5c4541 commit 575dcf8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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

0 comments on commit 575dcf8

Please sign in to comment.