Skip to content

Commit 9231ceb

Browse files
committed
Stabilize the Error trait
This small commit stabilizes the `Error` trait as-is, except that `Send` and `Debug` are added as constraints. The `Send` constraint is because most uses of `Error` will be for trait objects, and by default we would like these objects to be transferrable between threads. The `Debug` constraint is to ensure that e.g. `Box<Error>` is `Debug`, and because types that implement `Display` should certainly implement `Debug` in any case. In the near future we expect to add `Any`-like downcasting features to `Error`, but this is waiting on some additional mechanisms (`Reflect`). It will be added before 1.0 via default methods. [breaking-change]
1 parent 7f53b94 commit 9231ceb

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

src/libcore/error.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,21 @@
8282
#![stable(feature = "rust1", since = "1.0.0")]
8383

8484
use prelude::*;
85-
use fmt::Display;
85+
use fmt::{Debug, Display};
8686

8787
/// Base functionality for all errors in Rust.
88-
#[unstable(feature = "core",
89-
reason = "the exact API of this trait may change")]
90-
pub trait Error: Display {
91-
/// A short description of the error; usually a static string.
88+
#[stable(feature = "rust1", since = "1.0.0")]
89+
pub trait Error: Debug + Display + Send {
90+
/// A short description of the error.
91+
///
92+
/// The description should not contain newlines or sentence-ending
93+
/// punctuation, to facilitate embedding in larger user-facing
94+
/// strings.
95+
#[stable(feature = "rust1", since = "1.0.0")]
9296
fn description(&self) -> &str;
9397

9498
/// The lower-level cause of this error, if any.
99+
#[stable(feature = "rust1", since = "1.0.0")]
95100
fn cause(&self) -> Option<&Error> { None }
96101
}
97102

src/libstd/io/buffered.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl<W> FromError<IntoInnerError<W>> for Error {
258258
}
259259

260260
#[stable(feature = "rust1", since = "1.0.0")]
261-
impl<W> error::Error for IntoInnerError<W> {
261+
impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
262262
fn description(&self) -> &str {
263263
error::Error::description(self.error())
264264
}

src/libstd/sync/mpsc/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ impl<T> fmt::Display for SendError<T> {
977977
}
978978

979979
#[stable(feature = "rust1", since = "1.0.0")]
980-
impl<T> error::Error for SendError<T> {
980+
impl<T: Send> error::Error for SendError<T> {
981981

982982
fn description(&self) -> &str {
983983
"sending on a closed channel"
@@ -1013,7 +1013,7 @@ impl<T> fmt::Display for TrySendError<T> {
10131013
}
10141014

10151015
#[stable(feature = "rust1", since = "1.0.0")]
1016-
impl<T> error::Error for TrySendError<T> {
1016+
impl<T: Send> error::Error for TrySendError<T> {
10171017

10181018
fn description(&self) -> &str {
10191019
match *self {

src/libstd/sync/poison.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ impl<T> fmt::Debug for PoisonError<T> {
105105
#[stable(feature = "rust1", since = "1.0.0")]
106106
impl<T> fmt::Display for PoisonError<T> {
107107
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108-
self.description().fmt(f)
108+
"poisoned lock: another task failed inside".fmt(f)
109109
}
110110
}
111111

112-
impl<T> Error for PoisonError<T> {
112+
impl<T: Send> Error for PoisonError<T> {
113113
fn description(&self) -> &str {
114114
"poisoned lock: another task failed inside"
115115
}
@@ -161,13 +161,13 @@ impl<T> fmt::Debug for TryLockError<T> {
161161
}
162162

163163
#[stable(feature = "rust1", since = "1.0.0")]
164-
impl<T> fmt::Display for TryLockError<T> {
164+
impl<T: Send> fmt::Display for TryLockError<T> {
165165
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
166166
self.description().fmt(f)
167167
}
168168
}
169169

170-
impl<T> Error for TryLockError<T> {
170+
impl<T: Send> Error for TryLockError<T> {
171171
fn description(&self) -> &str {
172172
match *self {
173173
TryLockError::Poisoned(ref p) => p.description(),

src/rustbook/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub type CommandError = Box<Error + 'static>;
2020
pub type CommandResult<T> = Result<T, CommandError>;
2121

2222
pub fn err(s: &str) -> CliError {
23+
#[derive(Debug)]
2324
struct E(String);
2425

2526
impl Error for E {

0 commit comments

Comments
 (0)