Skip to content

Commit 2f012e4

Browse files
authored
Rollup merge of rust-lang#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.
2 parents 58e1234 + 448215d commit 2f012e4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libstd/sync/mpsc/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,15 @@ impl<T: Send> error::Error for TrySendError<T> {
16251625
}
16261626
}
16271627

1628+
#[stable(feature = "mpsc_error_conversions", since = "1.23.0")]
1629+
impl<T> From<SendError<T>> for TrySendError<T> {
1630+
fn from(err: SendError<T>) -> TrySendError<T> {
1631+
match err {
1632+
SendError(t) => TrySendError::Disconnected(t),
1633+
}
1634+
}
1635+
}
1636+
16281637
#[stable(feature = "rust1", since = "1.0.0")]
16291638
impl fmt::Display for RecvError {
16301639
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -1677,6 +1686,15 @@ impl error::Error for TryRecvError {
16771686
}
16781687
}
16791688

1689+
#[stable(feature = "mpsc_error_conversions", since = "1.23.0")]
1690+
impl From<RecvError> for TryRecvError {
1691+
fn from(err: RecvError) -> TryRecvError {
1692+
match err {
1693+
RecvError => TryRecvError::Disconnected,
1694+
}
1695+
}
1696+
}
1697+
16801698
#[stable(feature = "mpsc_recv_timeout_error", since = "1.15.0")]
16811699
impl fmt::Display for RecvTimeoutError {
16821700
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -1709,6 +1727,15 @@ impl error::Error for RecvTimeoutError {
17091727
}
17101728
}
17111729

1730+
#[stable(feature = "mpsc_error_conversions", since = "1.23.0")]
1731+
impl From<RecvError> for RecvTimeoutError {
1732+
fn from(err: RecvError) -> RecvTimeoutError {
1733+
match err {
1734+
RecvError => RecvTimeoutError::Disconnected,
1735+
}
1736+
}
1737+
}
1738+
17121739
#[cfg(all(test, not(target_os = "emscripten")))]
17131740
mod tests {
17141741
use env;

0 commit comments

Comments
 (0)