From bcb968af8494d2808c2986104d6e67d0b276b1fe Mon Sep 17 00:00:00 2001 From: 0xd34d10cc <0xd34d10cc@gmail.com> Date: Mon, 10 Jan 2022 13:42:16 +0000 Subject: [PATCH] sync: add `blocking_recv` to `oneshot::Receiver` (#4334) --- tokio/src/sync/oneshot.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tokio/src/sync/oneshot.rs b/tokio/src/sync/oneshot.rs index cfc92259d39..2240074e733 100644 --- a/tokio/src/sync/oneshot.rs +++ b/tokio/src/sync/oneshot.rs @@ -1014,6 +1014,36 @@ impl Receiver { self.inner = None; result } + + /// Blocking receive to call outside of asynchronous contexts. + /// + /// # Panics + /// + /// This function panics if called within an asynchronous execution + /// context. + /// + /// # Examples + /// + /// ``` + /// use std::thread; + /// use tokio::sync::oneshot; + /// + /// #[tokio::main] + /// async fn main() { + /// let (tx, rx) = oneshot::channel::(); + /// + /// let sync_code = thread::spawn(move || { + /// assert_eq!(Ok(10), rx.blocking_recv()); + /// }); + /// + /// let _ = tx.send(10); + /// sync_code.join().unwrap(); + /// } + /// ``` + #[cfg(feature = "sync")] + pub fn blocking_recv(self) -> Result { + crate::future::block_on(self) + } } impl Drop for Receiver {