Skip to content

Commit

Permalink
sync: make watch::send_replace infallible (#4195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn authored Oct 27, 2021
1 parent 268ed5e commit 75c0777
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,30 +428,32 @@ impl<T> Sender<T> {
/// 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<T>> {
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(())
}

/// Sends a new value via the channel, notifying all receivers and returning
/// 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
///
/// ```
/// 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<T, error::SendError<T>> {
// 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();
Expand All @@ -472,7 +474,7 @@ impl<T> Sender<T> {
// Notify all watchers
self.shared.notify_rx.notify_waiters();

Ok(old)
old
}

/// Returns a reference to the most recently sent value
Expand Down

0 comments on commit 75c0777

Please sign in to comment.