rollup merge of #23541: aturon/stab-error
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] r? @alexcrichton Closes #21790
This commit is contained in:
commit
b97e1cc28e
@ -82,16 +82,21 @@
|
||||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use prelude::*;
|
||||
use fmt::Display;
|
||||
use fmt::{Debug, Display};
|
||||
|
||||
/// Base functionality for all errors in Rust.
|
||||
#[unstable(feature = "core",
|
||||
reason = "the exact API of this trait may change")]
|
||||
pub trait Error: Display {
|
||||
/// A short description of the error; usually a static string.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub trait Error: Debug + Display + Send {
|
||||
/// A short description of the error.
|
||||
///
|
||||
/// The description should not contain newlines or sentence-ending
|
||||
/// punctuation, to facilitate embedding in larger user-facing
|
||||
/// strings.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn description(&self) -> &str;
|
||||
|
||||
/// The lower-level cause of this error, if any.
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn cause(&self) -> Option<&Error> { None }
|
||||
}
|
||||
|
||||
|
@ -258,7 +258,7 @@ impl<W> FromError<IntoInnerError<W>> for Error {
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<W> error::Error for IntoInnerError<W> {
|
||||
impl<W: Send + fmt::Debug> error::Error for IntoInnerError<W> {
|
||||
fn description(&self) -> &str {
|
||||
error::Error::description(self.error())
|
||||
}
|
||||
|
@ -977,7 +977,7 @@ impl<T> fmt::Display for SendError<T> {
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> error::Error for SendError<T> {
|
||||
impl<T: Send> error::Error for SendError<T> {
|
||||
|
||||
fn description(&self) -> &str {
|
||||
"sending on a closed channel"
|
||||
@ -1013,7 +1013,7 @@ impl<T> fmt::Display for TrySendError<T> {
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> error::Error for TrySendError<T> {
|
||||
impl<T: Send> error::Error for TrySendError<T> {
|
||||
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
|
@ -105,11 +105,11 @@ impl<T> fmt::Debug for PoisonError<T> {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> fmt::Display for PoisonError<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.description().fmt(f)
|
||||
"poisoned lock: another task failed inside".fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Error for PoisonError<T> {
|
||||
impl<T: Send> Error for PoisonError<T> {
|
||||
fn description(&self) -> &str {
|
||||
"poisoned lock: another task failed inside"
|
||||
}
|
||||
@ -161,13 +161,13 @@ impl<T> fmt::Debug for TryLockError<T> {
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T> fmt::Display for TryLockError<T> {
|
||||
impl<T: Send> fmt::Display for TryLockError<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.description().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Error for TryLockError<T> {
|
||||
impl<T: Send> Error for TryLockError<T> {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
TryLockError::Poisoned(ref p) => p.description(),
|
||||
|
@ -20,6 +20,7 @@ pub type CommandError = Box<Error + 'static>;
|
||||
pub type CommandResult<T> = Result<T, CommandError>;
|
||||
|
||||
pub fn err(s: &str) -> CliError {
|
||||
#[derive(Debug)]
|
||||
struct E(String);
|
||||
|
||||
impl Error for E {
|
||||
|
Loading…
x
Reference in New Issue
Block a user