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

Add Error implementation for std::sync::mpsc::RecvTimeoutError. #37527

Merged
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
32 changes: 32 additions & 0 deletions src/libstd/sync/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,38 @@ impl error::Error for TryRecvError {
}
}

#[stable(feature = "mpsc_recv_timeout_error", since = "1.14.0")]
impl fmt::Display for RecvTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write this entire function as self.description().fmt(f).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@durka mentioned this here: #37527 (comment), but since the other implementations for display also duplicate this code, I think it's best to leave this as-is for now. I'll be happy to file another PR (once this lands) deduplicating this code.

RecvTimeoutError::Timeout => {
"timed out waiting on channel".fmt(f)
}
RecvTimeoutError::Disconnected => {
"channel is empty and sending half is closed".fmt(f)
}
}
}
}

#[stable(feature = "mpsc_recv_timeout_error", since = "1.14.0")]
impl error::Error for RecvTimeoutError {
fn description(&self) -> &str {
match *self {
RecvTimeoutError::Timeout => {
"timed out waiting on channel"
}
RecvTimeoutError::Disconnected => {
"channel is empty and sending half is closed"
}
}
}

fn cause(&self) -> Option<&error::Error> {
None
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're also going to need a Display implementation.

#[cfg(all(test, not(target_os = "emscripten")))]
mod tests {
use env;
Expand Down