Skip to content

Commit 428c875

Browse files
committed
Add std::sync::mpsc::Receiver::recv_deadline()
Essentially renames recv_max_until to recv_deadline (mostly copying recv_timeout documentation). This function is useful to avoid the often unnecessary call to Instant::now in recv_timeout (e.g. when the user already has a deadline). A concrete example would be something along those lines: ```rust use std::sync::mpsc::Receiver; use std::time::{Duration, Instant}; /// Reads a batch of elements /// /// Returns as soon as `max_size` elements have been received or `timeout` expires. fn recv_batch_timeout<T>(receiver: &Receiver<T>, timeout: Duration, max_size: usize) -> Vec<T> { recv_batch_deadline(receiver, Instant::now() + timeout, max_size) } /// Reads a batch of elements /// /// Returns as soon as `max_size` elements have been received or `deadline` is reached. fn recv_batch_deadline<T>(receiver: &Receiver<T>, deadline: Instant, max_size: usize) -> Vec<T> { let mut result = Vec::new(); while let Ok(x) = receiver.recv_deadline(deadline) { result.push(x); if result.len() == max_size { break; } } result } ```
1 parent 24bb4d1 commit 428c875

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

src/libstd/sync/mpsc/mod.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,11 +1297,70 @@ impl<T> Receiver<T> {
12971297
Err(TryRecvError::Disconnected)
12981298
=> Err(RecvTimeoutError::Disconnected),
12991299
Err(TryRecvError::Empty)
1300-
=> self.recv_max_until(Instant::now() + timeout)
1300+
=> self.recv_deadline(Instant::now() + timeout)
13011301
}
13021302
}
13031303

1304-
fn recv_max_until(&self, deadline: Instant) -> Result<T, RecvTimeoutError> {
1304+
/// Attempts to wait for a value on this receiver, returning an error if the
1305+
/// corresponding channel has hung up, or if `deadline` is reached.
1306+
///
1307+
/// This function will always block the current thread if there is no data
1308+
/// available and it's possible for more data to be sent. Once a message is
1309+
/// sent to the corresponding [`Sender`][] (or [`SyncSender`]), then this
1310+
/// receiver will wake up and return that message.
1311+
///
1312+
/// If the corresponding [`Sender`] has disconnected, or it disconnects while
1313+
/// this call is blocking, this call will wake up and return [`Err`] to
1314+
/// indicate that no more messages can ever be received on this channel.
1315+
/// However, since channels are buffered, messages sent before the disconnect
1316+
/// will still be properly received.
1317+
///
1318+
/// [`Sender`]: struct.Sender.html
1319+
/// [`SyncSender`]: struct.SyncSender.html
1320+
/// [`Err`]: ../../../std/result/enum.Result.html#variant.Err
1321+
///
1322+
/// # Examples
1323+
///
1324+
/// Successfully receiving value before reaching deadline:
1325+
///
1326+
/// ```no_run
1327+
/// use std::thread;
1328+
/// use std::time::{Duration, Instant};
1329+
/// use std::sync::mpsc;
1330+
///
1331+
/// let (send, recv) = mpsc::channel();
1332+
///
1333+
/// thread::spawn(move || {
1334+
/// send.send('a').unwrap();
1335+
/// });
1336+
///
1337+
/// assert_eq!(
1338+
/// recv.recv_deadline(Instant::now() + Duration::from_millis(400)),
1339+
/// Ok('a')
1340+
/// );
1341+
/// ```
1342+
///
1343+
/// Receiving an error upon reaching deadline:
1344+
///
1345+
/// ```no_run
1346+
/// use std::thread;
1347+
/// use std::time::{Duration, Instant};
1348+
/// use std::sync::mpsc;
1349+
///
1350+
/// let (send, recv) = mpsc::channel();
1351+
///
1352+
/// thread::spawn(move || {
1353+
/// thread::sleep(Duration::from_millis(800));
1354+
/// send.send('a').unwrap();
1355+
/// });
1356+
///
1357+
/// assert_eq!(
1358+
/// recv.recv_deadline(Instant::now() + Duration::from_millis(400)),
1359+
/// Err(mpsc::RecvTimeoutError::Timeout)
1360+
/// );
1361+
/// ```
1362+
#[stable(feature = "mpsc_recv_deadline", since = "1.23.0")]
1363+
pub fn recv_deadline(&self, deadline: Instant) -> Result<T, RecvTimeoutError> {
13051364
use self::RecvTimeoutError::*;
13061365

13071366
loop {

0 commit comments

Comments
 (0)