rust/src/libstd
bors 0d7597588d Auto merge of #34724 - mitchmindtree:mpsc_receiver_try_recv, r=alexcrichton
Add a method to the mpsc::Receiver for producing a non-blocking iterator

Currently, the `mpsc::Receiver` offers methods for receiving values in both blocking (`recv`) and non-blocking (`try_recv`) flavours. However only blocking iteration over values is supported. This PR adds a non-blocking iterator to complement the `try_recv` method, just as the blocking iterator complements the `recv` method.

Use-case
-------------

I predominantly use rust in my work on real-time systems and in particular real-time audio generation/processing. I use `mpsc::channel`s to communicate between threads in a purely non-blocking manner. I.e. I might send messages from the GUI thread to the audio thread to update the state of the dsp-graph, or from the audio thread to the GUI thread to display the RMS of each node. These are just a couple examples (I'm probably using 30+ channels across my various projects). I almost exclusively use the `mpsc::Receiver::try_recv` method to avoid blocking any of the real-time threads and causing unwanted glitching/stuttering. Now that I mention it, I can't think of a single time that I personally have used the `recv` method (though I can of course see why it would be useful, and perhaps the common case for many people).

As a result of this experience, I can't help but feel there is a large hole in the `Receiver` API.

| blocking | non-blocking |
|------------|--------------------|
| `recv` | `try_recv` |
| `iter` | 🙀   |

For the most part, I've been working around this using `while let Ok(v) = r.try_recv() { ... }`, however as nice as this is, it is clearly no match for the Iterator API.

As an example, in the majority of my channel use cases I only want to check for *n* number of messages before breaking from the loop so that I don't miss the audio IO callback or hog the GUI thread for too long when an unexpectedly large number of messages are sent. Currently, I have to write something like this:

```rust
let mut take = 100;
while let Ok(msg) = rx.try_recv() {
    // Do stuff with msg
    if take == 0 {
        break;
    }
    take -= 1;
}
```

or wrap the `try_recv` call in a `Range<usize>`/`FilterMap` iterator combo.

On the other hand, this PR would allow for the following:

```rust
for msg in rx.try_iter().take(100) {
    // Do stuff with msg
}
```

I imagine this might also be useful to game devs, embedded or anyone doing message passing across real-time threads.
2016-07-21 22:39:48 -07:00
..
collections Add debug for hash_map::{Entry, VacantEntry, OccupiedEntry} 2016-07-18 01:38:19 +02:00
ffi Document CStr::as_ptr dangers. 2016-06-19 15:14:51 +03:00
io Auto merge of #33974 - habnabit:eintr-retry-for-read-iterators, r=alexcrichton 2016-07-19 01:20:50 -07:00
net Auto merge of #34694 - mathphreak:master, r=alexcrichton 2016-07-20 07:10:09 -07:00
num std: Stabilize APIs for the 1.11.0 release 2016-07-03 10:49:01 -07:00
os std: Fix up stabilization discrepancies 2016-06-23 14:08:11 -07:00
prelude End stdlib module summaries with a full stop. 2016-03-04 17:37:11 -05:00
rand
sync Auto merge of #34724 - mitchmindtree:mpsc_receiver_try_recv, r=alexcrichton 2016-07-21 22:39:48 -07:00
sys Rollup merge of #34456 - tbu-:pr_ptr_null, r=aturon 2016-07-15 10:56:42 +02:00
thread Rollup merge of #34406 - frewsxcv:sleep-ex, r=alexcrichton 2016-06-28 16:05:14 +02:00
time std: Clean out old unstable + deprecated APIs 2016-05-30 20:46:32 -07:00
ascii.rs std: Stabilize APIs for the 1.9 release 2016-04-11 08:57:53 -07:00
build.rs Fix issue where rustbuild expected msvc to have ar 2016-06-16 08:38:06 -04:00
Cargo.toml rustbuild: Add support for crate tests + doctests 2016-05-12 08:52:20 -07:00
env.rs Auto merge of #33526 - steveklabnik:gh21889, r=alexcrichton 2016-07-20 00:48:21 -07:00
error.rs Add examples for std::Error module 2016-07-10 22:29:19 +02:00
fs.rs Remove rustdoc reference to walk_dir 2016-07-18 17:54:15 +09:30
lib.rs rustc: Update stage0 to beta-2016-07-06 2016-07-06 09:29:15 -07:00
macros.rs doc: Mention that writeln! and println! always use LF 2016-07-12 14:39:16 +09:00
memchr.rs Fix a few typos in the code 2016-07-03 10:02:24 +02:00
panic.rs Set unwind_safe_lock_refs stability to 1.12.0. 2016-07-11 07:34:20 -07:00
panicking.rs Revert "Refactored code to access TLS only in case of panic" 2016-07-16 22:19:43 +02:00
path.rs Fix std::path::Path::file_name() doc 2016-07-06 01:26:24 +02:00
primitive_docs.rs Improve primitive integers documentation 2016-07-08 23:03:17 +02:00
process.rs Add doc example for std::process::ExitStatus::success. 2016-07-12 21:32:55 -04:00
rt.rs Auto merge of #33803 - WiSaGaN:feature/rename-main-thread, r=alexcrichton 2016-06-03 19:36:32 -07:00
rtdeps.rs