From 8bfb1c92ceadd04f847d98ed482e7e59a3074954 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 21 Oct 2020 15:14:52 -0700 Subject: sync: revert Clone impl for broadcast::Receiver (#3020) The `Receiver` handle maintains a position in the broadcast channel for itself. Cloning implies copying the state of the value. Intuitively, cloning a `broadcast::Receiver` would return a new receiver with an identical position. However, the current implementation returns a new `Receiver` positioned at the tail of the channel. This behavior subtlety is why `new_subscriber()` is used to create `Receiver` handles. An alternate API should consider the position issue. Refs: #2933 --- tokio/src/sync/broadcast.rs | 10 +--------- tokio/src/sync/tests/loom_broadcast.rs | 3 +-- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/tokio/src/sync/broadcast.rs b/tokio/src/sync/broadcast.rs index 4e74a3aa..ee9aba07 100644 --- a/tokio/src/sync/broadcast.rs +++ b/tokio/src/sync/broadcast.rs @@ -405,8 +405,7 @@ const MAX_RECEIVERS: usize = usize::MAX >> 2; /// /// The `Sender` can be cloned to `send` to the same channel from multiple /// points in the process or it can be used concurrently from an `Arc`. New -/// `Receiver` handles can be cloned from an existing `Receiver` or created by -/// calling [`Sender::subscribe`]. +/// `Receiver` handles are created by calling [`Sender::subscribe`]. /// /// If all [`Receiver`] handles are dropped, the `send` method will return a /// [`SendError`]. Similarly, if all [`Sender`] handles are dropped, the [`recv`] @@ -985,13 +984,6 @@ impl Receiver { } } -impl Clone for Receiver { - fn clone(&self) -> Self { - let shared = self.shared.clone(); - new_receiver(shared) - } -} - impl Drop for Receiver { fn drop(&mut self) { let mut tail = self.shared.tail.lock(); diff --git a/tokio/src/sync/tests/loom_broadcast.rs b/tokio/src/sync/tests/loom_broadcast.rs index e15dc25f..4b1f034f 100644 --- a/tokio/src/sync/tests/loom_broadcast.rs +++ b/tokio/src/sync/tests/loom_broadcast.rs @@ -92,12 +92,11 @@ fn broadcast_two() { }); } -// Exercise the Receiver Clone impl as well #[test] fn broadcast_wrap() { loom::model(|| { let (tx, mut rx1) = broadcast::channel(2); - let mut rx2 = rx1.clone(); + let mut rx2 = tx.subscribe(); let th1 = thread::spawn(move || { block_on(async { -- cgit v1.2.3