From d5d68a22bcd8d2e3709e468fe1c6fe77056c3c05 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 26 Oct 2021 13:36:27 +0000 Subject: [PATCH 1/3] sync: make `watch::send_replace` infallible --- tokio/src/sync/watch.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index 74c5b40ecee..f41c5de5671 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -428,6 +428,11 @@ impl Sender { /// This method fails if the channel has been closed, which happens when /// every receiver has been dropped. pub fn send(&self, value: T) -> Result<(), error::SendError> { + // This is pretty much only useful as a hint anyway, so synchronization isn't critical. + if 0 == self.receiver_count() { + return Err(error::SendError(value)); + } + self.send_replace(value)?; Ok(()) } @@ -436,6 +441,8 @@ impl Sender { /// the previous value in the channel. /// /// This can be useful for reusing the buffers inside a watched value. + /// Additionally, this method permits sending values even when there are no + /// receivers. /// /// # Examples /// @@ -446,12 +453,7 @@ impl Sender { /// assert_eq!(tx.send_replace(2).unwrap(), 1); /// assert_eq!(tx.send_replace(3).unwrap(), 2); /// ``` - pub fn send_replace(&self, value: T) -> Result> { - // This is pretty much only useful as a hint anyway, so synchronization isn't critical. - if 0 == self.receiver_count() { - return Err(error::SendError(value)); - } - + pub fn send_replace(&self, value: T) -> T { let old = { // Acquire the write lock and update the value. let mut lock = self.shared.value.write().unwrap(); @@ -472,7 +474,7 @@ impl Sender { // Notify all watchers self.shared.notify_rx.notify_waiters(); - Ok(old) + old } /// Returns a reference to the most recently sent value From 9fb859d3e6c6e0ffd8b573cb8f99478f6683a28c Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 26 Oct 2021 13:42:52 +0000 Subject: [PATCH 2/3] Remove question mark --- tokio/src/sync/watch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index f41c5de5671..e33977cae3f 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -433,7 +433,7 @@ impl Sender { return Err(error::SendError(value)); } - self.send_replace(value)?; + self.send_replace(value); Ok(()) } From 966e4bb9afa8ce95d9bf0386fcc726fa993f15e9 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 26 Oct 2021 16:18:40 +0200 Subject: [PATCH 3/3] Update tokio/src/sync/watch.rs Co-authored-by: Taiki Endo --- tokio/src/sync/watch.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index e33977cae3f..7e45c116c82 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -450,8 +450,8 @@ impl Sender { /// use tokio::sync::watch; /// /// let (tx, _rx) = watch::channel(1); - /// assert_eq!(tx.send_replace(2).unwrap(), 1); - /// assert_eq!(tx.send_replace(3).unwrap(), 2); + /// assert_eq!(tx.send_replace(2), 1); + /// assert_eq!(tx.send_replace(3), 2); /// ``` pub fn send_replace(&self, value: T) -> T { let old = {