From 5a1a6dc90c6d5a7eb5f31ae215f9ec383d6767aa Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Wed, 2 Sep 2020 05:57:48 +0200 Subject: sync: watch channel breaking changes (#2806) Fixes: #2172 --- tokio/src/sync/watch.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'tokio/src/sync/watch.rs') diff --git a/tokio/src/sync/watch.rs b/tokio/src/sync/watch.rs index 13033d9e..f6660b6e 100644 --- a/tokio/src/sync/watch.rs +++ b/tokio/src/sync/watch.rs @@ -23,12 +23,12 @@ //! let (tx, mut rx) = watch::channel("hello"); //! //! tokio::spawn(async move { -//! while let Some(value) = rx.recv().await { +//! while let Some(value) = Some(rx.recv().await) { //! println!("received = {:?}", value); //! } //! }); //! -//! tx.broadcast("world")?; +//! tx.send("world")?; //! # Ok(()) //! # } //! ``` @@ -162,12 +162,12 @@ const CLOSED: usize = 1; /// let (tx, mut rx) = watch::channel("hello"); /// /// tokio::spawn(async move { -/// while let Some(value) = rx.recv().await { +/// while let Some(value) = Some(rx.recv().await) { /// println!("received = {:?}", value); /// } /// }); /// -/// tx.broadcast("world")?; +/// tx.send("world")?; /// # Ok(()) /// # } /// ``` @@ -223,7 +223,7 @@ impl Receiver { // TODO: document #[doc(hidden)] - pub fn poll_recv_ref<'a>(&'a mut self, cx: &mut Context<'_>) -> Poll>> { + pub fn poll_recv_ref<'a>(&'a mut self, cx: &mut Context<'_>) -> Poll> { // Make sure the task is up to date self.inner.waker.register_by_ref(cx.waker()); @@ -233,12 +233,14 @@ impl Receiver { if self.inner.version.swap(version, Relaxed) != version { let inner = self.shared.value.read().unwrap(); - return Ready(Some(Ref { inner })); + return Ready(Ref { inner }); } if CLOSED == state & CLOSED { // The `Store` handle has been dropped. - return Ready(None); + let inner = self.shared.value.read().unwrap(); + + return Ready(Ref { inner }); } Pending @@ -264,25 +266,25 @@ impl Receiver { /// async fn main() { /// let (tx, mut rx) = watch::channel("hello"); /// - /// let v = rx.recv().await.unwrap(); + /// let v = rx.recv().await; /// assert_eq!(v, "hello"); /// /// tokio::spawn(async move { - /// tx.broadcast("goodbye").unwrap(); + /// tx.send("goodbye").unwrap(); /// }); /// /// // Waits for the new task to spawn and send the value. - /// let v = rx.recv().await.unwrap(); + /// let v = rx.recv().await; /// assert_eq!(v, "goodbye"); /// /// let v = rx.recv().await; - /// assert!(v.is_none()); + /// assert_eq!(v, "goodbye"); /// } /// ``` - pub async fn recv(&mut self) -> Option { + pub async fn recv(&mut self) -> T { poll_fn(|cx| { let v_ref = ready!(self.poll_recv_ref(cx)); - Poll::Ready(v_ref.map(|v_ref| (*v_ref).clone())) + Poll::Ready((*v_ref).clone()) }) .await } @@ -295,7 +297,7 @@ impl crate::stream::Stream for Receiver { fn poll_next(mut self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let v_ref = ready!(self.poll_recv_ref(cx)); - Poll::Ready(v_ref.map(|v_ref| (*v_ref).clone())) + Poll::Ready(Some((*v_ref).clone())) } } @@ -318,8 +320,8 @@ impl Drop for Receiver { } impl Sender { - /// Broadcasts a new value via the channel, notifying all receivers. - pub fn broadcast(&self, value: T) -> Result<(), error::SendError> { + /// Sends a new value via the channel, notifying all receivers. + pub fn send(&self, value: T) -> Result<(), error::SendError> { let shared = match self.shared.upgrade() { Some(shared) => shared, // All `Watch` handles have been canceled -- cgit v1.2.3