Skip to content

Commit

Permalink
sync: add blocking_recv to oneshot::Receiver (#4334)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xd34d10cc committed Jan 10, 2022
1 parent cec1bc1 commit bcb968a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tokio/src/sync/oneshot.rs
Expand Up @@ -1014,6 +1014,36 @@ impl<T> Receiver<T> {
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::<u8>();
///
/// 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<T, RecvError> {
crate::future::block_on(self)
}
}

impl<T> Drop for Receiver<T> {
Expand Down

0 comments on commit bcb968a

Please sign in to comment.