]> git.lizzy.rs Git - rust.git/commitdiff
Implement the error trait for errors in std::sync.
authorMichael Sproul <micsproul@gmail.com>
Sat, 17 Jan 2015 19:31:55 +0000 (11:31 -0800)
committerMichael Sproul <micsproul@gmail.com>
Sat, 17 Jan 2015 22:35:16 +0000 (14:35 -0800)
src/libstd/sync/poison.rs

index cc8c331ef3997c3d2e34467de445907f3050d068..e28c3c37b6f765ee7df620998bd0359b5c04f4fe 100644 (file)
@@ -11,7 +11,7 @@
 use prelude::v1::*;
 
 use cell::UnsafeCell;
-use error::FromError;
+use error::{Error, FromError};
 use fmt;
 use thread::Thread;
 
@@ -92,7 +92,13 @@ pub enum TryLockError<T> {
 
 impl<T> fmt::Show for PoisonError<T> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        "poisoned lock: another task failed inside".fmt(f)
+        self.description().fmt(f)
+    }
+}
+
+impl<T> Error for PoisonError<T> {
+    fn description(&self) -> &str {
+        "poisoned lock: another task failed inside"
     }
 }
 
@@ -126,11 +132,22 @@ fn from_error(err: PoisonError<T>) -> TryLockError<T> {
 
 impl<T> fmt::Show for TryLockError<T> {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        self.description().fmt(f)
+    }
+}
+
+impl<T> Error for TryLockError<T> {
+    fn description(&self) -> &str {
+        match *self {
+            TryLockError::Poisoned(ref p) => p.description(),
+            TryLockError::WouldBlock => "try_lock failed because the operation would block"
+        }
+    }
+
+    fn cause(&self) -> Option<&Error> {
         match *self {
-            TryLockError::Poisoned(ref p) => p.fmt(f),
-            TryLockError::WouldBlock => {
-                "try_lock failed because the operation would block".fmt(f)
-            }
+            TryLockError::Poisoned(ref p) => Some(p),
+            _ => None
         }
     }
 }