diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index 74c5b40ecee..7e45c116c82 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -428,7 +428,12 @@ 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> { - self.send_replace(value)?; + // 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 /// @@ -443,15 +450,10 @@ 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) -> 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