Skip to content

Commit 1656d8e

Browse files
barafaelRafael Bachmann
and
Rafael Bachmann
authored
sync: add mpsc::Receiver::blocking_recv_many (#6867)
Fixes: #6865 Co-authored-by: Rafael Bachmann <[email protected]>
1 parent c9e998e commit 1656d8e

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

tokio/src/sync/mpsc/bounded.rs

+10
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,16 @@ impl<T> Receiver<T> {
419419
crate::future::block_on(self.recv())
420420
}
421421

422+
/// Variant of [`Self::recv_many`] for blocking contexts.
423+
///
424+
/// The same conditions as in [`Self::blocking_recv`] apply.
425+
#[track_caller]
426+
#[cfg(feature = "sync")]
427+
#[cfg_attr(docsrs, doc(alias = "recv_many_blocking"))]
428+
pub fn blocking_recv_many(&mut self, buffer: &mut Vec<T>, limit: usize) -> usize {
429+
crate::future::block_on(self.recv_many(buffer, limit))
430+
}
431+
422432
/// Closes the receiving half of a channel without dropping it.
423433
///
424434
/// This prevents any further messages from being sent on the channel while

tokio/src/sync/mpsc/unbounded.rs

+10
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ impl<T> UnboundedReceiver<T> {
319319
crate::future::block_on(self.recv())
320320
}
321321

322+
/// Variant of [`Self::recv_many`] for blocking contexts.
323+
///
324+
/// The same conditions as in [`Self::blocking_recv`] apply.
325+
#[track_caller]
326+
#[cfg(feature = "sync")]
327+
#[cfg_attr(docsrs, doc(alias = "recv_many_blocking"))]
328+
pub fn blocking_recv_many(&mut self, buffer: &mut Vec<T>, limit: usize) -> usize {
329+
crate::future::block_on(self.recv_many(buffer, limit))
330+
}
331+
322332
/// Closes the receiving half of a channel, without dropping it.
323333
///
324334
/// This prevents any further messages from being sent on the channel while

tokio/tests/sync_panic.rs

+33
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ fn mpsc_bounded_receiver_blocking_recv_panic_caller() -> Result<(), Box<dyn Erro
129129
Ok(())
130130
}
131131

132+
#[test]
133+
fn mpsc_bounded_receiver_blocking_recv_many_panic_caller() -> Result<(), Box<dyn Error>> {
134+
let panic_location_file = test_panic(|| {
135+
let rt = current_thread();
136+
let (_tx, mut rx) = mpsc::channel::<u8>(1);
137+
rt.block_on(async {
138+
let _ = rx.blocking_recv();
139+
});
140+
});
141+
142+
// The panic location should be in this file
143+
assert_eq!(&panic_location_file.unwrap(), file!());
144+
145+
Ok(())
146+
}
147+
132148
#[test]
133149
fn mpsc_bounded_sender_blocking_send_panic_caller() -> Result<(), Box<dyn Error>> {
134150
let panic_location_file = test_panic(|| {
@@ -161,6 +177,23 @@ fn mpsc_unbounded_receiver_blocking_recv_panic_caller() -> Result<(), Box<dyn Er
161177
Ok(())
162178
}
163179

180+
#[test]
181+
fn mpsc_unbounded_receiver_blocking_recv_many_panic_caller() -> Result<(), Box<dyn Error>> {
182+
let panic_location_file = test_panic(|| {
183+
let rt = current_thread();
184+
let (_tx, mut rx) = mpsc::unbounded_channel::<u8>();
185+
let mut vec = vec![];
186+
rt.block_on(async {
187+
let _ = rx.blocking_recv_many(&mut vec, 1);
188+
});
189+
});
190+
191+
// The panic location should be in this file
192+
assert_eq!(&panic_location_file.unwrap(), file!());
193+
194+
Ok(())
195+
}
196+
164197
#[test]
165198
fn semaphore_merge_unrelated_owned_permits() -> Result<(), Box<dyn Error>> {
166199
let panic_location_file = test_panic(|| {

0 commit comments

Comments
 (0)