about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2017-11-29 18:37:43 +0800
committerGitHub <noreply@github.com>2017-11-29 18:37:43 +0800
commit0ec3aee569a8a34117d265e48eb980a955a2186d (patch)
tree6839d3966e0bb4d92f20a1f3c0b360fee8d4f453 /src/liballoc
parent77ab3a1d5ff69c0cb3eb85a75ef734eaf7429f1b (diff)
parent8e025d8009b661bde3397bf323aa088ba23e46d1 (diff)
downloadrust-0ec3aee569a8a34117d265e48eb980a955a2186d.tar.gz
rust-0ec3aee569a8a34117d265e48eb980a955a2186d.zip
Rollup merge of #45969 - ia0:mpsc_recv_deadline, r=alexcrichton
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
}
```
Diffstat (limited to 'src/liballoc')
0 files changed, 0 insertions, 0 deletions