Skip to content

Add an iterator for receiving messages from GenericPorts #10688

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 1 commit into from
Nov 27, 2013

Conversation

brendanzab
Copy link
Member

I've noticed I use this pattern quite a bit:

do spawn {
    loop {
        match port.try_recv() {
            Some(x) => ...,
            None => ...,
        }
    }
}

The RecvIterator, returned from a default recv_iter method on the GenericPort trait, allows you to reduce this down to:

do spawn {
    for x in port.recv_iter() {
        ...
    }
}

As demonstrated in the tests, you can also access the port from within the for block for further recving and peeking with no borrow errors, which is quite nice.

bors added a commit that referenced this pull request Nov 27, 2013
I've noticed I use this pattern quite a bit:

~~~rust
do spawn {
    loop {
        match port.try_recv() {
            Some(x) => ...,
            None => ...,
        }
    }
}
~~~

The `RecvIterator`, returned from a default `recv_iter` method on the `GenericPort` trait, allows you to reduce this down to:

~~~rust
do spawn {
    for x in port.recv_iter() {
        ...
    }
}
~~~

As demonstrated in the tests, you can also access the port from within the `for` block for further `recv`ing and `peek`ing with no borrow errors, which is quite nice.
@bors bors closed this Nov 27, 2013
@bors bors merged commit 31da6b7 into rust-lang:master Nov 27, 2013
@brendanzab brendanzab deleted the recv_iter branch November 27, 2013 11:24
flip1995 pushed a commit to flip1995/rust that referenced this pull request Apr 23, 2023
clippy: drop unused semver crate

Drop unused `semver` crate dependency.

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants