From e8ed2d41505826df2c3c53131de401f69b1df427 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Fri, 6 Mar 2015 00:24:47 -0800 Subject: [PATCH] Implement std::error::Error for std::sync::mpsc error types --- src/libstd/sync/mpsc/mod.rs | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs index 1a1e9e69e71..2ae1d4a9d50 100644 --- a/src/libstd/sync/mpsc/mod.rs +++ b/src/libstd/sync/mpsc/mod.rs @@ -318,6 +318,7 @@ use prelude::v1::*; use sync::Arc; +use error; use fmt; use mem; use cell::UnsafeCell; @@ -975,6 +976,18 @@ impl fmt::Display for SendError { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl error::Error for SendError { + + fn description(&self) -> &str { + "sending on a closed channel" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for TrySendError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -999,6 +1012,25 @@ impl fmt::Display for TrySendError { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl error::Error for TrySendError { + + fn description(&self) -> &str { + match *self { + TrySendError::Full(..) => { + "sending on a full channel" + } + TrySendError::Disconnected(..) => { + "sending on a closed channel" + } + } + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for RecvError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -1006,6 +1038,18 @@ impl fmt::Display for RecvError { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl error::Error for RecvError { + + fn description(&self) -> &str { + "receiving on a closed channel" + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for TryRecvError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -1020,6 +1064,25 @@ impl fmt::Display for TryRecvError { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl error::Error for TryRecvError { + + fn description(&self) -> &str { + match *self { + TryRecvError::Empty => { + "receiving on an empty channel" + } + TryRecvError::Disconnected => { + "receiving on a closed channel" + } + } + } + + fn cause(&self) -> Option<&error::Error> { + None + } +} + #[cfg(test)] mod test { use prelude::v1::*;