summaryrefslogtreecommitdiffstats
path: root/tokio/src/sync
diff options
context:
space:
mode:
authorSunjay Varma <sunjay@users.noreply.github.com>2020-12-12 08:47:35 -0800
committerGitHub <noreply@github.com>2020-12-12 08:47:35 -0800
commitdf20c162ae1308c07073b6a67c8ba4202f52d208 (patch)
tree94fe5abd9735b0c4985d5b38a8d96c51953b0f0b /tokio/src/sync
parentc1ec469ad2af883b001d54e81dad426c01f918cd (diff)
sync: add blocking_recv method to UnboundedReceiver, similar to Receiver::blocking_recv (#3262)HEADmaster
Diffstat (limited to 'tokio/src/sync')
-rw-r--r--tokio/src/sync/mpsc/unbounded.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/tokio/src/sync/mpsc/unbounded.rs b/tokio/src/sync/mpsc/unbounded.rs
index fe882d5b..48fbca96 100644
--- a/tokio/src/sync/mpsc/unbounded.rs
+++ b/tokio/src/sync/mpsc/unbounded.rs
@@ -122,6 +122,36 @@ impl<T> UnboundedReceiver<T> {
poll_fn(|cx| self.poll_recv(cx)).await
}
+ /// 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::mpsc;
+ ///
+ /// #[tokio::main]
+ /// async fn main() {
+ /// let (tx, mut rx) = mpsc::unbounded_channel::<u8>();
+ ///
+ /// let sync_code = thread::spawn(move || {
+ /// assert_eq!(Some(10), rx.blocking_recv());
+ /// });
+ ///
+ /// let _ = tx.send(10);
+ /// sync_code.join().unwrap();
+ /// }
+ /// ```
+ #[cfg(feature = "sync")]
+ pub fn blocking_recv(&mut self) -> Option<T> {
+ crate::future::block_on(self.recv())
+ }
+
/// Attempts to return a pending value on this receiver without blocking.
///
/// This method will never block the caller in order to wait for data to