mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-09 00:00:08 +02:00
sync: add broadcast::Receiver::blocking_recv (#5690)
This commit is contained in:
@@ -1194,6 +1194,33 @@ impl<T: Clone> Receiver<T> {
|
|||||||
let guard = self.recv_ref(None)?;
|
let guard = self.recv_ref(None)?;
|
||||||
guard.clone_value().ok_or(TryRecvError::Closed)
|
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> {
|
impl<T> Drop for Receiver<T> {
|
||||||
|
|||||||
Reference in New Issue
Block a user