mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
sync: add blocking_recv method to UnboundedReceiver, similar to Receiver::blocking_recv (#3262)
This commit is contained in:
parent
c1ec469ad2
commit
df20c162ae
@ -122,6 +122,36 @@ impl<T> UnboundedReceiver<T> {
|
|||||||
poll_fn(|cx| self.poll_recv(cx)).await
|
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.
|
/// 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
|
/// This method will never block the caller in order to wait for data to
|
||||||
|
Loading…
x
Reference in New Issue
Block a user