about summary refs log tree commit diff
path: root/src/libstd/sync
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2017-11-28 03:16:41 +0800
committerGitHub <noreply@github.com>2017-11-28 03:16:41 +0800
commit2f012e4405c6d5bdd2855f3e1c416bd6e75808d8 (patch)
tree89558aff45e969e64427396ba07403a46c9a0763 /src/libstd/sync
parent58e1234cddd996378cb9df6bed537b9c08a6df73 (diff)
parent448215d226f2946c08d01ff230570a6e3b64b1de (diff)
downloadrust-2f012e4405c6d5bdd2855f3e1c416bd6e75808d8.tar.gz
rust-2f012e4405c6d5bdd2855f3e1c416bd6e75808d8.zip
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.
Diffstat (limited to 'src/libstd/sync')
-rw-r--r--src/libstd/sync/mpsc/mod.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs
index 45a26e594b0..8d7f60f9d2c 100644
--- a/src/libstd/sync/mpsc/mod.rs
+++ b/src/libstd/sync/mpsc/mod.rs
@@ -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 {
@@ -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 {
@@ -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;