From 6ca4933cb149b871dcc2cf5857030146c4f71b78 Mon Sep 17 00:00:00 2001 From: LinkTed Date: Fri, 12 Jun 2020 20:21:53 +0200 Subject: [PATCH 1/2] Added a `is_receiver` to (`Unbounded`)`Sender`. --- futures-channel/src/mpsc/mod.rs | 26 ++++++++++++++++++++++++++ futures-channel/tests/mpsc.rs | 11 +++++++++++ 2 files changed, 37 insertions(+) diff --git a/futures-channel/src/mpsc/mod.rs b/futures-channel/src/mpsc/mod.rs index bce5e2e1e0..249346941a 100644 --- a/futures-channel/src/mpsc/mod.rs +++ b/futures-channel/src/mpsc/mod.rs @@ -481,6 +481,11 @@ impl UnboundedSenderInner { Arc::ptr_eq(&self.inner, &other.inner) } + /// Returns whether the sender send to this receiver. + fn is_receiver(&self, inner: &Arc>) -> 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! @@ -657,6 +662,11 @@ impl BoundedSenderInner { Arc::ptr_eq(&self.inner, &other.inner) } + /// Returns whether the sender send to this receiver. + fn is_receiver(&self, receiver: &Arc>) -> 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! @@ -779,6 +789,14 @@ impl Sender { } } + /// Returns whether the sender send to this receiver. + pub fn is_receiver(&self, receiver: &Receiver) -> bool { + match (&self.0, &receiver.inner) { + (Some(inner), Some(receiver)) => inner.is_receiver(receiver), + _ => false, + } + } + /// Hashes the receiver into the provided hasher pub fn hash_receiver(&self, hasher: &mut H) where H: std::hash::Hasher { use std::hash::Hash; @@ -860,6 +878,14 @@ impl UnboundedSender { } } + /// Returns whether the sender send to this receiver. + pub fn is_receiver(&self, receiver: &UnboundedReceiver) -> bool { + match (&self.0, &receiver.inner) { + (Some(inner), Some(receiver)) => inner.is_receiver(receiver), + _ => false, + } + } + /// Hashes the receiver into the provided hasher pub fn hash_receiver(&self, hasher: &mut H) where H: std::hash::Hasher { use std::hash::Hash; diff --git a/futures-channel/tests/mpsc.rs b/futures-channel/tests/mpsc.rs index 409fa6e308..f675c0d299 100644 --- a/futures-channel/tests/mpsc.rs +++ b/futures-channel/tests/mpsc.rs @@ -527,6 +527,17 @@ fn same_receiver() { assert!(txb1.same_receiver(&txb2)); } +#[test] +fn is_receiver() { + let (txa, rxa) = mpsc::channel::(1); + let (txb, rxb) = mpsc::channel::(1); + + assert!(txa.is_receiver(&rxa)); + assert!(txb.is_receiver(&rxb)); + assert!(!txa.is_receiver(&rxb)); + assert!(!txb.is_receiver(&rxa)); +} + #[test] fn hash_receiver() { use std::hash::Hasher; From 5ff1af0cd20d1f82c7ac6c797bd63671b3a88168 Mon Sep 17 00:00:00 2001 From: LinkTed Date: Fri, 12 Jun 2020 21:33:36 +0200 Subject: [PATCH 2/2] Rename `is_receiver` to `is_connected_to` --- futures-channel/src/mpsc/mod.rs | 12 ++++++------ futures-channel/tests/mpsc.rs | 10 +++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/futures-channel/src/mpsc/mod.rs b/futures-channel/src/mpsc/mod.rs index 249346941a..64c551eb39 100644 --- a/futures-channel/src/mpsc/mod.rs +++ b/futures-channel/src/mpsc/mod.rs @@ -482,7 +482,7 @@ impl UnboundedSenderInner { } /// Returns whether the sender send to this receiver. - fn is_receiver(&self, inner: &Arc>) -> bool { + fn is_connected_to(&self, inner: &Arc>) -> bool { Arc::ptr_eq(&self.inner, &inner) } @@ -663,7 +663,7 @@ impl BoundedSenderInner { } /// Returns whether the sender send to this receiver. - fn is_receiver(&self, receiver: &Arc>) -> bool { + fn is_connected_to(&self, receiver: &Arc>) -> bool { Arc::ptr_eq(&self.inner, &receiver) } @@ -790,9 +790,9 @@ impl Sender { } /// Returns whether the sender send to this receiver. - pub fn is_receiver(&self, receiver: &Receiver) -> bool { + pub fn is_connected_to(&self, receiver: &Receiver) -> bool { match (&self.0, &receiver.inner) { - (Some(inner), Some(receiver)) => inner.is_receiver(receiver), + (Some(inner), Some(receiver)) => inner.is_connected_to(receiver), _ => false, } } @@ -879,9 +879,9 @@ impl UnboundedSender { } /// Returns whether the sender send to this receiver. - pub fn is_receiver(&self, receiver: &UnboundedReceiver) -> bool { + pub fn is_connected_to(&self, receiver: &UnboundedReceiver) -> bool { match (&self.0, &receiver.inner) { - (Some(inner), Some(receiver)) => inner.is_receiver(receiver), + (Some(inner), Some(receiver)) => inner.is_connected_to(receiver), _ => false, } } diff --git a/futures-channel/tests/mpsc.rs b/futures-channel/tests/mpsc.rs index f675c0d299..5571b27aff 100644 --- a/futures-channel/tests/mpsc.rs +++ b/futures-channel/tests/mpsc.rs @@ -528,14 +528,14 @@ fn same_receiver() { } #[test] -fn is_receiver() { +fn is_connected_to() { let (txa, rxa) = mpsc::channel::(1); let (txb, rxb) = mpsc::channel::(1); - assert!(txa.is_receiver(&rxa)); - assert!(txb.is_receiver(&rxb)); - assert!(!txa.is_receiver(&rxb)); - assert!(!txb.is_receiver(&rxa)); + 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]