Skip to content

Commit ffdf111

Browse files
committed
Implement the error trait for errors in std::sync.
1 parent f4f10db commit ffdf111

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/libstd/sync/poison.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use prelude::v1::*;
1212

1313
use cell::UnsafeCell;
14-
use error::FromError;
14+
use error::{Error, FromError};
1515
use fmt;
1616
use thread::Thread;
1717

@@ -92,7 +92,13 @@ pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;
9292

9393
impl<T> fmt::Show for PoisonError<T> {
9494
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95-
"poisoned lock: another task failed inside".fmt(f)
95+
self.description().fmt(f)
96+
}
97+
}
98+
99+
impl<T> Error for PoisonError<T> {
100+
fn description(&self) -> &str {
101+
"poisoned lock: another task failed inside"
96102
}
97103
}
98104

@@ -126,11 +132,22 @@ impl<T> FromError<PoisonError<T>> for TryLockError<T> {
126132

127133
impl<T> fmt::Show for TryLockError<T> {
128134
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135+
self.description().fmt(f)
136+
}
137+
}
138+
139+
impl<T> Error for TryLockError<T> {
140+
fn description(&self) -> &str {
141+
match *self {
142+
TryLockError::Poisoned(ref p) => p.description(),
143+
TryLockError::WouldBlock => "try_lock failed because the operation would block"
144+
}
145+
}
146+
147+
fn cause(&self) -> Option<&Error> {
129148
match *self {
130-
TryLockError::Poisoned(ref p) => p.fmt(f),
131-
TryLockError::WouldBlock => {
132-
"try_lock failed because the operation would block".fmt(f)
133-
}
149+
TryLockError::Poisoned(ref p) => Some(p),
150+
_ => None
134151
}
135152
}
136153
}

0 commit comments

Comments
 (0)