From bcba4aaa5414eeb12b57e86a3abaf61425cef22b Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Mon, 27 Jan 2020 09:11:12 -0800 Subject: docs: write sync mod API docs (#2175) Fixes #2171 --- tokio/src/sync/watch.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'tokio/src/sync/watch.rs') diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index 3e945563..59e3eec0 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -258,6 +258,38 @@ impl Receiver { impl Receiver { /// Attempts to clone the latest value sent via the channel. + /// + /// If this is the first time the function is called on a `Receiver` + /// instance, then the function completes immediately with the **current** + /// value held by the channel. On the next call, the function waits until + /// a new value is sent in the channel. + /// + /// `None` is returned if the `Sender` half is dropped. + /// + /// # Examples + /// + /// ``` + /// use tokio::sync::watch; + /// + /// #[tokio::main] + /// async fn main() { + /// let (tx, mut rx) = watch::channel("hello"); + /// + /// let v = rx.recv().await.unwrap(); + /// assert_eq!(v, "hello"); + /// + /// tokio::spawn(async move { + /// tx.broadcast("goodbye").unwrap(); + /// }); + /// + /// // Waits for the new task to spawn and send the value. + /// let v = rx.recv().await.unwrap(); + /// assert_eq!(v, "goodbye"); + /// + /// let v = rx.recv().await; + /// assert!(v.is_none()); + /// } + /// ``` pub async fn recv(&mut self) -> Option { poll_fn(|cx| { let v_ref = ready!(self.poll_recv_ref(cx)); -- cgit v1.2.3