Rollup merge of #45506 - ia0:mpsc_recv_error_from, r=alexcrichton

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.
This commit is contained in:
kennytm
2017-11-28 03:16:41 +08:00
committed by GitHub
+27
View File
@@ -1625,6 +1625,15 @@ fn cause(&self) -> Option<&error::Error> {
}
}
#[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 {
@@ -1677,6 +1686,15 @@ fn cause(&self) -> Option<&error::Error> {
}
}
#[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 {
@@ -1709,6 +1727,15 @@ fn cause(&self) -> Option<&error::Error> {
}
}
#[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;