Skip to content

Add a method to the mpsc::Receiver for producing a non-blocking iterator #34724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/libstd/sync/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,17 @@ pub struct Iter<'a, T: 'a> {
rx: &'a Receiver<T>
}

/// An iterator that attempts to yield all pending values for a receiver.
/// `None` will be returned when there are no pending values remaining or
/// if the corresponding channel has hung up.
///
/// This Iterator will never block the caller in order to wait for data to
/// become available. Instead, it will return `None`.
#[unstable(feature = "receiver_try_iter", issue = "34931")]
pub struct TryIter<'a, T: 'a> {
rx: &'a Receiver<T>
}

/// An owning iterator over messages on a receiver, this iterator will block
/// whenever `next` is called, waiting for a new message, and `None` will be
/// returned when the corresponding channel has hung up.
Expand Down Expand Up @@ -982,6 +993,16 @@ impl<T> Receiver<T> {
pub fn iter(&self) -> Iter<T> {
Iter { rx: self }
}

/// Returns an iterator that will attempt to yield all pending values.
/// It will return `None` if there are no more pending values or if the
/// channel has hung up. The iterator will never `panic!` or block the
/// user by waiting for values.
#[unstable(feature = "receiver_try_iter", issue = "34931")]
pub fn try_iter(&self) -> TryIter<T> {
TryIter { rx: self }
}

}

impl<T> select::Packet for Receiver<T> {
Expand Down Expand Up @@ -1077,6 +1098,13 @@ impl<'a, T> Iterator for Iter<'a, T> {
fn next(&mut self) -> Option<T> { self.rx.recv().ok() }
}

#[unstable(feature = "receiver_try_iter", issue = "34931")]
impl<'a, T> Iterator for TryIter<'a, T> {
type Item = T;

fn next(&mut self) -> Option<T> { self.rx.try_recv().ok() }
}

#[stable(feature = "receiver_into_iter", since = "1.1.0")]
impl<'a, T> IntoIterator for &'a Receiver<T> {
type Item = T;
Expand Down Expand Up @@ -1814,6 +1842,34 @@ mod tests {
assert_eq!(count_rx.recv().unwrap(), 4);
}

#[test]
fn test_recv_try_iter() {
let (request_tx, request_rx) = channel();
let (response_tx, response_rx) = channel();

// Request `x`s until we have `6`.
let t = thread::spawn(move|| {
let mut count = 0;
loop {
for x in response_rx.try_iter() {
count += x;
if count == 6 {
return count;
}
}
request_tx.send(()).unwrap();
}
});

for _ in request_rx.iter() {
if response_tx.send(2).is_err() {
break;
}
}

assert_eq!(t.join().unwrap(), 6);
}

#[test]
fn test_recv_into_iter_owned() {
let mut iter = {
Expand Down