Skip to content
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

Allow calling UnboundedReceiver::try_next after None #2369

Merged
merged 1 commit into from Mar 1, 2021

Commits on Mar 1, 2021

  1. Allow calling UnboundedReceiver::try_next after None

    Allow calling `UnboundedReceiver::try_next` and `Receiver::try_next`
    after `None`: do not panic.
    
    Not-panicking is equally safe, and does not have negative performance
    implication.
    
    It is irrelevant for `Stream` implementation to panic or not (because
    `Stream` behavior is unspecified after `None`), but panicking in
    `try_next` just complicates the interface: returned `Ok(None)` is
    reasonable assumption to have.
    
    Consider this use case: drain the queue on drop by performing
    app-specific cleanup of queued messages.
    
    The obvious implementation would be:
    
    ```
    impl Drop for MyReceiverWrapper {
        fn drop(&mut self) {
            while let Ok(Some(m)) self.try_next() {
                cleanup(m);
            }
        }
    }
    ```
    
    Without this change, I cannot even say for sure how this code need
    to be implemented to avoid panicking. E. g. is `is_closed` enough
    or some additional checks need to be performed?
    stepancheg committed Mar 1, 2021
    Copy the full SHA
    2203386 View commit details
    Browse the repository at this point in the history