Skip to content

Commit

Permalink
Extend documentation for watch::Receiver<_>::wait_for
Browse files Browse the repository at this point in the history
- The "Cancel safety" section was missing, as reported in tokio-rs#7021.
- Add the "Panics" section since technically the method can panic.
- Change the doctest to mimic the one for `changed` (also I find the
current version slightly more confusing than it should be).
  • Loading branch information
cip999 committed Dec 15, 2024
1 parent 10e23d1 commit 7000f08
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ impl<T> Receiver<T> {
/// When this function returns, the value that was passed to the closure
/// when it returned `true` will be considered seen.
///
/// If the channel is closed, then `wait_for` will return a `RecvError`.
/// If the channel is closed, then `wait_for` will return a [`RecvError`].
/// Once this happens, no more messages can ever be sent on the channel.
/// When an error is returned, it is guaranteed that the closure has been
/// called on the last value, and that it returned `false` for that value.
Expand All @@ -780,31 +780,35 @@ impl<T> Receiver<T> {
/// possible. See the documentation of `borrow` for more information on
/// this.
///
/// [`Receiver::changed()`]: crate::sync::watch::Receiver::changed
/// [`RecvError`]: error::RecvError
///
/// # Cancel safety
///
/// This method is cancel safe. If dropped before completion, it is
/// guaranteed that the last seen value `val` satisfies `f(val) == false`.
///
/// # Panics
///
/// If and only if the closure `f` panics. In that case, no resource owned
/// or shared by this [`Receiver`] will be poisoned.
///
/// # Examples
///
/// ```
/// use tokio::sync::watch;
/// use tokio::time::{sleep, Duration};
///
/// #[tokio::main]
///
/// async fn main() {
/// let (tx, _rx) = watch::channel("hello");
/// let (tx, mut rx) = watch::channel("hello");
///
/// tx.send("goodbye").unwrap();
/// tokio::spawn(async move {
/// sleep(Duration::from_micros(100)).await;
/// tx.send("goodbye").unwrap();
/// });
///
/// // here we subscribe to a second receiver
/// // now in case of using `changed` we would have
/// // to first check the current value and then wait
/// // for changes or else `changed` would hang.
/// let mut rx2 = tx.subscribe();
///
/// // in place of changed we have use `wait_for`
/// // which would automatically check the current value
/// // and wait for changes until the closure returns true.
/// assert!(rx2.wait_for(|val| *val == "goodbye").await.is_ok());
/// assert_eq!(*rx2.borrow(), "goodbye");
/// assert!(rx.wait_for(|val| *val == "goodbye").await.is_ok());
/// assert_eq!(*rx.borrow(), "goodbye");
/// }
/// ```
pub async fn wait_for(
Expand Down

0 comments on commit 7000f08

Please sign in to comment.