Skip to content

Commit

Permalink
sync: add broadcast::Receiver::blocking_recv (#5690)
Browse files Browse the repository at this point in the history
  • Loading branch information
nylonicious committed May 15, 2023
1 parent 4e2ef63 commit dd9471d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tokio/src/sync/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,33 @@ impl<T: Clone> Receiver<T> {
let guard = self.recv_ref(None)?;
guard.clone_value().ok_or(TryRecvError::Closed)
}

/// 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::broadcast;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, mut rx) = broadcast::channel(16);
///
/// let sync_code = thread::spawn(move || {
/// assert_eq!(rx.blocking_recv(), Ok(10));
/// });
///
/// let _ = tx.send(10);
/// sync_code.join().unwrap();
/// }
pub fn blocking_recv(&mut self) -> Result<T, RecvError> {
crate::future::block_on(self.recv())
}
}

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

0 comments on commit dd9471d

Please sign in to comment.