Skip to content

Allow more "error" values in try_recv() #11112

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

Closed
wants to merge 1 commit into from

Conversation

alexcrichton
Copy link
Member

This should allow callers to know whether the channel was empty or disconnected
without having to block.

Closes #11087

@bill-myers
Copy link
Contributor

Renaming it to a try_recv_opt that returns Option<Option> seems a better approach, as it doesn't introduce an extra enum type, and follows the simple logic that a nonblocking version of a function (try_recv_opt vs recv_opt in this case) wraps the result of the blocking function in an Option, which allows to convert code between the blocking and non-blocking versions in a straightforward fashion.

Also, Result is supposed to be used for things were it is reasonable to fail! on any error, and it is of course usually not appropriate to fail! on a non-blocking function not returning input yet (that leads to random timing/scheduler dependent failures unless there's some extra serialization to prevent it).

BTW, maybe changing recv_opt to return Result<T, ()> instead of Option might make sense, but that's orthogonal.

@brson
Copy link
Contributor

brson commented Jan 13, 2014

I think I agree that this should not return a Result. Is it really an error for the channel to be closed? In many cases it's part of the normal program logic.

@brson
Copy link
Contributor

brson commented Jan 13, 2014

Option<Option<T>> though is not a very nice looking type signature.

@brson
Copy link
Contributor

brson commented Jan 13, 2014

I'm having a hard time even imagining what Option<Option<T>> means here. Maybe we introduce an enum just for this purpose.

@alexcrichton
Copy link
Member Author

Changed to enum TryRecvResult<T>

This should allow callers to know whether the channel was empty or disconnected
without having to block.

Closes rust-lang#11087
bors added a commit that referenced this pull request Jan 15, 2014
This should allow callers to know whether the channel was empty or disconnected
without having to block.

Closes #11087
@bors bors closed this Jan 15, 2014
@alexcrichton alexcrichton deleted the issue-11087 branch January 16, 2014 01:52
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.

There needs to be a way to combine the effects of try_recv and recv_opt
4 participants