Skip to content

Commit

Permalink
Rollup merge of rust-lang#45506 - ia0:mpsc_recv_error_from, r=alexcri…
Browse files Browse the repository at this point in the history
…chton

Implement From<RecvError> for TryRecvError and RecvTimeoutError

According to the documentation, it looks to me that `TryRecvError` and `RecvTimeoutError` are strict extensions of `RecvError`. As such, it makes sense to allow conversion from the latter type to the two former types without constraining future developments.

This permits to write `input.recv()?` and `input.recv_timeout(timeout)?` in the same function for example.
  • Loading branch information
kennytm authored Nov 27, 2017
2 parents 58e1234 + 448215d commit 2f012e4
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/libstd/sync/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,15 @@ impl<T: Send> error::Error for TrySendError<T> {
}
}

#[stable(feature = "mpsc_error_conversions", since = "1.23.0")]
impl<T> From<SendError<T>> for TrySendError<T> {
fn from(err: SendError<T>) -> TrySendError<T> {
match err {
SendError(t) => TrySendError::Disconnected(t),
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -1677,6 +1686,15 @@ impl error::Error for TryRecvError {
}
}

#[stable(feature = "mpsc_error_conversions", since = "1.23.0")]
impl From<RecvError> for TryRecvError {
fn from(err: RecvError) -> TryRecvError {
match err {
RecvError => TryRecvError::Disconnected,
}
}
}

#[stable(feature = "mpsc_recv_timeout_error", since = "1.15.0")]
impl fmt::Display for RecvTimeoutError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -1709,6 +1727,15 @@ impl error::Error for RecvTimeoutError {
}
}

#[stable(feature = "mpsc_error_conversions", since = "1.23.0")]
impl From<RecvError> for RecvTimeoutError {
fn from(err: RecvError) -> RecvTimeoutError {
match err {
RecvError => RecvTimeoutError::Disconnected,
}
}
}

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

0 comments on commit 2f012e4

Please # to comment.